クエリでクロス集計を使用して、要件に従って行を列に変換しました。それでも、私は望ましい結果から離れています。ここで私はいくつかのテストデータを与えました
Declare @tblDepartment Table
(
DepartmentID int,
DepartmentName nvarchar(10)
)
Insert into @tblDepartment
Select 30,'AA'
union
Select 31,'BB'
union
Select 32,'CC'
union
Select 33,'DD'
union
Select 34,'EE'
SELECT * FROM @tblDepartment
/*************************************************/
Declare @tblCurrency Table
(
CurrencyID int,
CurrencyName nvarchar(10)
)
Insert into @tblCurrency
Select 1,'AUD'
union
Select 2,'USD'
union
Select 3,'Euro'
union
Select 4,'GBP'
SELECT * FROM @tblCurrency
/*************************************************/
Declare @tblSale Table
(
ProductID int,
DepartmentID int,
CurrencyID int,
Value money
)
Insert into @tblSale
Select 1, 30, 2, 160.00 UNION
Select 1, 30, 3, 91927.00 UNION
Select 1, 32, 3, 914426.00 UNION
Select 1, 34, 4, 121.00 UNION
Select 2, 33, 4, 121.00 UNION
Select 2, 32, 4, 121.00 UNION
Select 2, 33, 2, 100.00 UNION
Select 2, 33, 3, 2000.00 UNION
Select 2, 33, 4, 121.00 UNION
Select 2, 32, 2, 52.00 UNION
Select 2, 32, 3, 5450.00 UNION
Select 2, 32, 4, 121.00 UNION
Select 2, 34, 1, 250.00 UNION
Select 2, 34, 2, 240.00 UNION
Select 2, 34, 3, 4540.00 UNION
Select 2, 34, 4, 8972.00
SELECT * FROM @tblSale
そして、ここに出力があります
DepartmentID DepartmentName
30 AA
31 BB
32 CC
33 DD
34 EE
CurrencyId CurrencyName
1 AUD
2 USD
3 Euro
4 GBP
ProductID DepartmentID CurrencyID Value
1 30 2 160.00
1 30 3 91927.00
1 32 3 914426.00
1 34 4 121.00
2 33 4 121.00
2 32 4 121.00
2 33 2 100.00
2 33 3 2000.00
2 33 4 121.00
2 32 2 52.00
2 32 3 5450.00
2 32 4 121.00
2 34 1 250.00
2 34 2 240.00
2 34 3 4540.00
2 34 4 8972.00
クロス集計を使用すると、次の結果が得られます
Select ProductID, DepartmentID,
Sum(CASE CurrencyID When 1 then value else 0 END) as AUD,
Sum(CASE CurrencyID When 2 then value else 0 End) as USD,
Sum(CASE CurrencyID When 3 then value else 0 END) as EURO,
Sum(CASE CurrencyID When 4 then value else 0 End) as GBP
from
(
SELECT T.ProductID, T.DepartmentID, T.CurrenCyID, T.Value
FROM @tblSale AS T
) S
Group By ProductID, DepartmentID
Order By ProductID, DepartmentID
ProductID DepartID AUD USD Euro GBP
1 30 0.00 160.00 91927.00 0.00
1 32 0.00 0.00 914426.00 0.00
1 34 0.00 0.00 0.00 121.00
2 32 0.00 52.00 5450.00 121.00
2 33 0.00 100.00 2000.00 121.00
2 34 250.00 240.00 4540.00 8972.00
しかし、存在しない場合は、デフォルトのゼロ値で各製品に対してすべての部門を表示する必要があります。
ProductID DepartID AUD USD Euro GBP
1 30 0.00 160.00 91927.00 0.00
1 31 0.00 0.00 0.00 0.00
1 32 0.00 0.00 914426.00 0.00
1 33 0.00 0.00 0.00 0.00
1 34 0.00 0.00 0.00 121.00
2 30 0.00 0.00 0.00 0.00
2 31 0.00 0.00 0.00 0.00
2 32 0.00 52.00 5450.00 121.00
2 33 0.00 100.00 2000.00 121.00
2 34 250.00 240.00 4540.00 8972.00
何か提案はありますか?