SQLite - 按多列分组,统计为一列

时间:2016-05-09 22:53:02

标签: sqlite count group-by distinct

说我有下表:

Commit File    Past_Author
1       a      Alice
1       a      Bob
1       a      Bob
1       b      Alice

我希望计算每个文件的不同作者的数量,总结为提交。在这种情况下,我想要3个作者用于提交1(2个用于文件a + 1用于文件b)。

我猜是这样的:SELECT Commit, SUM(NUM_AUTHORS) FROM (SELECT Commit, File, COUNT(DISTINT Past_Author) as NA FROM COMMITS GROUP BY File) GROUP BY Commit

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题。

SELECT Commit, SUM(NUM_AUTHORS) FROM
(
   SELECT 
      Commit, 
      File,
      COUNT(DISTINCT Past_Author) AS NUM_AUTHORS
   FROM COMMITS
   GROUP BY Commit, File
) AS AUTHOR_COUNT
GROUP BY Commit;