2010-10-13 39 views
1

SQL SERVER或ORACLE中可以使用case statment轉換下面的度量單位嗎?如何使用CASE語句在SQL SERVER或ORACLE中轉換MEASURE的單位

select UoM, sum(Quantity) as 'QTY' 
from mytable 
group by UoM 


UoM QTY 
LBS 2.4 
LBS 2 
LBS 0.233 
LBS 0.97 
OZS 1.8 
GMS 1236 
LBS 120.459 
LBS 59.1 
LBS 252.82 
LBS 175.23 
LBS 3.42 
LBS 455.4 
LBS 57.6 
LBS 146.8 
LBS 117.78 
LBS 197.92 
LBS 40.245 
GMS 9 
LBS 15.78 
LBS 12.6 
LBS 125.1 
LBS 42.3 
LBS 1292.3 

1磅= 16盎司(盎司) 1磅=453.5924克(GMS)

+0

你的表結構是什麼?你有兩個不同的列你的'計量單位'和測量? – 2010-10-13 16:52:47

回答

2

我想你可以做這樣的事情

SELECT sum((case when UoM = 'LBS' then quantity 
        when UoM = 'OZS' then quantity/16 
        when UoM = 'GMS' then quantity/453.5924 
        else null 
       end)) weight_in_lbs, 
     sum((case when UoM = 'LBS' then quantity*16 
        when UoM = 'OZS' then quantity 
        when UoM = 'GMS' then quantity*16/453.5924 
        else null 
       end)) weight_in_ozs, 
     sum((case when UoM = 'LBS' then quantity*453.5924 
        when UoM = 'OZS' then quantity*453.5924/16 
        when UoM = 'GMS' then quantity 
       end)) weight_in_gms 
    FROM myTable 
4

你只是尋找一個單總數是?還是每個不同措施的小計?或者用3種不同的措施表達同樣的總數?

SELECT SUM(CASE UoM 
     WHEN 'GMS' THEN Quantity 
     WHEN 'LBS' THEN Quantity * 453.5924 
     WHEN 'GMS' THEN Quantity * 453.5924/16 
     END) AS TOTALGrams 
FROM myTable 

應該給你格蘭姆總計。

+0

不同的測量。但這會起作用。感謝,BradC! – joe 2010-10-13 17:56:39