Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Smile Unanswered: SQL rows into Columns

    Hi,

    I am trying to write a query where I need the rows obtained as a result of the query to be a column in Select list. I am guessing Pivot does this job but unable to understand it. Appreciate any help

  2. #2
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    This will work.
    Code:
    DROP TABLE DaTable
    CREATE TABLE DaTable(
    	id			INT	NOT NULL,
    	course_id	INT	NOT NULL,
    	admission_req_type	VARCHAR(25)	NOT NULL,
    	admission_req_description	VARCHAR(100)
    )
    
    INSERT INTO DaTable(id, course_id, admission_req_type, admission_req_description) VALUES
    (1, 1, 'prev. study', 'MBA'),
    (2, 1, 'medical', 'not colorblind'),
    (3, 1, 'physical', 'run 100 m in less than 10 seconds'),
    
    (6, 3, 'prev. study', 'kindergarten')
    
    
    SELECT VVW.course_id, d_admissance_description = 
    	STUFF((SELECT ', ' + VW.admission_req_description
    		FROM DaTable VW 
    		WHERE VVW.course_id = VW.course_id
    		ORDER BY VW.admission_req_type
    		FOR XML PATH('')), 1, 2, '')
    FROM DaTable VVW
    GROUP BY VVW.course_id
    Last edited by Wim; 06-24-13 at 06:41. Reason: simplified the code
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Thanks. I used unpivot operation. It worked for me

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •