Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a parent table BASE with below structureISN Col1 COl2 Col31 cmntsX1 cmntsX2 CmntsX32 cmntsY1 CmntsY2 NULLThis will be loaded into the child table as ISN ColAll Count arraycolumn1 cmntsX1 3 01 cmntsX2 3 11 cmntsX3 3 22 cmntsY1 2 02 cmntsY2 2 1For ISN 1 i have value for all 3 columns so count for ISN 1 will be 3 & arraycolumn 0 to 2similarly for ISN 2 there are 2 entries for col1 & col2 so count in child table would become 2 & arraycolumn as 0 & 1Now i need to validate if the columns from base table are converted into rows in child table using a SQL query.Can any1 help in framing this query?
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2011-08-30 : 08:10:10
something like below should work:-
SELECT u.ISN,u.Val AS ColAll,COUNT(1) OVER (PARTITION BY u.ISN) AS [Count],ROW_NUMBER() OVER (PARTITION BY u.ISN ORDER BY u.Val ASC)-1 AS arraycolumnFROM YourtableUNPIVOT (Val FOR Col IN (Col1,Col2,Col3))u
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/