[英]Two selects based on one select in on query
Can I do one select and then do different selects on the result in one query?我可以做一个 select 然后在一个查询中对结果进行不同的选择吗?
Now I want to do something like that (which is not working)现在我想做类似的事情(这是行不通的)
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;
I don't want to select view and then select some data with additional filtering based on the view.我不想 select 视图然后 select 一些数据与基于视图的附加过滤。
You can use conditional aggregation:您可以使用条件聚合:
select count(*),
sum(amount) filter (where waga > 5),
sum(amount) filter (where amount < 5)
from warehouse
where createdate = date '2019-01-01'
About the general syntax question you could use WITH clause:关于您可以使用 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
);
(I don't mean it will be faster; it's just a way to use an "inline" view). (我并不是说它会更快;这只是一种使用“内联”视图的方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.