0

我有一张如下表(MSSql)

Name  Status
----  -------
kmk   Success
kmk   Success
mvk   Fail
per   Success
Per   Fail
mvk   Fail

我需要给出如下结果的查询

Name Status  Count
---- ------- -----
kmk  Success 2
kmk  Fail    0
mvk  Success 0
mvk  Fail    2
Per  Success 1
Per  Fail    1

我试过了,group by但我得到了如下结果我的查询是

(Select Name,[Status],COUNT([Status]) As Count From Checking group by [Status],Name)

Name Status  Count
---- ------- -----
kmk  Success 2
mvk  Fail    2
Per  Success 1
Per  Fail    1

我尝试了很多方法,请您给我查询

4

1 回答 1

2

您可以尝试以下方法。它适用于 sqlite3,并且根据您的 SQL 引擎,您的里程可能会有所不同:

select
  name,
  "Success" as status,
  sum(case when status = "Success" then 1 else 0 end) as count
from checking group by name
union
select
  name,
  "Fail" as status,
  sum (case when status = "Fail" then 1 else 0 end) as count
from checking group by name;

SQLite3 的结果:

kmk|Fail|0
kmk|Success|2
mvk|Fail|2
mvk|Success|0
per|Fail|1
per|Success|1
于 2012-07-31T21:16:25.417 回答