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

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Merging query rows into single row.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

smantz
Starting Member

USA
6 Posts

Posted - 08/01/2012 :  17:57:28  Show Profile  Reply with Quote
I have a query, when run, gives me the following output:

ID Last First phone1 phone2 phone3 phone4
1 doe john 1 null null null
1 doe john 1 2 null null
1 doe john 1 null 3 null
1 doe john 1 null null 4
2 may sue x null null null
2 may sue x null y null
2 may sue x null null null
2 may sue x null null null
2 may sue x null null z


I was wondering if there is a way combine all the like id rows into just one row to end up with:

ID Last First phone1 phone2 phone3 phone4
1 doe john 1 2 3 4
2 may sue x null y z

Please keep in mind the original rows are the out put of a query.
Thanks,
SM

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 08/01/2012 :  18:38:33  Show Profile  Reply with Quote
can you show us your query ?


KH
Time is always against us

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47060 Posts

Posted - 08/01/2012 :  19:10:08  Show Profile  Reply with Quote
you've some additional columns in group by which is why values get split into rows

As per given output you should have only ID,last and first in group by

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

smantz
Starting Member

USA
6 Posts

Posted - 08/01/2012 :  19:28:09  Show Profile  Reply with Quote
I have the query at work so I will post it in the morning.
Go to Top of Page

smantz
Starting Member

USA
6 Posts

Posted - 08/01/2012 :  21:39:41  Show Profile  Reply with Quote
I found my query in another place so here it is:

Student extract
-- Rev 05-09-2012 by S Mantz -- Added case statements for additional phone contacts as demonstrated by Murphey2. Join statement added additional rows for various phone types. Need to clean up Null values in columns and combine rows into single row.

select
s.sid as ReferenceCode,
'Student' as ContactType,
s.fname as FirstName,
s.lname as LastName,
'Class of ' + s.gradyear as Status,
isnull (l.description,'') as Language,
s.sex as Gender,

case when hi.homephone1 is NULL then ''
when hi.homephone1=' ' then ''
else '(' + substring (hi.homephone1, 1,3) + ') ' + substring (hi.homephone1, 5,8)
end as PrimaryPhone,
'' as HomePhone,
CASE WHEN cn.PhoneTitle like 'Father% Work Phone' THEN Number
END Workphone,
CASE WHEN cn.PhoneTitle like 'Father% Cell Phone' THEN Number
END MobilePhone,
'' as HomePhoneAlt,
CASE WHEN cn.PhoneTitle like 'Mother% Work Phone' THEN Number
END WorkPhoneAlt,
CASE WHEN cn.PhoneTitle like 'Mother% Cell Phone' THEN Number
END MobilePhoneAlt,
isnull (s.email,'') as EmailAddress,
'' as EmailAddressAlt,
'' as SMSPhone,
'' as [Group],
'' as [Group],
'' as [Group],
'001' as Institution
from student s
left join person p on p.personid=s.persid
left join codes l on l.code=s.prilang and l.grpid='langua'
left join studenthomeinfo hi on hi.persid=s.persid
left join StudentContactNumbers cn on cn.sno=s.sno
where s.status='A'
order by s.lname, s.fname

This output creates several rows for each student id; a row for each phone type, whether it is null or has a value, for each student. The phone number for the "primary phone number" appears in every row. Some rows are all nulls (except primary phone... same number for every like id row)or if there is a phone number for the type, it appears. This is the first example I gave. What I need is my second example. Sorry if this looks awkward, but I used some pieces from another query to get started. Hope this helps.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47060 Posts

Posted - 08/01/2012 :  22:01:29  Show Profile  Reply with Quote

select ReferenceCode,
ContactType,
FirstName,
LastName,
Status,
Language,
Gender,
MAX(PrimaryPhone) AS PrimaryPhone,
MAX(HomePhone) AS HomePhone,
MAX(Workphone) AS Workphone,
MAX(MobilePhone) AS MobilePhone,
MAX(WorkPhoneAlt) AS WorkPhoneAlt,
MAX(MobilePhoneAlt) AS MobilePhoneAlt,
MAX(EmailAddress) AS EmailAddress,
MAX(EmailAddressAlt) AS EmailAddressAlt,
SMSPhone,
[Group1],
[Group2],
[Group3],
Institution
from
(
select 
s.sid as ReferenceCode, 
'Student' as ContactType, 
s.fname as FirstName,
s.lname as LastName,
'Class of ' + s.gradyear as Status,
isnull (l.description,'') as Language,
s.sex as Gender,

case when hi.homephone1 is NULL then ''
when hi.homephone1=' ' then ''
else '(' + substring (hi.homephone1, 1,3) + ') ' + substring (hi.homephone1, 5,8)
end as PrimaryPhone,
'' as HomePhone,
CASE WHEN cn.PhoneTitle like 'Father% Work Phone' THEN Number
END Workphone,
CASE WHEN cn.PhoneTitle like 'Father% Cell Phone' THEN Number
END MobilePhone,
'' as HomePhoneAlt,
CASE WHEN cn.PhoneTitle like 'Mother% Work Phone' THEN Number
END WorkPhoneAlt,
CASE WHEN cn.PhoneTitle like 'Mother% Cell Phone' THEN Number
END MobilePhoneAlt,
isnull (s.email,'') as EmailAddress,
'' as EmailAddressAlt,
'' as SMSPhone,
'' as [Group],
'' as [Group],
'' as [Group],
'001' as Institution
from student s
left join person p on p.personid=s.persid
left join codes l on l.code=s.prilang and l.grpid='langua'
left join studenthomeinfo hi on hi.persid=s.persid
left join StudentContactNumbers cn on cn.sno=s.sno
where s.status='A'
)t
GROUP BY ReferenceCode,
ContactType,
FirstName,
LastName,
Status,
Language,
Gender,
SMSPhone,
[Group1],
[Group2],
[Group3],
Institution
order by lname, fname


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

smantz
Starting Member

USA
6 Posts

Posted - 08/02/2012 :  00:30:14  Show Profile  Reply with Quote
Visakh16 your great!
Had to make a couple of changes to clear up errors. Seems it doesn't like 3 undifferentiated "groups" (worked in the original query) so I added the 1,2 and 3 to Group, Group, and Group in the original section.
Also, the Order By had to have the alias' FirstName and LastName instead of lname and fname.
If you can think of why I have to have Group1 Group2 Group3 instead of just Group 3x let me know. This will get pasted into a spreadsheet to start with but I may be creating a DTS package for future scheduled runs, in which case I will probably need the "group" by 3x.
Thanks again for your great help and quick response.
-SM
Go to Top of Page

smantz
Starting Member

USA
6 Posts

Posted - 08/02/2012 :  14:36:23  Show Profile  Reply with Quote
I have fixed most everything but am curious; What is the letter t for at the end of the inner query?
I know you have to have it to make it work, but I am not sure why?
Thanks again,
-SM
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47060 Posts

Posted - 08/02/2012 :  14:43:10  Show Profile  Reply with Quote
quote:
Originally posted by smantz

I have fixed most everything but am curious; What is the letter t for at the end of the inner query?
I know you have to have it to make it work, but I am not sure why?
Thanks again,
-SM



t is alias

ie short name you give for the derived table (ie table formed of inner query). it needs to have a name for reference

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

smantz
Starting Member

USA
6 Posts

Posted - 08/02/2012 :  16:35:55  Show Profile  Reply with Quote
Thanks for the info and all the great help.
SM
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47060 Posts

Posted - 08/02/2012 :  16:55:32  Show Profile  Reply with Quote
u're wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000