[英]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.