3

我有这张桌子

ID / creationDate
2  / 2012-06-28 04:58:27
2  / 2012-07-10 14:26:51
2  / 2013-04-18 00:35:36
3  / 2012-07-02 16:47:00
4  / 2012-07-02 20:24:22
5  / 2012-07-03 00:32:17
6  / 2012-07-05 04:40:02
6  / 2012-07-06 18:17:08
6  / 2013-04-28 22:28:54

我想计算ID其中有 2 个或更多不同的数量creationDate。在我的示例中,我希望有 2 个(ID2 和 6 不同creationDate)。

我试过了

SELECT COUNT(ID) FROM table
GROUP BY ID
HAVING count(DISTINCT creationDate)>1
4

2 回答 2

2
SELECT COUNT(*)
FROM (SELECT ID,COUNT(DISTINCT creationDate)'CT'
      FROM Table
      GROUP BY ID
      HAVING COUNT(DISTINCT creationDate) > 1
     )sub

如果使用支持SUM() OVER()您的 RDBMS,您可以使用:

SELECT TOP 1 SUM(COUNT(DISTINCT ID))OVER()
FROM Table1
GROUP BY ID
HAVING COUNT(DISTINCT creationDate) > 1

演示:SQL 小提琴

于 2013-08-05T17:02:04.423 回答
1

尝试这个:

SELECT COUNT(ID)
FROM (
    SELECT COUNT(ID) AS IDCnt, ID
    FROM table
    GROUP BY ID
    HAVING count(ID)>1
    ) a
于 2013-08-05T17:01:06.177 回答