20 พฤษภาคม 2568

SQL group by multiple columns and distinct

datetime order_id
01 Jan 2020 order1
01 Jan 2020 order1
04 Jan 2020 order1
02 Jan 2020 order2
02 Jan 2020 order2
03 Jan 2020 order3
04 Jan 2020 order4
05 Jan 2020 order4


From table above, if you want to see how many duplicate rows (order_id and distinct datetime).

You can query data using this SQL command

SELECT order_id, COUNT(DISTINCT(datetime))
FROM table
GROUP BY order_id
HAVING COUNT(DISTINCT(datetime)) > 1

You will get this result


datetime order_id
01 Jan 2020 order1
04 Jan 2020 order1
02 Jan 2020 order2
03 Jan 2020 order3
04 Jan 2020 order4
05 Jan 2020 order4

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

บทความยอดนิยม (ล่าสุด)

บทความยอดนิยม (1 ปีย้อนหลัง)