Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Multiple Columns, Display In 1 Row - Please Help!

Author  Topic 

besadmin
Posting Yak Master

116 Posts

Posted - 2012-02-15 : 17:04:17
Hey Friends, I have an issue I believe some of you SQL Masters can quickly help with. Any advice is much appreciate and thanks in advance for any replys.

So I have a Table with Customer ID, Type, And Method. Where type is like Invoice, Statement, Order Confirmation and Method is Email, Fax or Text

So its like


CustomerID Type Method
12 Invoice Fax
12 Satement Fax
12 OrderConf Email
12 Newsletter Text


So that info is there. Then I want to do the following query.


SELECT
B.CustomerID
, A.[full name]
, A.[business phone]
, A.[mobile phone]
, 'No'
, 'Not Yet'
, A.[e-mail 1]
, case
when type = 'Invoices' then C.method
end as Invoices
, case
when type = 'Statements' then C.method
end as Statements
, case
when type = 'Order Confirmations' then C.method
end as Order_Confirmations
, case
when type = 'Packing Slips' then C.method
end as Packing_Slips
, 'N'
, GetDate()
, 'Frankt@Gmail.com'
, 'message' --<message, varchar(400),>
FROM Server.ContactsDetail A
JOIN Server.ContactsView B
ON A.cntctid = B.contactid
JOIN Server.Communication C
ON B.bsicustomerid = C.dealerno
WHERE B.bsicustomerid <> ''
AND A.[full name] <> ''


Finnaly, when i run that query the case is giving me a row for each type so each customer is listed like 4 times. i want each customer listed only once with each column having the corresponding value in it like so.


CustomerID Newletter Invoice OrderConf Statement
12 Fax Email Fax Text


How can i get it like that friends??

Let me know if you have any questions.

Thanks to all!!

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-02-16 : 01:34:44
use group by

Go to Top of Page

vijayan.vinu3
Starting Member

19 Posts

Posted - 2012-02-16 : 07:34:37
CHeck the following link...this might help you...it helped me

http://forums.devarticles.com/microsoft-sql-server-5/converting-rows-into-columns-cross-tab-39850.html
Go to Top of Page

besadmin
Posting Yak Master

116 Posts

Posted - 2012-02-16 : 14:08:36
Thanks to both for your replys, i really appreciate it! i tried using group by and in different orders with no luck. the link looks promising, but i havent tried it yet. I will reply with success or failure on that. Thanks again!
Go to Top of Page
   

- Advertisement -