1

I have a table like below:

panelistId  DTTM
337322  7/27/2014 19:39
337322  7/27/2014 19:29
317420  7/27/2014 10:22
317420  7/27/2014 10:22
317420  7/27/2014 9:27
336333  7/27/2014 5:41
336333  7/27/2014 3:26
336333  7/27/2014 3:26
336333  7/27/2014 1:25

I am looking for a SQL select query to have below fields from this table:

panelistId | #ofExposures | Exposure_DTTM1 | Exposure_DTTM2 | Exposure_DTTM3 |  Exposure_DTTM4 | Exposure_DTTM5 | etc
337322 | 2 | 7/27/2014 19:39 | 7/27/2014 19:29 |  |  |  

Here number of DTTM will be different for each id. If an id will have only 3 DTTM then for rest of the DTTM columns it can be blank.

4
  • Welcome to StackOverflow: if you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it! Commented Aug 3, 2014 at 12:36
  • You are looking for count(*) group by panelistId and then pivot Commented Aug 3, 2014 at 12:38
  • I am looking for something like "data in a singular row per respondent" for more analysis on data Commented Aug 3, 2014 at 12:54
  • What you are looking for is a crosstab and there are at least a million explanations on the internet. Commented Aug 3, 2014 at 13:19

1 Answer 1

1

This should do the trick. Hope it helps

DECLARE @Table2 TABLE (RowID INT IDENTITY(1, 1),
                   panelistId INT ,
                   #ofExposures VARCHAR(MAX),
                   Exposure_DTTMVARCHAR(MAX))

DECLARE @Table TABLE (ID INT ,
                  DTTM VARCHAR(30)) INSERT INTO @Table (ID,
                DTTM) VALUES ('337322', '7/27/2014 19:39'), ('337322', '7/27/2014 19:29'), ('317420', '7/27/2014 10:22'), ('317420', '7/27/2014 10:22'), ('317420', '7/27/2014 9:27'), ('336333', '7/27/2014 5:41'), ('336333', '7/27/2014 3:26'), ('336333', '7/27/2014 3:26'), ('336333', '7/27/2014 1:25'); WITH cte_StageOne
    AS (

    SELECT ROW_NUMBER() OVER(PARTITION BY id ORDER BY CAST(t.DTTM AS DATETIME) ) AS OrderID,         *
    FROM @Table AS t
    )
    INSERT INTO @Table2
    SELECT  MAX(ID)  AS panelistId,
          MAX(OrderID) AS #ofExposures,
          STUFF((
               SELECT  ' | ' + cso.DTTM
               FROM cte_StageOne AS cso
               WHERE ocso.ID = cso.ID
               FOR XML PATH('')
               ), 1, 3, '') AS #Exposure
    FROM cte_StageOne AS ocso
    GROUP BY ID

--SELECT * FROM @Table2

DECLARE @MaxDTTM INT DECLARE @StartLoop INT DECLARE @EndLoop INT DECLARE @ColumnList VARCHAR(MAX)

SELECT  @MaxDTTM = MAX(#ofExposures),
       @EndLoop = MAX(RowID),
       @StartLoop = MIN(RowID) FROM @Table2

SET @ColumnList = 'panelistId|#ofExposures|Exposure_DTTM1'

WHILE @StartLoop <= @EndLoop
    BEGIN
       SET @ColumnList = @ColumnList + '|Exposure_DTTM' + CAST(@StartLoop + 1 AS VARCHAR(3))

       SET @StartLoop = @StartLoop + 1
    END SET @ColumnList = @ColumnList

SET @StartLoop = 1

WHILE @StartLoop <= @EndLoop
    BEGIN
       DECLARE @in_panelistId VARCHAR(10)
       DECLARE @in_ofExposures VARCHAR(10)
       DECLARE @in_Exposure_DTTM VARCHAR(MAX)

       SELECT  @in_panelistId = panelistId,
             @in_ofExposures = #ofExposures,
             @in_Exposure_DTTM = Exposure_DTTM
       FROM @Table2
       WHERE RowID = @StartLoop

       SET @ColumnList = @ColumnList + CHAR(13) + CHAR(10) + @in_panelistId + '|' + @in_ofExposures + '|' + @in_Exposure_DTTM + REPLICATE('|', @MaxDTTM - @in_ofExposures)

       --PRINT REPLICATE('|',@MaxDTTM - @in_ofExposures)

       SET @StartLoop = @StartLoop + 1
    END PRINT @ColumnList
Sign up to request clarification or add additional context in comments.

3 Comments

This will not fully solve my requirement.<br> I am looking for a result like:<br> panelistId | #ofExposures | Exposure_DTTM1 | Exposure_DTTM2 | Exposure_DTTM3 | Exposure_DTTM4 | Exposure_DTTM5 | etc<br> 337322 | 2 | 7/27/2014 19:39 | 7/27/2014 19:29 | | | <br> 317420 | 3 | 7/27/2014 10:22 | 7/27/2014 10:22 | 7/27/2014 9:27 | | <br> 336333 | 4 | 7/27/2014 5:41 | 7/27/2014 3:26 | 7/27/2014 3:26 | 7/27/2014 1:25 |
@Anoop so do u need pipe delimited with everything in one line?
@Anoop Made the changes as requested, Please verify and hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.