我有一个愚蠢的查询,它给了我下一个结果:
select *
from(
SELECT a.fisrt_name,a.last_name,a.job_id
FROM employees A
WHERE A.salary >(Select avg(salary) from employees) and a.salary < select (max(salary) from employees)
) b
where b.job_id=SY_ANA;
信息是正确的,但我只希望每个OBJ_Position一行,做它的数字总和(ACT_EST),就像这样:
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
我尝试了很多分组方式,但似乎都没有效果。 我认为从group by中删除EVT_JOBTYPE可能有效,但是随后抛出错误,因为它不是聚合函数。与EVT_DESC相同...
答案 0 :(得分:1)
这有帮助吗??
;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