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 2005 Forums
 Transact-SQL (2005)
 combining rows

Author  Topic 

amason
Starting Member

7 Posts

Posted - 2007-09-21 : 10:49:43
So I have a table that has address fields made from pulling the address out of a string in another table. I was able to separate out the street1, street2, city, state, zip data into there own columns, and I have the name column filled for each row. Right now it looks like this

Name street1 street2 city state zip
Joe 123maple null null null null
Joe null apt 4 null null null
Joe null null wash. null null
Joe null null null DC null
Joe null null null null 26666

what I want to end up with is a table where the row for Joe would have all of that information in one row, not 5, like this

Name street1 street2 city state zip
Joe 123maple apt 4 wash. DC 21111

How would this be done??

Thanks,
Andrew

nr
SQLTeam MVY

12543 Posts

Posted - 2007-09-21 : 10:57:03
select name, street1 = max(street1 ), street2 = max(street2 ), city = max(city ), state = max(state ), zip = max(zip)
from tbl
group by name


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

amason
Starting Member

7 Posts

Posted - 2007-09-21 : 11:03:23
NICE!!!
worked like a charm. What is this type of function called? I'd like to learn more.
Thanks very much!!
~Andrew
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-21 : 11:26:03
quote:
Originally posted by amason

NICE!!!
worked like a charm. What is this type of function called? I'd like to learn more.
Thanks very much!!
~Andrew



He used the MAX function to return the maximum value in each column. This worked because all the other values were null.





Future guru in the making.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-21 : 11:33:47
"What is this type of function called?"

"Aggregate", but what Nigel has done is using a side effect of the MAX() function, so looking up all the other Aggregate functions may not advance your cause very much

Kristen
Go to Top of Page
   

- Advertisement -