gpt4 book ai didi

c# - SQL 查询 : Select Column1, 行数 (criteria1),行数 (criteria2)

转载 作者:行者123 更新时间:2023-12-02 12:28:17 29 4
gpt4 key购买 nike

我需要有关小型 SQL 查询的帮助。考虑下表:

TicketNo | Rules          | Audit Result
---------------------------------
PR123 | some random strings | Audited
PR123 | some random strings | Not Audited
PR123 | some random strings | Audited
PR123 | some random strings | Audited
PR123 | some random strings | Not Audited
PR123 | some random strings | Not Audited
PR123 | some random strings | Audited
PR999 | some random strings | Audited
PR999 | some random strings | Audited
PR999 | some random strings | Audited
PR999 | some random strings | Audited
PR999 | some random strings | Audited
PR111 | some random strings | Not Audited
PR111 | some random strings | Not Audited
PR111 | some random strings | Not Audited
PR111 | some random strings | Not Audited

我需要以下结果:

Ticket | Count of Audited Rules | Count of NOT-Audited Rules
------------------------------------------------------------
PR123 | 4 | 3
PR999 | 5 | 0
PR111 | 0 | 4

我尝试过:

Select T1.TicketNo,T1.Audited_Count,T2.NonAudited_Count
FROM
(
Select [TicketNo], COUNT([TicketNo]) AS Audited_Count
from [Resolved Results$] where [Audit Result] = 'Audited'
group by TicketNo
) T1
inner JOIN
(
Select [TicketNo], COUNT([TicketNo]) AS NonAudited_Count
from [Resolved Results$] where [Audit Result] <> 'Audited'
group by TicketNo
) T2
on T1.[TicketNo] = T2.[TicketNo]

但是上述命令不适用于那些仅具有审核规则或仅未经审核规则的工单,而仅适用于同时具有这两种规则的工单。

有人可以帮我解决这个问题吗?

请注意:只有内部联接适用于 OleDB,因此请避免使用内部联接以外的任何内容。非常感谢

编辑:戈登的第一个答案应该理想地工作,但不幸的是 Excel 的 OLEDB 驱动程序不支持它。然而,戈登使用 iff(..) 的建议奏效了。

我使用的最终查询是:

select [TicketNo],
sum(iif([Audit Result] = "AUDITED", 1, 0)) as Audited,
sum(iif([Audit Result] = "NOT AUDITED", 1, 0)) as NotAudited
from [Resolved Results$]
group by [TicketNo];

谢谢戈登!

最佳答案

您需要条件求和,可以通过将 case 语句放入 sum() 中来实现:

select ticket,
sum(case when AuditResult = 'Audited' then 1 else 0 end) as Audited,
sum(case when AuditResult = 'Not Audited' then 1 else 0 end) as NotAudited
from t
group by ticket;

这是假设 AuditResult 是一个单独的列。如果它是 SomeStrings 上的最终注释,那么查询将如下所示:

select ticket,
sum(case when SomeStrings like '%Audited' and SomeStrings not like '%Not Audited' then 1
else 0
end) as Audited,
sum(case when SomeStrings like '%Not Audited' then 1
else 0
end) as NotAudited
from t
group by ticket;

关于c# - SQL 查询 : Select Column1, 行数 (criteria1),行数 (criteria2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574376/

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