October 10, 2007 at 2:32 am
Hello
I have a table (contacts) with this format:
clientType, clientID , contactusage, contacttype, address
internal , 1 , private , email , xxx@xxx.com
internal , 1 , private , phone , xxx
internal , 1 , professional , email , yyy@yyy.com
internal , 1 , professional , phone , yyy
I need to have a view that groups all the info of a client in one record:
clientID, privatemail , profemail , privatephone , profphone
1 , xxx@xxx.com , yyy@yyy.com , xxx , yyy
This is what i did, but am concerned about performance:
Select clientID,
max(CASE WHEN (contactusage= 'private') AND (contacttype= 'email') THEN address ELSE NULL END) AS privatemail,
max(CASE WHEN (contactusage= 'professional') AND (contacttype= 'email') THEN address ELSE NULL END) AS profemail,
max(CASE WHEN (contactusage= 'private') AND (contacttype= 'phone') THEN address ELSE NULL END) AS privatephone,
max(CASE WHEN (contactusage= 'professional') AND (contacttype= 'phone') THEN address ELSE NULL END) AS profphone
from contacts
where clientType = 'internal'
group by clientID
any comment to optimize or even re-write?!
tx!!
October 10, 2007 at 3:27 am
Your solution looks fine to me :).
Andras
October 10, 2007 at 3:38 am
If Andras Belokosztolszki from Red Gate says it's ok, then am ok!
Tx!!:cool:
October 10, 2007 at 6:41 am
The code you have is the classic code necessary to put records together from multiple rows in a name/value table. It's actually pretty darned fast... only thing faster would be proper table design 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply