如何在SQL中获得总结值

时间:2014-05-13 09:50:18

标签: sql sql-server

这是我目前的查询:

 SELECT dbo.TOL.sName AS sMacName, dbo.JOB.sName AS sJobName,
        dbo.SHT_SUM.tShtBegin AS tShiftBegin, dbo.SHT_SUM.tShtEnd AS tShiftEnd,
        SUM(dbo.SHT_SUM.dActShots) AS dShots, '450' AS dQuant,
        SUM(dbo.SHT_SUM.dActScrap) AS dScrap, MAX(dbo.JOB.iSetCavity) as SetCavity,
        CASE
            WHEN (dbo.SHT_SUM_SCR.iScrap) LIKE '99' THEN (dbo.SHT_SUM_SCR.dActScrap)
            ELSE '0'
        END as TOS
   FROM dbo.SHT_SUM_SCR
        RIGHT OUTER JOIN dbo.SHT_SUM ON dbo.SHT_SUM_SCR.lShtSumRef = dbo.SHT_SUM.lShtSumRef
         LEFT OUTER JOIN dbo.JOB ON dbo.SHT_SUM.lJobRef = dbo.JOB.lJobRef
        RIGHT OUTER JOIN dbo.TOL ON dbo.SHT_SUM.lMacRef = dbo.TOL.lTolRef
  WHERE dbo.TOL.bInactive = 0
    AND dbo.SHT_SUM.tShtDay > DATEADD(DAY, - 1, CONVERT(DATE, GETDATE()))
    AND GETDATE() BETWEEN dbo.SHT_SUM.tShtBegin AND dbo.SHT_SUM.tShtEnd
    AND iSetCavity is Not NUll
  GROUP BY dbo.TOL.sName, dbo.SHT_SUM.tShtBegin, dbo.SHT_SUM.tShtEnd, dbo.JOB.sName,
        dbo.SHT_SUM_SCR.iScrap, dbo.SHT_SUM_SCR.dActScrap
  ORDER BY sMacName

并且它当前返回:

sMacName    sJobName    tShiftBegin             tShiftEnd               dShots  dQuant  dScrap  SetCavity   TOS
FT2-E14     10401445710 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 98      450     2       2           0
FT2-E14     10401445710 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 98      450     2       2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           6

但我希望它返回:

FT2-E14     IGNORE      2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 223     450     12      2           6

总结一下:

  1. 分组由sMacName
  2. 不关心JobName(结果中不需要它)
  3. 按tShiftBegin分组
  4. 分组由tShiftEnd
  5. sJobName相同的dShots总和
  6. 按dQuant分组
  7. sSobName相同的dScrap总和
  8. 按SetCavity分组
  9. 服务条款总和
  10. 这在某种程度上可以用SQL吗?

2 个答案:

答案 0 :(得分:1)

这是一个使用子查询进行第一轮分组的工作小提琴:SQL Fiddle

SQL表格创建(仅供小提琴参考):

create table SHT_SUM_SCR  (
  sMacName nvarchar(100),
  sJobName nvarchar(100),
  tShiftBegin datetime,
  tShiftEnd datetime,
  dShots int,
  dQuant int,
  dScrap int,
  SetCavity int,
  TOS int
  )

insert into SHT_SUM_SCR(sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity,TOS)
values('FT2-E14','10401445710','2014-05-13 05:00:00.000','2014-05-13 13:00:00.000',98,450,2,2,0)
insert into SHT_SUM_SCR(sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity,TOS)
values('FT2-E14','10401445710','2014-05-13 05:00:00.000','2014-05-13 13:00:00.000',98,450,2,2,0)
insert into SHT_SUM_SCR(sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity,TOS)
values('FT2-E14','10401445792','2014-05-13 05:00:00.000','2014-05-13 13:00:00.000',125,450,10,2,0)
insert into SHT_SUM_SCR(sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity,TOS)
values('FT2-E14','10401445792','2014-05-13 05:00:00.000','2014-05-13 13:00:00.000',125,450,10,2,0)
insert into SHT_SUM_SCR(sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity,TOS)
values('FT2-E14','10401445792','2014-05-13 05:00:00.000','2014-05-13 13:00:00.000',125,450,10,2,0)
insert into SHT_SUM_SCR(sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity,TOS)
values('FT2-E14','10401445792','2014-05-13 05:00:00.000','2014-05-13 13:00:00.000',125,450,10,2,6)

查询以获得结果:

select sMacName,
      tShiftBegin,
      tShiftEnd,
      sum(dShots) as dshots,
      dQuant,
      sum(dScrap) as dScrap,
      SetCavity,
      sum(TOS) as TOS
from
(
  -- subquery - place your query here
  select sMacName,sJobName,tShiftBegin,tShiftEnd,
        dShots,dQuant,dScrap,SetCavity,sum(TOS) as tos
  from SHT_SUM_SCR
  group by sMacName,sJobName,tShiftBegin,tShiftEnd,dShots,dQuant,dScrap,SetCavity
  -- subquery end
) as results
group by sMacName,tShiftBegin,tShiftEnd,dQuant,SetCavity

如果修改此查询并围绕查询包装外部SQL,则应得到相同的结果。

答案 1 :(得分:1)

这可能不是最有效的解决方案,但确实可以解决问题。

从您的原始查询开始(为了便于阅读而编辑了一点):

SELECT     
  t.sName            AS sMacName
, j.sName            AS sJobName
, ss.tShtBegin       AS tShiftBegin
, ss.tShtEnd         AS tShiftEnd
, SUM(ss.dActShots)  AS dShots
, '450'              AS dQuant
, SUM(ss.dActScrap)  AS dScrap
, MAX(j.iSetCavity)  as SetCavity
, CASE
   WHEN (scr.iScrap) LIKE '99' THEN (scr.dActScrap)
   ELSE '0'
END                  as TOS
FROM dbo.SHT_SUM_SCR scr
RIGHT OUTER JOIN dbo.SHT_SUM ss  ON scr.lShtSumRef = ss.lShtSumRef 
LEFT OUTER JOIN dbo.JOB j        ON ss.lJobRef = j.lJobRef 
RIGHT OUTER JOIN dbo.TOL t       ON ss.lMacRef = t.lTolRef
WHERE 1=1
and t.bInactive = 0
AND (ss.tShtDay > DATEADD(DAY, - 1, CONVERT(DATE, GETDATE()))) 
AND (GETDATE() BETWEEN ss.tShtBegin AND ss.tShtEnd) 
AND (j.iSetCavity is Not NUll)
GROUP BY 
   t.sName
, ss.tShtBegin
, ss.tShtEnd
, j.sName
, scr.iScrap
, scr.dActScrap
--ORDER BY sMacName

返回

sMacName    sJobName    tShiftBegin             tShiftEnd               dShots  dQuant  dScrap  SetCavity   TOS
FT2-E14     10401445710 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 98      450     2       2           0
FT2-E14     10401445710 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 98      450     2       2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           0
FT2-E14     10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125     450     10      2           6

我将此查询放在子查询中,以便按sJobName分组:

select 
  i.sMacName    
, i.sJobName   
, i.tShiftBegin
, i.tShiftEnd
, max(i.dShots ) as dShots 
, i.dQuant
, max(i.dScrap ) as dScrap
, i.SetCavity
, sum(i.TOS ) as TOS
from (
   SELECT     
     t.sName            AS sMacName
   , j.sName            AS sJobName
   , ss.tShtBegin       AS tShiftBegin
   , ss.tShtEnd         AS tShiftEnd
   , SUM(ss.dActShots)  AS dShots
   , '450'              AS dQuant
   , SUM(ss.dActScrap)  AS dScrap
   , MAX(j.iSetCavity)  as SetCavity
   , CASE
      WHEN (scr.iScrap) LIKE '99' THEN (scr.dActScrap)
      ELSE '0'
   END                  as TOS
   FROM dbo.SHT_SUM_SCR scr
   RIGHT OUTER JOIN dbo.SHT_SUM ss  ON scr.lShtSumRef = ss.lShtSumRef 
   LEFT OUTER JOIN dbo.JOB j        ON ss.lJobRef = j.lJobRef 
   RIGHT OUTER JOIN dbo.TOL t       ON ss.lMacRef = t.lTolRef
   WHERE 1=1
   and t.bInactive = 0
   AND (ss.tShtDay > DATEADD(DAY, - 1, CONVERT(DATE, GETDATE()))) 
   AND (GETDATE() BETWEEN ss.tShtBegin AND ss.tShtEnd) 
   AND (j.iSetCavity is Not NUll)
   GROUP BY 
      t.sName
   , ss.tShtBegin
   , ss.tShtEnd
   , j.sName
   , scr.iScrap
   , scr.dActScrap
   --ORDER BY sMacName
) i
group by 
  i.sMacName    
, i.sJobName   
, i.tShiftBegin
, i.tShiftEnd
--, i.dShots 
, i.dQuant 
--, i.dScrap 
, i.SetCavity  
--, i.TOS 

返回:

sMacName    sJobName    tShiftBegin tShiftEnd   dShots  dQuant  dScrap  SetCavity   TOS
FT2-E14 10401445710 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 98  450 2   2   0
FT2-E14 10401445792 2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 125 450 10  2   6

然后可以再次将其放入子查询中,通过以下方式完成最后一组:

select 
  i2.sMacName
, 'IGNORE' as sJobName
, i2.tShiftBegin
, i2.tShiftEnd
, sum(i2.dShots)
, i2.dQuant
, sum(i2.dScrap)
, i2.SetCavity
, sum(i2.TOS)
from (
   select 
     i.sMacName    
   , i.sJobName   
   , i.tShiftBegin
   , i.tShiftEnd
   , max(i.dShots ) as dShots 
   , i.dQuant
   , max(i.dScrap ) as dScrap
   , i.SetCavity
   , sum(i.TOS ) as TOS
   from (
      SELECT     
        t.sName            AS sMacName
      , j.sName            AS sJobName
      , ss.tShtBegin       AS tShiftBegin
      , ss.tShtEnd         AS tShiftEnd
      , SUM(ss.dActShots)  AS dShots
      , '450'              AS dQuant
      , SUM(ss.dActScrap)  AS dScrap
      , MAX(j.iSetCavity)  as SetCavity
      , CASE
         WHEN (scr.iScrap) LIKE '99' THEN (scr.dActScrap)
         ELSE '0'
      END                  as TOS
      FROM dbo.SHT_SUM_SCR scr
      RIGHT OUTER JOIN dbo.SHT_SUM ss  ON scr.lShtSumRef = ss.lShtSumRef 
      LEFT OUTER JOIN dbo.JOB j        ON ss.lJobRef = j.lJobRef 
      RIGHT OUTER JOIN dbo.TOL t       ON ss.lMacRef = t.lTolRef
      WHERE 1=1
      and t.bInactive = 0
      AND (ss.tShtDay > DATEADD(DAY, - 1, CONVERT(DATE, GETDATE()))) 
      AND (GETDATE() BETWEEN ss.tShtBegin AND ss.tShtEnd) 
      AND (j.iSetCavity is Not NUll)
      GROUP BY 
         t.sName
      , ss.tShtBegin
      , ss.tShtEnd
      , j.sName
      , scr.iScrap
      , scr.dActScrap
      --ORDER BY sMacName
   ) i
   group by 
     i.sMacName    
   , i.sJobName   
   , i.tShiftBegin
   , i.tShiftEnd
   --, i.dShots 
   , i.dQuant 
   --, i.dScrap 
   , i.SetCavity  
   --, i.TOS 
) i2
group by 
  i2.sMacName
, i2.tShiftBegin
, i2.tShiftEnd
, i2.dQuant
, i2.SetCavity

这肯定不是很好,但确实会回复:

sMacName    sJobName    tShiftBegin tShiftEnd   (No column name)    dQuant  (No column name)    SetCavity   (No column name)
FT2-E14 IGNORE  2014-05-13 05:00:00.000 2014-05-13 13:00:00.000 223 450 12  2   6