COUNT(CASE WHEN)SQL中的重复行

时间:2015-08-13 19:39:49

标签: mysql sql-server duplicates rows multiple-columns

初学SQL用户在这里。

我需要我的桌子看起来像这样:

ModuleID | Draft | Proposed | Committed | Total
---------------------------------------------
A        |    1  |    0     |     1     | 2

目前看起来像这样:

ModuleID | Draft | Proposed | Committed | Total
---------------------------------------------
A        |    1  |    0     |     1     | 1
A        |    0  |    0     |     1     | 1

以下是代码:

SELECT T3.NAME AS ModuleID, 
   COUNT(CASE WHEN T4.LITERAL_NAME = 'Draft' THEN 1 END) AS Draft,
   COUNT(CASE WHEN T4.LITERAL_NAME = 'Proposed' THEN 1 END) AS Proposed,
   COUNT(CASE WHEN T4.LITERAL_NAME = 'Committed' THEN 1 END) AS Committed,
   SUM(1) AS Total

FROM ...
WHERE ...

GROUP BY T4.LITERAL_NAME,T3.NAME

所以它提取了正确的信息,但它每列重复模块ID。我已经仔细检查了主板和谷歌,无法弄清楚是什么问题。请帮忙!

1 个答案:

答案 0 :(得分:1)

GROUP BY T4.LITERAL_NAME,T3.NAME

更改为

GROUP BY T3.NAME