使用 case 表达式时对行进行分组
Group rows when using case expresion
我有以下查询,它给了我下一个结果:
select OBJ_POSITION,
CASE
WHEN (EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_TMPL,
CASE
WHEN (EVT_JOBTYPE = 'CMS') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_SUST,
CASE
WHEN ((EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') or (EVT_JOBTYPE = 'CMS')) THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as TOTAL_HORAS
from R5ACTIVITIES inner join R5EVENTS on ACT_EVENT = EVT_CODE inner join R5OBJECTS on EVT_OBJECT = OBJ_CODE
where OBJ_ORG = '156' and OBJ_POSITION like '156-EBM%' and EVT_STATUS = 'R' and EVT_TYPE in ('JOB', 'PPM')
group by OBJ_POSITION, EVT_JOBTYPE, EVT_DESC
OBJ_POSITION / HORAS_TMPL / HORAS_SUST / TOTAL_HORAS
156-EBM01 0 0 0
156-EBM01 0 0 0
156-EBM01 0 0 0
156-EBM02 0 0 0
156-EBM02 0 0 0
156-EBM03 0 0 0
156-EBM04 0 0 0
156-EBM04 8 0 8
156-EBM04 0.5 0 0.5
156-EBM04 0.75 0 0.75
156-EBM04 0 1.5 1.5
156-EBM05 0 0 0
156-EBM05 0 0 0
156-EBM05 0 0 0
信息是正确的,但我只想要每个 OBJ_Position 一行,对其数字 (ACT_EST) 求和,如下所示:
OBJ_POSITION / HORAS_TMPL / HORAS_SUST / TOTAL_HORAS
156-EBM01 0 0 0
156-EBM02 0 0 0
156-EBM03 0 0 0
156-EBM04 9.25 1.5 10.75
156-EBM05 0 0 0
我尝试了很多分组依据,但 none 似乎有效。
我认为从 group by 中删除 EVT_JOBTYPE 可能会起作用,但随后会抛出错误,因为它不是聚合函数。与 EVT_DESC...
相同
这有帮助吗?
;with cte
As
(
select OBJ_POSITION,
CASE
WHEN (EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_TMPL,
CASE
WHEN (EVT_JOBTYPE = 'CMS') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_SUST,
CASE
WHEN ((EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') or (EVT_JOBTYPE = 'CMS')) THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as TOTAL_HORAS
from R5ACTIVITIES inner join R5EVENTS on ACT_EVENT = EVT_CODE inner join R5OBJECTS on EVT_OBJECT = OBJ_CODE
where OBJ_ORG = '156' and OBJ_POSITION like '156-EBM%' and EVT_STATUS = 'R' and EVT_TYPE in ('JOB', 'PPM')
group by OBJ_POSITION, EVT_JOBTYPE, EVT_DESC
)
Select OBJ_POSITION, SUM(HORAS_TMPL) as HORAS_TMPL,SUM(HORAS_SUST) as HORAS_SUST,SUM(TOTAL_HORAS) as TOTAL_HORAS
Group by OBJ_POSITION
我有以下查询,它给了我下一个结果:
select OBJ_POSITION,
CASE
WHEN (EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_TMPL,
CASE
WHEN (EVT_JOBTYPE = 'CMS') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_SUST,
CASE
WHEN ((EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') or (EVT_JOBTYPE = 'CMS')) THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as TOTAL_HORAS
from R5ACTIVITIES inner join R5EVENTS on ACT_EVENT = EVT_CODE inner join R5OBJECTS on EVT_OBJECT = OBJ_CODE
where OBJ_ORG = '156' and OBJ_POSITION like '156-EBM%' and EVT_STATUS = 'R' and EVT_TYPE in ('JOB', 'PPM')
group by OBJ_POSITION, EVT_JOBTYPE, EVT_DESC
OBJ_POSITION / HORAS_TMPL / HORAS_SUST / TOTAL_HORAS
156-EBM01 0 0 0
156-EBM01 0 0 0
156-EBM01 0 0 0
156-EBM02 0 0 0
156-EBM02 0 0 0
156-EBM03 0 0 0
156-EBM04 0 0 0
156-EBM04 8 0 8
156-EBM04 0.5 0 0.5
156-EBM04 0.75 0 0.75
156-EBM04 0 1.5 1.5
156-EBM05 0 0 0
156-EBM05 0 0 0
156-EBM05 0 0 0
信息是正确的,但我只想要每个 OBJ_Position 一行,对其数字 (ACT_EST) 求和,如下所示:
OBJ_POSITION / HORAS_TMPL / HORAS_SUST / TOTAL_HORAS
156-EBM01 0 0 0
156-EBM02 0 0 0
156-EBM03 0 0 0
156-EBM04 9.25 1.5 10.75
156-EBM05 0 0 0
我尝试了很多分组依据,但 none 似乎有效。 我认为从 group by 中删除 EVT_JOBTYPE 可能会起作用,但随后会抛出错误,因为它不是聚合函数。与 EVT_DESC...
相同这有帮助吗?
;with cte
As
(
select OBJ_POSITION,
CASE
WHEN (EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_TMPL,
CASE
WHEN (EVT_JOBTYPE = 'CMS') THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as HORAS_SUST,
CASE
WHEN ((EVT_JOBTYPE = 'PM' and EVT_DESC like '%TMPL%') or (EVT_JOBTYPE = 'CMS')) THEN (SUM(CONVERT(float, ACT_EST)))
ELSE '0'
END as TOTAL_HORAS
from R5ACTIVITIES inner join R5EVENTS on ACT_EVENT = EVT_CODE inner join R5OBJECTS on EVT_OBJECT = OBJ_CODE
where OBJ_ORG = '156' and OBJ_POSITION like '156-EBM%' and EVT_STATUS = 'R' and EVT_TYPE in ('JOB', 'PPM')
group by OBJ_POSITION, EVT_JOBTYPE, EVT_DESC
)
Select OBJ_POSITION, SUM(HORAS_TMPL) as HORAS_TMPL,SUM(HORAS_SUST) as HORAS_SUST,SUM(TOTAL_HORAS) as TOTAL_HORAS
Group by OBJ_POSITION