I have a table which has some duplicate values for the same date
MyTime | Mrc | Num
21-Dec | ABC | 200
21-Dec | ABC | 200
20-Dec | DEF | 300
20-Dec | DEF | 300
19-Dec | XYZ | 254
21-Nov | ABC | 200
I want to ignore the duplicate entries and group by the DATE(Mytime), Mrc, Num
ie I want to detect entries which have same day of the month, Mrc & Num appearing more than once but are not exact duplicate
So the expected output is:
MyTime | Mrc | Num | cnt
21 | ABC | 200 | 2
I tried something along the following lines:
SELECT DISTINCT(MyTime, Mrc, Num, Count(*) as cnt)
FROM `My_Table`
WHERE MType != 'XX'
AND HType != 'YY'
GROUP BY Date(MyTime), Mrc, Num
ORDER BY MyTime DESC
But it is not working.
Use COUNT(DISTINCT MyTime)
:
SELECT
DAY(MyTime) as MyTime
Mrc,
Num,
COUNT(DISTINCT MyTime) as cnt
FROM `My_Table`
WHERE MType != 'XX'
AND HType != 'YY'
GROUP BY 1, 2, 3
ORDER BY 1 DESC
Notes:
DAY()
function, not the DATE()
function to extract the day part of the date DISTINCT
is a keyword , not a function, so avoid using brackets ie use it like this: SELECT DISTINCT column1, column2, ...
COUNT(DISTINCT expression)
returns the number of different values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.