Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Count idUser with different creationDate

Tags:

sql

I have this table

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

I would like to count the number of ID which have 2 or more different creationDate. In my example, I would like to have 2 (ID 2 and 6 have different creationDate).

I tried

SELECT COUNT(ID) FROM table
GROUP BY ID
HAVING count(DISTINCT creationDate)>1
like image 330
Alex C Avatar asked Feb 07 '26 17:02

Alex C


2 Answers

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

If using an RDBMS that supports SUM() OVER() you could use:

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

Demo: SQL Fiddle

like image 169
Hart CO Avatar answered Feb 09 '26 12:02

Hart CO


try this:

SELECT COUNT(ID)
FROM (
    SELECT COUNT(ID) AS IDCnt, ID
    FROM table
    GROUP BY ID
    HAVING count(ID)>1
    ) a
like image 32
Luis LL Avatar answered Feb 09 '26 12:02

Luis LL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!