[英]Two selects based on one select in on query
我可以做一個 select 然后在一個查詢中對結果進行不同的選擇嗎?
現在我想做類似的事情(這是行不通的)
select
(select count(*), sum(amount) from view where amount > 5),
(select count(*), sum(amount) from view where amount < 5)
from
(select id, amount from warehouse where createDate = '2019-01-01') as view;
我不想 select 視圖然后 select 一些數據與基於視圖的附加過濾。
您可以使用條件聚合:
select count(*),
sum(amount) filter (where waga > 5),
sum(amount) filter (where amount < 5)
from warehouse
where createdate = date '2019-01-01'
關於您可以使用 WITH 子句的一般語法問題:
with v as
(
select id, amount from warehouse where createDate = '2019-01-01'
)
select * from
(
(select count(*), sum(amount) from v where waga > 5) as count1,
(select count(*), sum(amount) from v where amount < 5) as count2
);
(我並不是說它會更快;這只是一種使用“內聯”視圖的方法)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.