gpt4 book ai didi

SQL - 如何计算一列中多次出现的值

转载 作者:行者123 更新时间:2023-12-04 21:04:27 25 4
gpt4 key购买 nike

我有一张 table ,上面有 StatusID具有许多不同可能值的列,我想要做的是生成以下格式的报告,该报告能够生成各种条件的计数。

所需输出:

Notes | Total | Valid | Invalid | Consults Booked |

Total 是返回的所有行的计数 - 已经在下面的查询中

有效是任何 StatusID那不是 5 , 742
无效的是 5 的计数, 742
咨询预订是 4 的计数

(无效 + 有效应等于总计)

到目前为止,我只能设法获得 Total ,我不知道如何使用 IF 确定其他值或其他任何东西。

查询至今
select notes, tLeadStatus.Status, tLeadStatus.StatusID,
count(*) as Total from LeadManagement.dbo.tLead with (NOLOCK)
left join LeadManagement.dbo.tInternetLead on tLead.Leadid = tinternetlead.leadid
left join LeadManagement..tLeadStatus on tLeadStatus.StatusID = tLead.Status
where (CampaignID = '12327')
and (registerdate >= '2013-03-01' and registerdate < '2013-04-01')
group by notes,tLeadStatus.StatusID,tLeadStatus.Status

最佳答案

SUM(CASE WHEN StatusID NOT IN (5, 7, 42) THEN 1 ELSE 0 END) AS Valid,
SUM(CASE WHEN StatusID IN (5, 7, 42) THEN 1 ELSE 0 END) AS Invalid,
SUM(CASE WHEN StatusId = 4 THEN 1 ELSE 0 END) AS 'Consults Booked'

关于SQL - 如何计算一列中多次出现的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170852/

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