SQL中怎樣簡化GROUP BY
SELECT A1.*, A2.ProdClassID, A2.LPiece, A2.LLPiece, A3.ProcName, A4.AreaName, A5.ClassName, A6.Notes,
A6.Remark, A2.NetAreaLeng, A2.NetAreaWid, A2.ProdLeng, A2.ProdWid, A2.CUHoleAVG ,A2.CUSurfaceAVG ,
A2.CUAreaC ,A2.CUAreaS , A2.VCutCross, A2.VCutRip, A2.GrossAreaLeng, A2.GrossAreaWid,
A2.InCuThick, A7.ASF AS ASF1, A8.ASF AS ASF2, A2.GoldArea,
sum(A9.NumOfHole*3.14*A9.HoleWidth/25.4*A10.NetThick/25.4/144) AS CuArea
FROM ProductSchedule A1(NOLOCK)
LEFT JOIN dbo.fnENProdBasic() A2 ON A2.PartNum = A1.PartNum AND A2.Revision = A1.Revision
LEFT JOIN ProcBasic A3(NOLOCK) ON A3.ProcCode = A1.ProcCode
LEFT JOIN ProductArea A4(NOLOCK) ON A4.AreaCode = A1.AreaID
LEFT JOIN ProductClass A5(NOLOCK) ON A5.ClassCode = A2.ProdClassID
LEFT JOIN WIPScheInfo A6(NOLOCK) ON A6.TheDate = A1.TheDate AND A6.PartNum = A1.PartNum AND A6.Revision
= A1.Revision AND ISNULL(A6.RwkFlag, 0) = ISNULL(A1.RwkFlag, 0) AND ISNULL(A6.SuperSedeFlag, 0) = ISNULL
(A1.SuperSedeFlag, 0) AND A6.ProcCode = A1.ProcCode AND CASE LEN(ISNULL(A6.AreaCode, '')) WHEN 0 THEN '0'
ELSE A6.AreaCode END = A1.AreaID AND A6.Shape = A1.Shape AND ISNULL(A6.ScheType, 1) = 1
LEFT JOIN vProdPlateC A7 ON A7.PartNum = A1.PartNum AND A7.Revision = A1.Revision
LEFT JOIN vProdPlateS A8 ON A8.PartNum = A1.PartNum AND A8.Revision = A1.Revision
LEFT JOIN ProdHole A9 ON A1.PartNum = A9.PartNum AND A1.Revision = A9.Revision
LEFT JOIN ProdBasic A10 ON A1.PartNum = A10.PartNum AND A1.Revision = A10.Revision
WHERE A1.PartNum = 'E002A4012B'
GROUP BY A1.TheDate, A1.PartNum, A1.Revision,A1.ProcCode,A1.AreaID,
A1.Shape,A1.RwkFlag,A1.SuperSedeFlag,A1.PCSSF,A1.ScheduleQTY,A1.ScheduleSF,
A1.ScheduleQTY1,A1.ScheduleSF1,A1.BeginQTY,A1.BeginSF,A1.InQTY,A1.InSF,A1.InQTY1,
A1.InSF1,A1.rInQTY,A1.rInSF,A1.rInQTY1,A1.rInSF1,A1.otherInQTY,A1.otherInSF,A1.OutQTY,
A1.OutSF,A1.OutQTY1,A1.OutSF1,A1.rOutQTY,A1.rOutSF,A1.rOutQTY1,A1.rOutSF1,A1.ScrapQTY,A1.ScrapSF,
A1.ScrapQTY1,A1.ScrapSF1,A1.ChangeQTY,A1.ChangeSF,A1.AdjustQTY,A1.AdjustSF,A1.EndQTY,
A1.EndSF,A1.TotalOutQTY,A1.TotalOutSF,A1.FinishRate,A1.CheckQTY,A1.WIPFlag,A1.EndQTY1,A1.EndSF1,
A1.ProcDes,A1.ProcFlag,A1.EndQTY2,A1.EndSF2,A1.EndQTY3,A1.EndSF3,A1.ControlFlag,A1.AreaCodeMain,
A2.ProdClassID, A2.LPiece, A2.LLPiece, A3.ProcName, A4.AreaName, A5.ClassName, A6.Notes,
A6.Remark,A2.NetAreaLeng, A2.NetAreaWid, A2.ProdLeng, A2.ProdWid, A2.CUHoleAVG ,A2.CUSurfaceAVG,
A2.CUAreaC,A2.CUAreaS ,A2.VCutCross, A2.VCutRip,A2.GrossAreaLeng, A2.GrossAreaWid,
A2.InCuThick,A7.ASF,A8.ASF,A2.GoldArea
ORDER BY A1.TheDate, A1.PartNum, A1.Revision, A1.ProcCode, A1.AreaID, A1.RwkFlag,A1.SuperSedeFlag,A1.Shape
[最优解释]
--帮整理,前面的A1.*,就决定了group by 后面会多,另外,这个是语法要求,没得省
SELECT A1.* ,
A2.ProdClassID ,
A2.LPiece ,
A2.LLPiece ,
A3.ProcName ,
A4.AreaName ,
A5.ClassName ,
A6.Notes ,
A6.Remark ,
A2.NetAreaLeng ,
A2.NetAreaWid ,
A2.ProdLeng ,
A2.ProdWid ,
A2.CUHoleAVG ,
A2.CUSurfaceAVG ,
A2.CUAreaC ,
A2.CUAreaS ,
A2.VCutCross ,
A2.VCutRip ,
A2.GrossAreaLeng ,
A2.GrossAreaWid ,
A2.InCuThick ,
A7.ASF AS ASF1 ,
A8.ASF AS ASF2 ,
A2.GoldArea ,
SUM(A9.NumOfHole * 3.14 * A9.HoleWidth / 25.4 * A10.NetThick / 25.4
/ 144) AS CuArea
FROM ProductSchedule A1 ( NOLOCK )
LEFT JOIN dbo.fnENProdBasic() A2 ON A2.PartNum = A1.PartNum
AND A2.Revision = A1.Revision
LEFT JOIN ProcBasic A3 ( NOLOCK ) ON A3.ProcCode = A1.ProcCode
LEFT JOIN ProductArea A4 ( NOLOCK ) ON A4.AreaCode = A1.AreaID
LEFT JOIN ProductClass A5 ( NOLOCK ) ON A5.ClassCode = A2.ProdClassID
LEFT JOIN WIPScheInfo A6 ( NOLOCK ) ON A6.TheDate = A1.TheDate
AND A6.PartNum = A1.PartNum
AND A6.Revision = A1.Revision
AND ISNULL(A6.RwkFlag, 0) = ISNULL(A1.RwkFlag,
0)
AND ISNULL(A6.SuperSedeFlag, 0) = ISNULL(A1.SuperSedeFlag,
0)
AND A6.ProcCode = A1.ProcCode
AND CASE LEN(ISNULL(A6.AreaCode,
''))
WHEN 0 THEN '0'
ELSE A6.AreaCode
END = A1.AreaID
AND A6.Shape = A1.Shape
AND ISNULL(A6.ScheType, 1) = 1
LEFT JOIN vProdPlateC A7 ON A7.PartNum = A1.PartNum
AND A7.Revision = A1.Revision
LEFT JOIN vProdPlateS A8 ON A8.PartNum = A1.PartNum
AND A8.Revision = A1.Revision
LEFT JOIN ProdHole A9 ON A1.PartNum = A9.PartNum
AND A1.Revision = A9.Revision
LEFT JOIN ProdBasic A10 ON A1.PartNum = A10.PartNum
AND A1.Revision = A10.Revision
WHERE A1.PartNum = 'E002A4012B'
GROUP BY A1.TheDate ,
A1.PartNum ,
A1.Revision ,
A1.ProcCode ,
A1.AreaID ,
A1.Shape ,
A1.RwkFlag ,
A1.SuperSedeFlag ,
A1.PCSSF ,
A1.ScheduleQTY ,
A1.ScheduleSF ,
A1.ScheduleQTY1 ,
A1.ScheduleSF1 ,
A1.BeginQTY ,
A1.BeginSF ,
A1.InQTY ,
A1.InSF ,
A1.InQTY1 ,
A1.InSF1 ,
A1.rInQTY ,
A1.rInSF ,
A1.rInQTY1 ,
A1.rInSF1 ,
A1.otherInQTY ,
A1.otherInSF ,
A1.OutQTY ,
A1.OutSF ,
A1.OutQTY1 ,
A1.OutSF1 ,
A1.rOutQTY ,
A1.rOutSF ,
A1.rOutQTY1 ,
A1.rOutSF1 ,
A1.ScrapQTY ,
A1.ScrapSF ,
A1.ScrapQTY1 ,
A1.ScrapSF1 ,
A1.ChangeQTY ,
A1.ChangeSF ,
A1.AdjustQTY ,
A1.AdjustSF ,
A1.EndQTY ,
A1.EndSF ,
A1.TotalOutQTY ,
A1.TotalOutSF ,
A1.FinishRate ,
A1.CheckQTY ,
A1.WIPFlag ,
A1.EndQTY1 ,
A1.EndSF1 ,
A1.ProcDes ,
A1.ProcFlag ,
A1.EndQTY2 ,
A1.EndSF2 ,
A1.EndQTY3 ,
A1.EndSF3 ,
A1.ControlFlag ,
A1.AreaCodeMain ,
A2.ProdClassID ,
A2.LPiece ,
A2.LLPiece ,
A3.ProcName ,
A4.AreaName ,
A5.ClassName ,
A6.Notes ,
A6.Remark ,
A2.NetAreaLeng ,
A2.NetAreaWid ,
A2.ProdLeng ,
A2.ProdWid ,
A2.CUHoleAVG ,
A2.CUSurfaceAVG ,
A2.CUAreaC ,
A2.CUAreaS ,
A2.VCutCross ,
A2.VCutRip ,
A2.GrossAreaLeng ,
A2.GrossAreaWid ,
A2.InCuThick ,
A7.ASF ,
A8.ASF ,
A2.GoldArea
ORDER BY A1.TheDate ,
A1.PartNum ,
A1.Revision ,
A1.ProcCode ,
A1.AreaID ,
A1.RwkFlag ,
A1.SuperSedeFlag ,
A1.Shape
FROM ProductSchedule A1
LEFT JOIN ProdHole A9 ON A1.PartNum = A9.PartNum AND A1.Revision = A9.Revision
LEFT JOIN ProdBasic A10 ON A1.PartNum = A10.PartNum AND A1.Revision = A10.Revision
GROUP BY A1.TheDate, A1.PartNum, A1.Revision, A1.ProcCode, A1.AreaID,
A1.Shape, A1.RwkFlag, A1.SuperSedeFlag
)
SELECT A1.*, A2.ProdClassID, A2.LPiece, A2.LLPiece, A3.ProcName, A4.AreaName, A5.ClassName, A6.Notes,
A6.Remark, A2.NetAreaLeng, A2.NetAreaWid, A2.ProdLeng, A2.ProdWid, A2.CUHoleAVG ,A2.CUSurfaceAVG ,
A2.CUAreaC ,A2.CUAreaS , A2.VCutCross, A2.VCutRip, A2.GrossAreaLeng, A2.GrossAreaWid,
A2.InCuThick, A7.ASF AS ASF1, A8.ASF AS ASF2, A2.GoldArea,A9.CuArea
FROM ProductSchedule A1(NOLOCK)
LEFT JOIN dbo.fnENProdBasic() A2 ON A2.PartNum = A1.PartNum AND A2.Revision = A1.Revision
LEFT JOIN ProcBasic A3(NOLOCK) ON A3.ProcCode = A1.ProcCode
LEFT JOIN ProductArea A4(NOLOCK) ON A4.AreaCode = A1.AreaID
LEFT JOIN ProductClass A5(NOLOCK) ON A5.ClassCode = A2.ProdClassID
LEFT JOIN WIPScheInfo A6(NOLOCK) ON A6.TheDate = A1.TheDate AND A6.PartNum = A1.PartNum AND A6.Revision
= A1.Revision AND ISNULL(A6.RwkFlag, 0) = ISNULL(A1.RwkFlag, 0) AND ISNULL(A6.SuperSedeFlag, 0) = ISNULL
(A1.SuperSedeFlag, 0) AND A6.ProcCode = A1.ProcCode AND CASE LEN(ISNULL(A6.AreaCode, '')) WHEN 0 THEN '0'
ELSE A6.AreaCode END = A1.AreaID AND A6.Shape = A1.Shape AND ISNULL(A6.ScheType, 1) = 1
LEFT JOIN vProdPlateC A7 ON A7.PartNum = A1.PartNum AND A7.Revision = A1.Revision
LEFT JOIN vProdPlateS A8 ON A8.PartNum = A1.PartNum AND A8.Revision = A1.Revision
LEFT JOIN vCuArea A9 ON A1.TheDate = A9.TheDate AND A1.PartNum = A9.PartNum AND A1.Revision = A9.Revision
AND A1.ProcCode = A9.ProcCode AND A1.AreaID = A9.AreaID
AND A1.Shape = A9.Shape AND A1.RwkFlag = A9.RwkFlag AND A1.SuperSedeFlag = A9.SuperSedeFlag
WHERE 1 = 0
ORDER BY A1.TheDate, A1.PartNum, A1.Revision, A1.ProcCode, A1.AreaID, A1.RwkFlag,A1.SuperSedeFlag,A1.Shape
這樣就可以了 3q