If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > view multiple records in a single row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-12, 05:40
sa32529 sa32529 is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
view multiple records in a single row

Id account num acc_type
42 1376200071278 gl
42 1308111111111 ic
42 1291111111111 os
34 1245200000000 gl
34 1132485111111 ic


this is table structure
there are multiple records like this in a table . I need output as

id gl accountnum ic accountnum osaccountnum
42 1376200071278 1308111111111 1291111111111
34 1245200000000 1132485111111 -
Reply With Quote
  #2 (permalink)  
Old 01-03-12, 07:30
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
Try this:
Code:
SELECT ids.id
	,COALESCE(gl.account, '-') as gl_AccountNum
	,COALESCE(ic.account, '-') as ic_AccountNum
	,COALESCE(os.account, '-') as os_AccountNum
FROM (SELECT DISTINCT ID
	FROM #DaTable
	) AS ids
	LEFT OUTER JOIN #DaTable as gl ON
		ids.id = gl.id AND
		gl.Acc_Type = 'gl'	
	LEFT OUTER JOIN #DaTable as ic ON
		ids.id = ic.id AND
		ic.Acc_Type = 'ic'	
	LEFT OUTER JOIN #DaTable as os ON
		ids.id = os.id AND
		os.Acc_Type = 'os'
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
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
Reply With Quote
  #3 (permalink)  
Old 01-03-12, 08:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT id
     , MAX(CASE WHEN type = 'gl'
                THEN accountnum
                ELSE NULL END) AS gl_accountnum
     , MAX(CASE WHEN type = 'ic'
                THEN accountnum
                ELSE NULL END) AS ic_accountnum
     , MAX(CASE WHEN type = 'os'
                THEN accountnum
                ELSE NULL END) AS os_accountnum
  FROM daTable
GROUP
    BY id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 01-03-12, 09:02
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
Rudy,

I compared your solution with mine (Estimated Execution Plan).
- LEFT OUTER JOIN solution : 63% relative execution cost
- GROUP BY solution : 37% relative execution cost

Yours is definitely the winner.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
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
Reply With Quote
  #5 (permalink)  
Old 01-03-12, 09:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
thanks

it's a technique that also has the benefit of working when there is (incorrectly?) more than one row of an account type, which would blow up the join solution

i'm gonna trademark the phrase "defensive sql"

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 01-03-12, 11:35
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
The phrase "Defensive SQL" would be a challengle to trademark. I've got 15 year old documents (written in WordPerfect 4.2 no leess) using that phrase!

It is frightening how far I've come since writing that paper, but defensive coding is still needed as much today as it was then!

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #7 (permalink)  
Old 01-04-12, 00:53
sa32529 sa32529 is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Wink

Quote:
Originally Posted by r937 View Post
Code:
SELECT id
     , MAX(CASE WHEN type = 'gl'
                THEN accountnum
                ELSE NULL END) AS gl_accountnum
     , MAX(CASE WHEN type = 'ic'
                THEN accountnum
                ELSE NULL END) AS ic_accountnum
     , MAX(CASE WHEN type = 'os'
                THEN accountnum
                ELSE NULL END) AS os_accountnum
  FROM daTable
GROUP
    BY id


thank u very much
Reply With Quote
  #8 (permalink)  
Old 01-04-12, 08:44
Maxine2012 Maxine2012 is offline
Registered User
 
Join Date: Jan 2012
Posts: 1
hi

very useful for me too. thanks a lot.
Reply With Quote
Reply

Tags
multiple rows, single row

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On