我有这个函数返回下面的结果,但是如何删除重复的行?一个小组将会这样做但是如何将它构建到我的函数中?然后还使用' H'字段。
SELECT
ACTION.holiday_id,
ACTION.id,
--H.holiday_name,
--H.date_start,
--H.date_end,
(SELECT Count(DISTINCT ACTION.holiday_id) FROM ACTION) as 'Count'
FROM
ACTION
--INNER JOIN HOLIDAY_REF H ON ACTION.holiday_id=H.holiday_id
WHERE ACTION.delete_date is NULL
--GROUP BY ACTION.holiday_id
holiday_id id Count
31 5 3
31 6 3
31 8 3
332 7 1
所以它看起来像:
31 3
332 1
答案 0 :(得分:1)
这应该有用。
with cte as
(
select holiday_id,
id,
holiday_name,
date_start,
date_end,
row_number() over (partition by holiday_id order by holiday_id) as [Index]
from Action
)
select holiday_id, id, holiday_name, date_start, date_end,
(select count(*) from Action
where holiday_id = cte.holiday_id and delete_date is null) as [Count]
from cte
where [Index] = 1
编辑:同时检查delete_date
答案 1 :(得分:0)
你有没有尝试过:
SELECT
ACTION.holiday_id,
Count(*) [Count]
FROM ACTION
WHERE ACTION.delete_date is NULL
GROUP BY ACTION.holiday_id