组行在表中

时间:2013-05-03 10:51:10

标签: sql sql-server

enter image description here

我需要按说明进行分组,以便我在一周中的所有日子都进入同一行 例如' Unjustified'必须在一行中显示一次 但是当我使用Group By语句时,我没有让它们连接起来 有人可以帮我这个..谢谢你

select Emp.ID, 
sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork as SchedWork,

case when absence.description is null then case when absence1.description is null then 'Unjustified' else absence1.description end else absence.description end as description,

case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Mon' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end,
case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Tue' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end,
case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Wed' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end,
case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Thu' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end,
case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Fri' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end,
case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Sat' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end,
case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Sun' then sum(case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 then ta_timecard.scheduledwork else case when ta_timecardlinks.fromtimer = ta_timecard.schedintime then ta_timecardlinks.effdurationr + ta_timecard.rndminin else case when ta_timecardlinks.totimer = ta_timecard.schedouttime then ta_timecardlinks.effdurationr - ta_timecard.rndminout else ta_timecardlinks.effdurationr end end end) / ta_timecard.scheduledwork end

from ta_timecarddet
inner join ta_timecard on ta_timecarddet.timecardid = ta_timecard.id
inner join emp on ta_timecard.empid = emp.id
inner join ta_schedulehist on ta_timecard.schedhistid = ta_schedulehist.id
left outer join ta_timecardlinks on ta_timecarddet.id = ta_timecardlinks.tctrsid
left outer join absence on ta_timecard.leavetype = absence.id
left outer join absence absence1 on ta_timecardlinks.leavetype = absence1.id

where  ta_timecarddet.trstype in (1, 5, 6, 11)
and ta_timecard.worktype in (1, 4, 5, 8)

Group BY absence.description, ta_timecard.ScheduledWork, Emp.ID,absence1.description, dbo.ta_dayofweek(ta_timecard.schedindate)

1 个答案:

答案 0 :(得分:2)

为了不重复整个查询并尝试在不加载滚动的情况下使其显示,我需要切换你所做的总和,我将用一个列进行演示:

请注意,我还将您的表达式从case when .. else case when ... else更改为包含多个小时的单个案例表达式。

sum(case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Mon' then 
            case when ta_timecarddet.trstype = 6 or ta_timecarddet.trstype = 11 
                    then ta_timecard.scheduledwork 

                when ta_timecardlinks.fromtimer = ta_timecard.schedintime 
                    then ta_timecardlinks.effdurationr + ta_timecard.rndminin 

                when ta_timecardlinks.totimer = ta_timecard.schedouttime 
                    then ta_timecardlinks.effdurationr - ta_timecard.rndminout 

                else ta_timecardlinks.effdurationr 
            end 
        else 0 end)  / 
    sum(case when dbo.ta_dayofweek(ta_timecard.schedindate) = 'Mon' 
                then ta_timecard.scheduledwork
            else 0
        end),

您还需要从群组中删除ta_timecard.ScheduledWorkdbo.ta_dayofweek(ta_timecard.schedindate)

基本上,您将所需的内容作为条件放置,但不是在内部 sum语句中放置在组中,因此它们不需要包含在组中。