TABEL CONTOH
SELECT * INTO #tblStock
FROM
(
SELECT 'A' PartCode, 10 StockQty, 'WHs-A' Location
UNION ALL
SELECT 'B', 22, 'WHs-A'
UNION ALL
SELECT 'A', 1, 'WHs-B'
UNION ALL
SELECT 'C', 20, 'WHs-A'
UNION ALL
SELECT 'D', 39, 'WHs-F'
UNION ALL
SELECT 'E', 3, 'WHs-D'
UNION ALL
SELECT 'F', 7, 'WHs-A'
UNION ALL
SELECT 'A', 9, 'WHs-C'
UNION ALL
SELECT 'D', 2, 'WHs-A'
UNION ALL
SELECT 'F', 54, 'WHs-E'
)TAB
Dapatkan kolom untuk pivot dinamis dan ganti NULL
dengan zero
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + Location + ']', '[' + Location + ']')
FROM (SELECT DISTINCT Location FROM #tblStock) PV
ORDER BY Location
-- Since we need Total in last column, we append it at last
SELECT @cols += ',[Total]'
--Varible to replace NULL with zero
DECLARE @NulltoZeroCols NVARCHAR (MAX)
SELECT @NullToZeroCols = SUBSTRING((SELECT ',ISNULL(['+Location+'],0) AS ['+Location+']'
FROM (SELECT DISTINCT Location FROM #tblStock)TAB
ORDER BY Location FOR XML PATH('')),2,8000)
SELECT @NullToZeroCols += ',ISNULL([Total],0) AS [Total]'
Anda dapat menggunakan CUBE
untuk menemukan total baris dan kolom dan mengganti NULL
dengan Total
untuk baris yang dihasilkan dari CUBE
.
DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT PartCode,' + @NulltoZeroCols + ' FROM
(
SELECT
ISNULL(CAST(PartCode AS VARCHAR(30)),''Total'')PartCode,
SUM(StockQty)StockQty ,
ISNULL(Location,''Total'')Location
FROM #tblStock
GROUP BY Location,PartCode
WITH CUBE
) x
PIVOT
(
MIN(StockQty)
FOR Location IN (' + @cols + ')
) p
ORDER BY CASE WHEN (PartCode=''Total'') THEN 1 ELSE 0 END,PartCode'
EXEC SP_EXECUTESQL @query
- Klik di sini untuk melihat hasil
HASIL
CATATAN :Jika Anda ingin NULL
bukannya zero
sebagai nilai, gunakan @cols
bukannya @NulltoZeroCols
dalam kode pivot dinamis
EDIT :
1. Hanya tampilkan Total Baris
- Jangan gunakan kode
SELECT @cols += ',[Total]'
danSELECT @NullToZeroCols += ',ISNULL([Total],0) AS [Total]'
. - Gunakan
ROLLUP
bukannyaCUBE
.
2. Hanya tampilkan Total Kolom
- Gunakan kode
SELECT @cols += ',[Total]'
danSELECT @NullToZeroCols += ',ISNULL([Total],0) AS [Total]'
. - Gunakan
ROLLUP
bukannyaCUBE
. - Ubah
GROUP BY Location,PartCode
keGROUP BY PartCode,Location
. - Alih-alih
ORDER BY CASE WHEN (PartCode=''Total'') THEN 1 ELSE 0 END,PartCode
, gunakanWHERE PartCode<>''TOTAL'' ORDER BY PartCode
.
UPDATE :Untuk membawa PartName
untuk OP
Saya memperbarui kueri di bawah ini untuk menambahkan PartName
dengan hasil. Sejak PartName
akan menambahkan hasil tambahan dengan CUBE
dan untuk menghindari kebingungan dalam AND
atau OR
kondisi, lebih baik untuk menggabungkan hasil pivot dengan DISTINCT
nilai di tabel sumber Anda.
DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT P.PartCode,T.PartName,' + @NulltoZeroCols + ' FROM
(
SELECT
ISNULL(CAST(PartCode AS VARCHAR(30)),''Total'')PartCode,
SUM(StockQty)StockQty ,
ISNULL(Location,''Total'')Location
FROM #tblStock
GROUP BY Location,PartCode
WITH CUBE
) x
PIVOT
(
MIN(StockQty)
FOR Location IN (' + @cols + ')
) p
LEFT JOIN
(
SELECT DISTINCT PartCode,PartName
FROM #tblStock
)T
ON P.PartCode=T.PartCode
ORDER BY CASE WHEN (P.PartCode=''Total'') THEN 1 ELSE 0 END,P.PartCode'
EXEC SP_EXECUTESQL @query
- Klik di sini untuk melihat hasil