我必须按日期对以下查询进行分组,但是REG和OT时间在同一日期的不同行上。谁能告诉我如何实现这一目标?
我按日期分组了,但是没有用。
SELECT VP.PERSONNUM AS PERSONID,
VP.FIRSTNM AS FIRST_NAME,
VP.LASTNM AS LAST_NAME,
(select personnum from vp_employeev42 where personnum = vp.homelaborlevelnm6) as FOREMAN_CODE,
CA.PERSONCSTMDATATXT as SHIFT,
TO_CHAR(X.APPLYDTM, 'DD-MM-YYYY') AS LABOR_DATE,
VP.HOMELABORLEVELNM3 AS DEPARTMENT,
CASE WHEN TS.LABORLEVELNAME3 != 'NULL' THEN TS.LABORLEVELNAME3 ELSE VP.HOMELABORLEVELNM3 END AS CHGD_DEPARTMENT,
CASE WHEN P.NAME = 'US-Regular' THEN SUM(X.DURATIONSECSQTY/3600.0) ELSE 0 END AS REG,
CASE WHEN P.NAME = 'US-Overtime' THEN SUM(X.DURATIONSECSQTY/3600.0) ELSE 0 END AS OT,
CASE WHEN P.NAME = 'US-Doubletime' THEN SUM(X.DURATIONSECSQTY/3600.0) ELSE 0 END AS DBL ,
CASE WHEN TS.LABORLEVELNAME4 != 'NULL' THEN TS.LABORLEVELNAME4 ELSE VP.HOMELABORLEVELNM4 END AS ACCOUNT_ID
FROM VP_EMPLOYEEV42 VP,
WFCTOTAL X, PAYCODE P,
PERSONCSTMDATA CA,
VP_TIMESHTPUNCHV42 TS
WHERE VP.PERSONID = X.EMPLOYEEID AND
X.PAYCODEID = P.PAYCODEID AND
X.EMPLOYEEID = CA.PERSONID AND
CA.CUSTOMDATADEFID ='13' AND
X.TIMESHEETITEMID = TS.TIMESHEETITEMID AND
P.NAME in ('US-Regular','US-Overtime','US-Doubletime') AND
X.EMPLOYEEID in (select personid from PERSONCSTMDATA where CUSTOMDATADEFID ='14' and PERSONCSTMDATATXT = 'USKEANE')
and VP.PAYRULENAME in ('USKEANE 10 HR Shift','USKEANE 12 HR Shift','USKEANE 1st Shift','USKEANE 2nd Shift','USKEANE 3rd Shift')
and X.APPLYDTM between '01-APR-19' and '30-APR-19'
group by VP.PERSONID, VP.PERSONNUM, VP.FIRSTNM, VP.LASTNM, VP.HOMELABORLEVELNM6,
X.APPLYDTM, VP.HOMELABORLEVELNM3, CA.PERSONCSTMDATATXT ,VP.HOMELABORLEVELNM4,
P.NAME, TS.LABORLEVELNAME3, TS.LABORLEVELNAME4 ORDER BY VP.PERSONID, X.APPLYDTM DESC
{
PERSONID FIRST_NAME LAST_NAME FOREMAN_CODE SHIFT LABOR_DATE DEPARTMENT CHGD_DEPARTMENT REG OT DBL ACCOUNT_ID
E0002887 Maria Putnam E0061775 1 10-04-2019 25855 25855 0 4 0 60467
E0002887 Maria Putnam E0061775 1 10-04-2019 25855 25855 4 0 0 60467
E0002887 Maria Putnam E0061775 1 15-04-2019 25855 25855 8 0 0 60467
}
答案 0 :(得分:1)
您正在这样做:
select case p.name when 'US-Regular' then sum(x.durationsecsqty/3600) else 0 end as reg,
case p.name when 'US-Overtime' then sum(x.durationsecsqty/3600) else 0 end as ot,
case p.name when 'US-Doubletime' then sum(x.durationsecsqty/3600) else 0 end as dbl
from p, x
where x.paycodeid = p.paycodeid
group by p.name
但是您不想在输出中区分p.name
。因此,将其从group by
中删除,然后像下面这样更改case
部分:
select sum(case p.name when 'US-Regular' then x.durationsecsqty/3600 else 0 end) as reg,
sum(case p.name when 'US-Overtime' then x.durationsecsqty/3600 else 0 end) as ot,
sum(case p.name when 'US-Doubletime' then x.durationsecsqty/3600 else 0 end) as dbl
from p, x
where x.paycodeid = p.paycodeid
如果我是你,我还将旧连接语法更改为更具可读性:
...
from paycode p
join wfctotal x on x.paycodeid = p.paycodeid
join personcstmdata ca on x.employeeid = ca.personid
...
where p.name in ('US-Regular','US-Overtime','US-Doubletime')
...
尽管有些人更喜欢旧的方式:)