question

sunilsql47 avatar image
sunilsql47 asked

LOOPING THE DATA

Hi I am new to SQL, I have a strange requirement to loop the data. I don't have any idea to do that. I have a table ![alt text][1] Can anyone has an idea to do it in T-SQL? Thanks in Advance [1]: /storage/temp/1572-sql_central_q.png
looploopingwhile-loop
sql_central_q.png (4.2 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
DenisT avatar image
DenisT answered
Use UNPIVOT get the desired result: DECLARE @Table TABLE ( STORE INT , PROD1 BIT , PROD2 BIT , PROD3 BIT ); INSERT INTO @Table VALUES ( 1, 1, 0, 1 ), ( 2, 0, 1, 0 ), ( 3, 1, 1, 0 ); WITH cteUNPIVOT AS ( SELECT STORE , PRODUCT , EXIST FROM ( SELECT T.STORE , T.PROD1 , T.PROD2 , T.PROD3 FROM @Table T ) AS L1 UNPIVOT ( EXIST FOR PRODUCT IN ( PROD1, PROD2, PROD3 ) ) as L2 ) SELECT T1.STORE , T1.PRODUCT FROM cteUNPIVOT AS T1 WHERE T1.EXIST = 1;
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sunilsql47 avatar image sunilsql47 commented ·
Thank You Denis
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.