我有以下查询,我需要根据hour_interval合并两列Total_here和Total_there。我已经设法通过使用UNION运行两个查询,但基本上每小时创建两行(一行为'Total new',一行为'Total_done')。当我尝试将这两行转换为两列时,我遇到了问题。
SELECT
hour_interval as hours
FROM
SELECT
DATEPART(hour, person) as hour_interval,
COUNT(person) AS Total_person
FROM
(SELECT
CASE
WHEN DATEPART(hour, x.person) > 12
THEN DATEADD (MINUTE, -1 * DATEPART(minute, x.person) - 1, x.person)
WHEN DATEPART(hour, x.person) < 4
THEN DATEADD (MINUTE, 55, x.person)
ELSE x.person
END AS person
FROM
new_table) f2
UNION
SELECT DATEPART( hour , done_person ) as hour_interval_1, COUNT(done_person) AS Total_done_person FROM
(
SELECT
case when DATEPART ( hour , x.done_person ) > 12 then DATEADD (MINUTE,-1 * DATEPART ( minute , x.done_person )-1,x.done_person)
when DATEPART ( hour , x.done_person ) < 4 then DATEADD (MINUTE, 55,x.done_person)
else x.done_person end as done_person
FROM done_table
) f3
group by hours