将售出数量转换为 SQL 中的标准化单位
Convert sold quantity to standardized units in SQL
我的结果包含不同单位的总售出数量。我需要根据参考 table 将其转换为标准化单位。此标准化单位可以从 KG、LBS 或 GAL 变化。
售出数量:
Material
UnitOfMeasure
QuantitySold
1910
KG
446780
1910
GAL
5000
1911
GAL
2000
参考:
Material
FromUnits
ToUnits
Multiplier
1910
GAL
Lbs
7.107
1910
Lbs
Kgs
0.5
1910
KG
Lbs
2.3
1911
GAL
Lbs
9.3
1911
Lbs
Kg
.9
1911
KG
Lbs
4.2
我需要:
Material
UnitOfMeasure
QuantitySold
Standardised_Lbs
Standardised_KG
Standardised_GAL
1910
KG
446780
(446780*2.3)
1910
GAL
5000
(5000*7.1)
1911
GAL
2000
(2000 * 9.3)
似乎 Conditional Aggregation
与 JOIN
配合使用就可以了。
Select A.Material
,A.UnitOfMeasure
,A.QuantitySold
,Standardised_Lbs = sum( case when B.ToUnits = 'Lbs' then A.QuantitySold * B.Multiplier end )
,Standardised_KG = sum( case when B.ToUnits = 'KG' then A.QuantitySold * B.Multiplier end )
,Standardised_GAl = sum( case when B.ToUnits = 'GAL' then A.QuantitySold * B.Multiplier end )
From [QuantitySold] A
Join [Reference] B on A.Material=B.Material
and A.UnitOfMeasure = B.FromUnits
Group By A.Material
,A.UnitOfMeasure
,A.QuantitySold
我的结果包含不同单位的总售出数量。我需要根据参考 table 将其转换为标准化单位。此标准化单位可以从 KG、LBS 或 GAL 变化。
售出数量:
Material | UnitOfMeasure | QuantitySold |
---|---|---|
1910 | KG | 446780 |
1910 | GAL | 5000 |
1911 | GAL | 2000 |
参考:
Material | FromUnits | ToUnits | Multiplier |
---|---|---|---|
1910 | GAL | Lbs | 7.107 |
1910 | Lbs | Kgs | 0.5 |
1910 | KG | Lbs | 2.3 |
1911 | GAL | Lbs | 9.3 |
1911 | Lbs | Kg | .9 |
1911 | KG | Lbs | 4.2 |
我需要:
Material | UnitOfMeasure | QuantitySold | Standardised_Lbs | Standardised_KG | Standardised_GAL |
---|---|---|---|---|---|
1910 | KG | 446780 | (446780*2.3) | ||
1910 | GAL | 5000 | (5000*7.1) | ||
1911 | GAL | 2000 | (2000 * 9.3) |
似乎 Conditional Aggregation
与 JOIN
配合使用就可以了。
Select A.Material
,A.UnitOfMeasure
,A.QuantitySold
,Standardised_Lbs = sum( case when B.ToUnits = 'Lbs' then A.QuantitySold * B.Multiplier end )
,Standardised_KG = sum( case when B.ToUnits = 'KG' then A.QuantitySold * B.Multiplier end )
,Standardised_GAl = sum( case when B.ToUnits = 'GAL' then A.QuantitySold * B.Multiplier end )
From [QuantitySold] A
Join [Reference] B on A.Material=B.Material
and A.UnitOfMeasure = B.FromUnits
Group By A.Material
,A.UnitOfMeasure
,A.QuantitySold