如何在选择SQL中嵌套案例?

时间:2010-09-10 06:45:27

标签: c# sql mysql sql-server

我有这个选择Case SQL语句,用于计算给定数量的总体积。


SELECT
DropshipPackinglist.CaseNumber as 'CASE NO.',  
DropshipPackinglist.ItemNumber as 'BOM NO.', 
DropshipPackinglist.Quantity as 'QTY',
                      CASE 
                      WHEN DropshipPackinglist.Quantity >=31 and DropshipPackinglist.Quantity <= 36 then '1090x730x1460'
                      WHEN DropshipPackinglist.Quantity >=25 and DropshipPackinglist.Quantity <= 30  then '1090x730x1230'
                      WHEN DropshipPackinglist.Quantity >=19 and DropshipPackinglist.Quantity <= 24  then '1090x730x1000'
                      WHEN DropshipPackinglist.Quantity >=13 and DropshipPackinglist.Quantity <= 18  then '1090x720x790'
                      WHEN DropshipPackinglist.Quantity >=7 and DropshipPackinglist.Quantity <= 17  then '1090x720x570'
                      WHEN DropshipPackinglist.Quantity >=1 and DropshipPackinglist.Quantity <= 6  then '1090x720x350'

                      ELSE 'Unkown' 
                      end
                      as 'TOTAL VOLUME (MM3)'                                      

FROM         DropshipPackinglist INNER JOIN
                      HuaweiDescription ON DropshipPackinglist.ItemNumber = HuaweiDescription.ItemNumber

WHERE     (DropshipPackinglist.BatchCode LIKE '%0005041007100AHWA11HG')

-------------------------------------------------------------------------------------------
Result:

CaseNumber ItemNumber      Quantity  TotalVolume
1     52411573  5   1090x720x350
1     52411576  20  1090x730x1000
2     52411576  36  1090x730x1460

-------------------------------------------------------------------------------------------

现在,我想将casenumber和结果分组,只有一个总容量。

结果就是这个。

CaseNumber ItemNumber      Quantity  TotalVolume
1     52411573  5   1090x730x1230  -- sum(casenumber 1)=25
1     52411576  20  1090x730x1230  --
2     52411576  36  1090x730x1460

如何解决这个问题......谢谢。

2 个答案:

答案 0 :(得分:0)

如果每个caseNumber只需要一行,那么使用

SELECT CaseNumber, Quantity, SUM(ItemNumber) TotalVolume 
FROM (...YourOriginalQuery...)
GROUP BY CaseNumber, Quantity

如果您需要所有行但又想报告每个案例编号的总数,请使用以下查询:

SELECT CaseNumber, ItemNumber, Quantity,
                 SUM(ItemNumber) OVER(PARTITION BY CaseNumber) TotalVolume
    FROM   (SELECT DropshipPackinglist.CaseNumber, DropshipPackinglist.ItemNumber,
                                    DropshipPackinglist.Quantity,
                                    CASE
                                         WHEN DropshipPackinglist.Quantity >= 31
                                                    AND DropshipPackinglist.Quantity <= 36 THEN
                                            '1090x730x1460'
                                         WHEN DropshipPackinglist.Quantity >= 25
                                                    AND DropshipPackinglist.Quantity <= 30 THEN
                                            '1090x730x1230'
                                         WHEN DropshipPackinglist.Quantity >= 19
                                                    AND DropshipPackinglist.Quantity <= 24 THEN
                                            '1090x730x1000'
                                         WHEN DropshipPackinglist.Quantity >= 13
                                                    AND DropshipPackinglist.Quantity <= 18 THEN
                                            '1090x720x790'
                                         WHEN DropshipPackinglist.Quantity >= 7
                                                    AND DropshipPackinglist.Quantity <= 17 THEN
                                            '1090x720x570'
                                         WHEN DropshipPackinglist.Quantity >= 1
                                                    AND DropshipPackinglist.Quantity <= 6 THEN
                                            '1090x720x350'
                                         ELSE
                                            'Unkown'
                                    END AS 'TOTAL VOLUME (MM3)'
                     FROM   DropshipPackinglist
                     INNER  JOIN HuaweiDescription
                     ON     DropshipPackinglist.ItemNumber = HuaweiDescription.ItemNumber
                     WHERE  (DropshipPackinglist.BatchCode LIKE '%0005041007100AHWA11HG'))

答案 1 :(得分:0)

;with SuperSelect as 
(
 SELECT  dpl.CaseNumber as 'CASE NO.'
     ,dpl.ItemNumber as 'BOM NO.'
     ,dpl.Quantity as 'QTY'
     ,CASE WHEN dpl.Quantity >= 31 and dpl.Quantity <= 36 then '1090x730x1460'
     WHEN dpl.Quantity >= 25 and dpl.Quantity <= 30 then '1090x730x1230'
     WHEN dpl.Quantity >= 19 and dpl.Quantity <= 24 then '1090x730x1000'
     WHEN dpl.Quantity >= 13 and dpl.Quantity <= 18 then '1090x720x790'
     WHEN dpl.Quantity >= 7 and dpl.Quantity <= 17 then '1090x720x570'
     WHEN dpl.Quantity >= 1 and dpl.Quantity <= 6 then '1090x720x350'
     ELSE 'Unkown'
   end as 'TOTAL VOLUME (MM3)'
 FROM    DropshipPackinglist dpl
 INNER JOIN HuaweiDescription hd ON dpl.ItemNumber = hd.ItemNumber
 WHERE   (dpl.BatchCode LIKE '%0005041007100AHWA11HG')
)
select *, sum([QTY]) over (partition by ss.[CASE NO.]) [TotalVolume]
from SuperSelect ss