在 sql 查询中使用聚合函数时避免按子句分组
avoiding group by clause while using aggregate function in sql query
我有这个 SQL 查询,我在其中使用聚合函数,但我不想使用 group by
子句,因为它会弄乱我的数据。这是我必须为一个聚合函数使用大量 group by
的第一个查询。
select ti.task_name,cast(ti.start_date As VARCHAR),cast(th.created_date As VARCHAR),sum(th.previous_completed) as total,
ld.level_data,pi.project_code_1,pi.project_code_2
from task_history th
left join task_information ti on ti.id =th.task_id
left join project_information pi on ti.project_id = pi.id
left join lookup_data ld on ti.activity=ld.id
where cast((th.created_date)as date) between '2021-06-28' and '2021-06-29' and ti.employee_id='092cdd13-5f30-4980-93d0-8246239728fd'
group by ti.employee_id,ti.task_name,ti.start_date,ld.level_data,th.created_date,pi.project_code_1,pi.project_code_2
我得到了这个结果:
attend hotline calls | 2021-06-28 | 2021-06-28 05:22:03.310768 | 0 | Sales monitoring
| |
attend hotline calls | 2021-06-28 | 2021-06-28 16:38:42.676874 | 3 | Sales monitoring
它应该合计为 1 列。
这是我避免使用所有列名以避免重复结果的查询:
select ti.task_name,sum(th.previous_completed) as total
from task_history th
left join task_information ti on ti.id =th.task_id
left join project_information pi on ti.project_id = pi.id
left join lookup_data ld on ti.activity=ld.id
where cast((th.created_date)as date) between '2021-06-28' and '2021-06-29' and ti.employee_id='092cdd13-5f30-4980-93d0-8246239728fd'
group by ti.task_name
我得到这个结果:
attend hotline calls | 3
第一个结果显示了一个额外的列,第二个结果符合预期,但我还想包括第一个结果的列。
我怎样才能做到这一点?
你可以使用window函数:
select ti.activity, ld.level_data,
sum(th.previous_completed) over (partition by ti.activity,ld.level_data) as total
from task_history th left join
task_information ti
on ti.id =th.task_id left join
lookup_data ld
on ti.activity=ld.id
您的主要问题是 th.created_date
是您用作 GROUP BY
列之一的时间戳。每条记录的时间不同,因此该列没有分组。
我不确定为什么您需要将日期转换为字符变化,但无论如何,如果您首先将日期转换为日期(例如 th.created_date::DATE),那么您 select 和 GROUP BY
。然后它会给你每个日期的总和(而不是每个微秒)。
我有这个 SQL 查询,我在其中使用聚合函数,但我不想使用 group by
子句,因为它会弄乱我的数据。这是我必须为一个聚合函数使用大量 group by
的第一个查询。
select ti.task_name,cast(ti.start_date As VARCHAR),cast(th.created_date As VARCHAR),sum(th.previous_completed) as total,
ld.level_data,pi.project_code_1,pi.project_code_2
from task_history th
left join task_information ti on ti.id =th.task_id
left join project_information pi on ti.project_id = pi.id
left join lookup_data ld on ti.activity=ld.id
where cast((th.created_date)as date) between '2021-06-28' and '2021-06-29' and ti.employee_id='092cdd13-5f30-4980-93d0-8246239728fd'
group by ti.employee_id,ti.task_name,ti.start_date,ld.level_data,th.created_date,pi.project_code_1,pi.project_code_2
我得到了这个结果:
attend hotline calls | 2021-06-28 | 2021-06-28 05:22:03.310768 | 0 | Sales monitoring
| |
attend hotline calls | 2021-06-28 | 2021-06-28 16:38:42.676874 | 3 | Sales monitoring
它应该合计为 1 列。
这是我避免使用所有列名以避免重复结果的查询:
select ti.task_name,sum(th.previous_completed) as total
from task_history th
left join task_information ti on ti.id =th.task_id
left join project_information pi on ti.project_id = pi.id
left join lookup_data ld on ti.activity=ld.id
where cast((th.created_date)as date) between '2021-06-28' and '2021-06-29' and ti.employee_id='092cdd13-5f30-4980-93d0-8246239728fd'
group by ti.task_name
我得到这个结果:
attend hotline calls | 3
第一个结果显示了一个额外的列,第二个结果符合预期,但我还想包括第一个结果的列。
我怎样才能做到这一点?
你可以使用window函数:
select ti.activity, ld.level_data,
sum(th.previous_completed) over (partition by ti.activity,ld.level_data) as total
from task_history th left join
task_information ti
on ti.id =th.task_id left join
lookup_data ld
on ti.activity=ld.id
您的主要问题是 th.created_date
是您用作 GROUP BY
列之一的时间戳。每条记录的时间不同,因此该列没有分组。
我不确定为什么您需要将日期转换为字符变化,但无论如何,如果您首先将日期转换为日期(例如 th.created_date::DATE),那么您 select 和 GROUP BY
。然后它会给你每个日期的总和(而不是每个微秒)。