有此查询
"SELECT DISTINCT ID, DATE FROM MAIN_TAB ORDER BY ID ASC"
如何在每行的前面打印ID
字段的不同组合的总数,例如TOTAL_DISTINCT_VALUES
:
ID DATE TOTAL_DISTINCT_VALUES
37870 02/07/2018 3
37870 03/07/2018 3
37870 04/07/2018 3
55887 04/07/2018 2
55887 03/07/2018 2
61891 02/07/2018 1
44891 02/07/2018 1
75891 02/07/2018 1
答案 0 :(得分:1)
您可以使用count和group by
SELECT ID, DATE, count(*) TOTAL_DISINCT_VALUE
FROM MAIN_TAB
GROUP BY ID, DATE
ORDER BY ID ASC
但是向您看数据样本似乎只需要将id续,因此您可以对id为的计数组使用联接
select t.id, a.date, t.TOTAL_DISTINCT_VALUE
from MAIN_TAB a
inner JOIN (
select id, count(*) TOTAL_DISTINCT_VALUE
FROM MAIN_TAB
group by ID
) t on a.id = t.id
或barmar建议。计数(不明确的日期)
select t.id, a.date, t.TOTAL_DISTINCT_VALUE
from MAIN_TAB a
inner JOIN (
select id, count(distinct date) TOTAL_DISTINCT_VALUE
FROM MAIN_TAB
group by ID
) t on a.id = t.id
答案 1 :(得分:0)
使用group by
子句
SELECT ID, DATE,count(*) as distinct_value FROM MAIN_TAB
group by ID, DATE
ORDER BY ID ASC
答案 2 :(得分:-1)
尝试一下:
select ID,DATE,count(*) as TOTAL_DISINCT_VALUE from MAIN_TAB group by ID,DATE order by ID asc;
,并将*替换为ID和DATE以外的任何其他列名称。