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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to make data of 2 rows into single row??
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/01/2013 :  07:51:39  Show Profile  Reply with Quote
Hello all,

I had a issue with this query.I am getting result set for Single Id into 2 rows. For example : user named : X have landline number and STD and Area code. For X ID =

result is coming like :

LandNo	LandAreaCodeLandCountryCode	MobileNo MobileCountyCode	
856856	5756	14	NULL	NULL
NULL	NULL	NULL	646474575	2


my output should come like :

LandNo	LandAreaCodeLandCountryCode	MobileNo MobileCountyCode
856856	5756	14 646474575	2


my query is like :

select case when isMobile=0 then Number end as LandNumber,
case when isMobile=0 then AreaCode end as LandAreaCode,
case when isMobile=0 then CountryCodeID end as LandCountryCode,
case when isMobile=1 then (Number) end as MobileNumber,
case when isMobile=1 then CountryCodeID end as MobileCountryCode
from Cust_ContactNumbers where Cust_Family_ID=150

how to get result set in one row

P.V.P.MOhan

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 02/01/2013 :  07:55:43  Show Profile  Reply with Quote
Try using an aggregate function (such as MAX) for each of the columns like shown below. If you expect a different row for each customer, you would also need to group by customerid or a similar column
SELECT MAX(CASE 
            WHEN isMobile = 0 THEN Number
       END)  AS LandNumber,
       MAX(CASE 
            WHEN isMobile = 0 THEN AreaCode
       END)  AS LandAreaCode,
       MAX(CASE 
            WHEN isMobile = 0 THEN CountryCodeID
       END)  AS LandCountryCode,
       MAX(CASE 
            WHEN isMobile = 1 THEN (Number)
       END)  AS MobileNumber,
       MAX(CASE 
            WHEN isMobile = 1 THEN CountryCodeID
       END)  AS MobileCountryCode
FROM   Cust_ContactNumbers
WHERE  Cust_Family_ID = 150
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/01/2013 :  08:00:57  Show Profile  Reply with Quote
excellent james great one...it worked like a charm

P.V.P.MOhan
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 02/01/2013 :  08:24:02  Show Profile  Reply with Quote
Great! Glad it worked out.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000