2

在此处输入图像描述

我需要按描述分组,所以我一周中的所有日子都在同一行中,例如“不合理”必须在一行中显示一次,但是当我使用 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)
4

1 回答 1

2

您需要切换求和的位置,以免重复整个查询并尝试使其在没有大量滚动的情况下可见,我将用一列进行演示:

请注意,我还将您的表达式从case when .. else case when ... else多个 whens 更改为单个 case 表达式。

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.ScheduledWork和删除。dbo.ta_dayofweek(ta_timecard.schedindate)

本质上,您将所需的东西作为条件放置,但不是在 sum 语句中的 group by,因此它们不需要包含在 group by 中。

于 2013-05-03T11:01:31.743 回答