SQL Server How to get a text ("N/A") for my pivoted data where values do not exist against pivoted column?

r7s23pms  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(114)

Please find sample input tables named as Table1 and Table2.

Table1 :
| id | Dates | Sales |
| ------------ | ------------ | ------------ |
| 1 | 18 Sept'23 | 445.23 |
| 1 | 20 Sept'23 | 470.33 |
| 1 | 22 Sept'23 | 500.33 |

Table2 :

idDates
118 Sept'23
119 Sept'23
120 Sept'23
121 Sept'23
122 Sept'23
123 Sept'23
124 Sept'23

Desired output :

id18 Sept'2319 Sept'2320 Sept'2321 Sept'2322 Sept'2323 Sept'2324 Sept'23
1445.23N/A470.33N/A500.33N/AN/A
Declare @PvtQry nvarchar(max)
Declare @Qry nvarchar(max)

SELECT @PvtQry = COALESCE(@PvtQry+',','') + QUOTENAME([Dates]) FROM Table2 WHERE id = 1 Order by Dates Desc

Set @Qry = 'Select id,'+@PvtQry+' INTO ##Tbl_Temp
        FROM (Select id,Dates,Sales as SalesValue FROM Table1) AS Source
        PIVOT (Max (isnull(Cast(SalesValue as nvarchar(100)),''N/A'') FOR [Dates] IN ('+@PvtQry+') AS Pvt'
EXEC sp_executesql @Qry
SELECT * FROM ##Tbl_Temp

Error Message: Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '('.

c9qzyr3d

c9qzyr3d1#

The T-SQL generated in @qry is not valid, so I refactored it to work.

I moved the code to insert N/A from the PIVOT to the SELECT clause, the following is a sample query.

WITH source AS (
    SELECT t2.id, dates = FORMAT(t2.dates, 'dd MMM yy'), t1.sales
    FROM Table2 t2
    JOIN Table1 t1
        ON t2.id = t1.id
        AND t2.dates = t1.dates
)
Select id,[24 Sep 23] = IsNull(FORMAT([24 Sep 23], '0.00'), 'N/A'),[23 Sep 23] = IsNull(FORMAT([23 Sep 23], '0.00'), 'N/A'),[22 Sep 23] = IsNull(FORMAT([22 Sep 23], '0.00'), 'N/A'),[21 Sep 23] = IsNull(FORMAT([21 Sep 23], '0.00'), 'N/A'),[20 Sep 23] = IsNull(FORMAT([20 Sep 23], '0.00'), 'N/A'),[19 Sep 23] = IsNull(FORMAT([19 Sep 23], '0.00'), 'N/A'),[18 Sep 23] = IsNull(FORMAT([18 Sep 23], '0.00'), 'N/A')
FROM source
PIVOT (
    Max(sales) 
    FOR dates
    IN ([24 Sep 23],[23 Sep 23],[22 Sep 23],[21 Sep 23],[20 Sep 23],[19 Sep 23],[18 Sep 23]) 
    ) AS Pvt

To generate this code dynamically you need to use an additional variable to use in the SELECT clause, that cast the sales value to a string and manage the null value, in the following way:

DECLARE @dateFormat NVARCHAR(MAX) = 'dd MMM yy';
DECLARE @PvtQryForSelect nvarchar(max)
SELECT @PvtQryForSelect = COALESCE(@PvtQryForSelect+',','') +
    CONCAT(QUOTENAME(FORMAT([Dates],@dateFormat)),' = IsNull(FORMAT(', QUOTENAME(FORMAT([Dates], @dateFormat)), ', ''0.00''), ''N/A'')') FROM Table2 WHERE id = 1 Order by Dates Desc;

相关问题