gpt4 book ai didi

sql - 一个语句中的多个查询

转载 作者:行者123 更新时间:2023-11-29 12:31:12 25 4
gpt4 key购买 nike

我需要在 PostgreSQL 中将以下两个查询合并到一个语句中。

select count(*) as REDEEMTOTALUSED 
from voucher
where campaign_id='123' and redeemcount>0;

select count(*) as REDEEMTOTALUNUSED
from voucher
where campaign_id='123' and redeemcount=0;

结果应该如下所示。

REDEEMTOTALUSED | REDEEMTOTALUNUSED1               | 9                

这可能吗?

最佳答案

请尝试:

select 
(select count(*) as REDEEMTOTALUSED
from voucher
where campaign_id='123' and redeemcount>0) AS REDEEMTOTALUSED ,
(select count(*) as REDEEMTOTALUNUSED
from voucher
where campaign_id='123' and redeemcount=0) AS REDEEMTOTALUNUSED

或另一种方式:

    SELECT 
SUM(CASE
WHEN redeemcount>0 THEN 1
ELSE 0
END) as REDEEMTOTALUSED ,
SUM(CASE
WHEN redeemcount=0 THEN 1
ELSE 0
END) as REDEEMTOTALUNUSED
from
voucher
WHERE
campaign_id='123'

关于sql - 一个语句中的多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27955265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com