Results 1 to 3 of 3
  1. #1
    Join Date
    May 2003
    Location
    Pinoy in NJ
    Posts
    104

    Unanswered: Is there a better way with T-SQL instead of a CURSOR

    Is there a better way to query to get the desired output on the RIGHT instead of using a cursor or while? TIA
    Attached Thumbnails Attached Thumbnails Tables.PNG  

  2. #2
    Join Date
    Apr 2012
    Posts
    213
    Try something like this:

    Code:
    SELECT distinct
        t.TransNo,
        t.TermNo,
        (SELECT s.Mode
         FROM MyTable as s
         WHERE 
             s.TransNo = t.TransNo AND
             s.TermNo = t.TermNo
         FOR XML PATH(''), TYPE).value('.', 'varchar(max)') as Mode
    FROM MyTable as t
    Hope this helps.

  3. #3
    Join Date
    May 2003
    Location
    Pinoy in NJ
    Posts
    104

    Thanks

    Made some changes and got this finally - thanks!

    Code:
    SELECT DISTINCT TransNo,TermNo
    	STUFF(
    			(
    			SELECT SUB.Mode AS [text()]
    			FROM #TEST AS SUB
    			WHERE (MAIN.TransNo=SUB.TransNo AND MAIN.TermNo=SUB.TermNo)
    			FOR XML PATH('')),1,0,''		
    			
    		) AS Mode
       
    FROM #TEST AS MAIN

Posting Permissions

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