下面是我的SQL查询。我需要更改它,以便可以按wkld_date
的总和获得所有工作量。当我给出条件ssawd.wkld_date = 201803
时,此代码可以完美工作,但我希望它返回按wkld_date
分组的工作量总和。请帮忙。非常感谢。
select
ISNULL(SUM(ssawd.WORKLOAD), 0) w3
from
AGMT_MAN_SCH_SS ssams
inner join
AGMT_WKLD_SUMMARY_SS ssaws on ssams.SCHEDULE_ID = ssaws.SCHEDULE_ID
and ssams.SNAP_DATE = ssaws.SNAP_DATE
inner join
AGMT_WKLD_DETAIL_SS ssawd on ssaws.summary_id = ssawd.summary_id
and ssaws.SNAP_DATE = ssawd.SNAP_DATE
inner join
AGREEMENT ag on ssams.agmt_id = ag.AGMT_ID
where
ssaws.STAFF_ID = 4331
and ssawd.wkld_date = 201803
and ((ag.AGMT_ACTCPLTDATE is not null
and ag.AGMT_ACTCPLTDATE > '2018-01-01')
or ag.AGMT_ACTCPLTDATE is null)
and ((ag.AGMT_TERMDATE is not null
and ag.AGMT_TERMDATE > '2018-01-01')
or ag.AGMT_TERMDATE is null)
and dateadd(year, 10, ag.AGMT_ACTCOMMENTDATE) > '2018-01-01'
and UPPER(ssaws.POST_TYPE) NOT LIKE UPPER('Unnamed')
and ssams.agmt_id != 140
and (ssams.INTM_SNAP_DATE in (select max(INTM_SNAP_DATE)
from AGMT_MAN_SCH ams
where ams.AGMT_ID = ag.AGMT_ID
and ams.SNAP_TYPE = 'I')
or ssams.LATEST_SNAP_DATE in (select max(LATEST_SNAP_DATE)
from AGMT_MAN_SCH ams
where ams.AGMT_ID = ag.AGMT_ID
and ams.SNAP_TYPE = 'F')
)
答案 0 :(得分:2)
听起来像您想要一个简单的GROUP BY语句。在您的情况下,它将如下所示:
SELECT ssawd.wkld_date, sum(ssawd.WORKLOAD) w3
from AGMT_MAN_SCH_SS ssams
inner join AGMT_WKLD_SUMMARY_SS ssaws on ssams.SCHEDULE_ID = ssaws.SCHEDULE_ID and ssams.SNAP_DATE = ssaws.SNAP_DATE
inner join AGMT_WKLD_DETAIL_SS ssawd on ssaws.summary_id = ssawd.summary_id and ssaws.SNAP_DATE = ssawd.SNAP_DATE
inner join AGREEMENT ag on ssams.agmt_id = ag.AGMT_ID
where ssaws.STAFF_ID = 4331
and ((ag.AGMT_ACTCPLTDATE is not null and ag.AGMT_ACTCPLTDATE > '2018-01-01') or ag.AGMT_ACTCPLTDATE is null)
and ((ag.AGMT_TERMDATE is not null and ag.AGMT_TERMDATE > '2018-01-01') or ag.AGMT_TERMDATE is null)
and dateadd(year, 10, ag.AGMT_ACTCOMMENTDATE) > '2018-01-01'
and UPPER(ssaws.POST_TYPE) NOT LIKE UPPER('Unnamed')
and ssams.agmt_id != 140
and (ssams.INTM_SNAP_DATE in
(select max(INTM_SNAP_DATE) from AGMT_MAN_SCH ams where ams.AGMT_ID = ag.AGMT_ID and ams.SNAP_TYPE = 'I' )
or ssams.LATEST_SNAP_DATE in
(select max(LATEST_SNAP_DATE) from AGMT_MAN_SCH ams where ams.AGMT_ID = ag.AGMT_ID and ams.SNAP_TYPE = 'F'))
GROUP BY ssawd.wkld_date
请注意,我已经从WHERE语句中删除了ssawd.wkld_date,将其添加到返回的字段中,并在末尾在GROUP BY语句中使用了它。