| Author |
Topic |
|
Peter Smith
Starting Member
8 Posts |
Posted - 2009-12-07 : 09:48:41
|
| I have a table[prospects]idcompanynameaddresscityzipcodetotal_carsNow in this table there are some duplicatesid companyname address city zipcode total_cars1 test street NY 54543 NULL2 test street NULL NULL 343 test street NY NULL NULL4 test NULL NULL 54543 34As you can see these 4 rows all contain a little bit of data. I want to merge these rows into 1 row with all available data: test street NY 54543 34The other rows should be deleted.How? And preferably I dont want to have each rowname in my SQL statement (because the table has way more rows than im showing here), I'd like a dynamic statement that goes through all available table rows.Thanks! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-07 : 10:15:01
|
| select companyname, max(address),max(city),max(zipcode), max(total_cars) from your_tablegroup by companynameMadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-12-07 : 10:21:57
|
SIMSALABIM  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-07 : 10:24:40
|
quote: Originally posted by webfred SIMSALABIM  No, you're never too old to Yak'n'Roll if you're too young to die.
What is it? MadhivananFailing to plan is Planning to fail |
 |
|
|
Peter Smith
Starting Member
8 Posts |
Posted - 2009-12-07 : 10:33:17
|
| @madhivanan: that selects a single row. But how do I update a single row with those values and how do I delete the other 3?And what if I'd like to use dynamic column names? Now you explicitly use the column names: max(address),max(city) etc...Thanks again! :) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-12-07 : 10:54:13
|
quote: Originally posted by madhivanan
quote: Originally posted by webfred SIMSALABIM  No, you're never too old to Yak'n'Roll if you're too young to die.
What is it? MadhivananFailing to plan is Planning to fail
It is like Abracadabra - you know?(spell - string of words with supposedly magic power) No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-12-07 : 11:18:05
|
[code]update t1set address = dt.address, city = dt.city,... and so on ...from your_table as t1join (select max(id) as id,companyname, max(address) as address,max(city) as city,max(zipcode) zipcode, max(total_cars) as total_cars from your_tablegroup by companyname)dton dt.id = t1.id[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Peter Smith
Starting Member
8 Posts |
Posted - 2009-12-07 : 12:44:51
|
| Ok, that does the merging trick :)And how do I delete the other duplicate rows after the merge? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-08 : 01:23:59
|
quote: Originally posted by Peter Smith Ok, that does the merging trick :)And how do I delete the other duplicate rows after the merge?
Is this one time update or the table data keeps on changing?MadhivananFailing to plan is Planning to fail |
 |
|
|
eralper
Yak Posting Veteran
66 Posts |
Posted - 2009-12-08 : 15:46:04
|
| Hi Peter,I used the new Merge command as a solution to your problemWith the data you supplied, the below query worked fine for meUpdated only one company record with existing values rather than null onesAlso the dublicate contents are deleted after they are used in the updateYou can find examples at [url]http://www.kodyaz.com/articles/SQL2008-merge-command.aspx[/url]MERGE prospectsUSING ( SELECT max(id) id, companyname, max(address) address, max(city) city, max(zipcode) zipcode, max(total_cars) total_cars FROM prospects GROUP BY companyname ) NewData ON prospects.id = NewData.id WHEN MATCHED THEN UPDATE SET prospects.companyname = NewData.companyname, prospects.address = NewData.address, prospects.city = NewData.city, prospects.zipcode = NewData.zipcode, prospects.total_cars = NewData.total_cars WHEN NOT MATCHED BY SOURCE THEN DELETE;GO-------------Eralperhttp://www.kodyaz.com |
 |
|
|
|