Learning #1 - Script for Insert Data generation from MSSQL tables using SSMS (Hidden Gem in MSSQL)
Step 1 - Database -> Tasks -> Generate Scripts
Step 2 - Generate the Database objects (Tables as needed)
Learning #2- Pivot for Data Preparation scenario
For a given scenario of customer/orders, Pivoting the data for next level of tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @cols AS NVARCHAR(MAX), | |
@query AS NVARCHAR(MAX) | |
SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(ItemId) | |
FROM CUSTOMER_ORDER | |
FOR XML PATH(''), TYPE | |
).value('.', 'NVARCHAR(MAX)') | |
,1,1,'') | |
SELECT @query = | |
'SELECT * FROM | |
(SELECT | |
CUSTOMER_NUMBER, | |
ItemId, | |
1 as INTC | |
FROM CUSTOMER_ORDER)X | |
PIVOT | |
( | |
COUNT(INTC) | |
for ItemId in (' + @cols + ') | |
) P' | |
EXEC SP_EXECUTESQL @query | |
Happy Learning!!!