连接数据行以将总计添加到一起

时间:2017-01-19 01:09:08

标签: sql oracle

从我的查询中返回的数据是将1天的时间分成2行(其中包括午餐扣除额,用于划分)。

如何连接总计以便它们出现在1行?

也许我需要让startdtm和enddtm全部显示在1行而不是分成2个不同的行,这样整个列的总量将是一整天?

我不知道该怎么做 - 抱歉格式可能不正确,我随身携带自学。

select 
    a.employeeid
    , b.personfullname
    , c.payrulename
    , to_char(a.startdtm, 'mm-dd-yyyy')
    , to_char(a.startdtm, 'hh24:mi')
    , to_char(a.enddtm, 'hh24:mi')
    , ((a.enddtm - artdtm)*24*60/60) "TOTAL"        
from
    timesheetitem a
    , vp_timesheetitmv42 b
    , vp_employeev42 c
where
    a.startdtm = b.startdtm
    and a.employeeid = c.employeeid
    and a.employeeid = b.employeeid
    and payrulename = CA Ex

结果

EEID    PERSON NAME     Payrule Eff Date    InPunch OutPunch    TOTAL
14065   Escobar, Test   CA Ex   7/1/2013    6:15    12:00        5.75
14065   Escobar, Test   CA Ex   7/1/2013    13:00   14:45        1.75
14065   Escobar, Test   CA Ex   7/2/2013    6:30    11:30        5
14065   Escobar, Test   CA Ex   7/2/2013    12:30   15:00        2.5

2 个答案:

答案 0 :(得分:0)

这可能是你的想法:

select a.employeeid as EEID,
       b.personfullname as "PERSON NAME",
       c.payrulename as Payrule,
       to_char(a.startdtm, 'mm-dd-yyyy') as "EFF DATE",
       SUM((a.enddtm - artdtm)*24*60/60) as "TOTAL"        
from timesheetitem a
inner join vp_timesheetitmv42 b
    on a.startdtm = b.startdtm and
       a.employeeid = b.employeeid
inner join vp_employeev42 c
    on a.employeeid = c.employeeid
where payrulename = 'CA Ex'
group by a.employeeid,
         b.personfullname,
         c.payrulename,
         to_char(a.startdtm, 'mm-dd-yyyy')

答案 1 :(得分:0)

而不是在评论中全部完成 - 这是对"协议"的回答。至今。这可能是你需要的方向。

最深嵌套的子查询为每个" in-out"附加一个数字。对(固定员工和日历日);它可能是数字1,在某些情况下它可能是2.然后我使用它来创建单行摘要,显示进出时间(在同一天只有一次或有时两次)和总时数那天。其余的是简单的连接 - 使用ANSI标准语法;您可以根据需要调整联接(我记下了我对此感到困惑的一点 - 第二个表中第一个AND中startdtmenddtm的存在。)

我"发明了"另外一名员工展示如何编写这篇文章以容纳多名员工,并在一天内只有一个及时和一个外出时显示结果。

with
     timesheetitem ( employeeid, startdtm, enddtm ) as (
       select 14065, to_date('7/1/2013 6:15' , 'mm/dd/yyyy hh24:mi')
                   , to_date('7/1/2013 12:00', 'mm/dd/yyyy hh24:mi') from dual union all
       select 14065, to_date('7/1/2013 13:00', 'mm/dd/yyyy hh24:mi')
                   , to_date('7/1/2013 14:45', 'mm/dd/yyyy hh24:mi') from dual union all
       select 14065, to_date('7/2/2013 6:30' , 'mm/dd/yyyy hh24:mi')
                   , to_date('7/2/2013 11:30', 'mm/dd/yyyy hh24:mi') from dual union all
       select 14065, to_date('7/2/2013 12:30', 'mm/dd/yyyy hh24:mi')
                   , to_date('7/2/2013 15:00', 'mm/dd/yyyy hh24:mi') from dual union all
       select 14068, to_date('7/2/2013 6:15' , 'mm/dd/yyyy hh24:mi')
                   , to_date('7/2/2013 13:45', 'mm/dd/yyyy hh24:mi') from dual
     ),
     vp_timesheetitmv42 ( employeeid, personfullname ) as (
       select 14065, 'Escobar, Test' from dual union all
       select 14068, 'Oracle, Maria' from dual
     ),
     vp_employeev42 ( employeeid, payrulename ) as (
       select 14065, 'CA Ex' from dual union all
       select 14068, 'CA Ex' from dual
     )
--  end of simulated data; SQL query begins BELOW THIS LINE
select a.employeeid, b.personfullname, c.payrulename,
       a.eff_date, a.in_1, a.out_1, a.in_2, a.out_2, a.total as tot_hours
from   (
         select employeeid, to_char(startdtm, 'mm/dd/yyyy') as eff_date,
                to_char(min(startdtm), 'hh24:mi') as in_1, 
                to_char(min(enddtm)  , 'hh24:mi') as out_1,
                to_char(min(case rn when 2 then startdtm end), 'hh24:mi') as in_2,
                to_char(min(case rn when 2 then enddtm   end), 'hh24:mi') as out_2,
                24 * sum(enddtm - startdtm) as total
         from   (
                  select employeeid, startdtm, enddtm, 
                         row_number() over (partition by employeeid, trunc(startdtm) 
                                            order by startdtm) as rn
                  from   timesheetitem
                )
         group by employeeid, to_char(startdtm, 'mm/dd/yyyy')
       ) a
       join vp_timesheetitmv42 b on a.employeeid = b.employeeid
                                        --    and some condition on startdtm??
       join vp_employeev42     c on a.employeeid = c.employeeid
where  c.payrulename = 'CA Ex'
order by employeeid, eff_date, in_1     --  if needed
;

<强>输出

EMPLOYEEID PERSONFULLNAME PAYRULENAME EFF_DATE   IN_1  OUT_1 IN_2  OUT_2  TOT_HOURS
---------- -------------- ----------- ---------- ----- ----- ----- ----- ----------
     14065 Escobar, Test  CA Ex       07/01/2013 06:15 12:00 13:00 14:45        7.5
     14065 Escobar, Test  CA Ex       07/02/2013 06:30 11:30 12:30 15:00        7.5
     14068 Oracle, Maria  CA Ex       07/02/2013 06:15 13:45                    7.5

3 rows selected.