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.
| Author |
Topic |
|
Almir_7
Starting Member
14 Posts |
Posted - 2008-11-12 : 11:10:33
|
| I have the following few rows in the table and need to delete the duplicate rows. The data looks like this.Row Log_id product created_date1 INC1758837 1-Gen 11/4/2008 22:102 INC1332047 AHRS-G 11/1/2008 16:283 INC1765192 AHRS-G 11/5/2008 21:594 INC1765192 AHRS-G 11/6/2008 22:305 INC1772344 1-Gen 11/9/2008 11:03The duplicates are in rows 3 and 4. I need to return rows 1, 2, 4 and 5. The only difference between the rows 3 and 4 is the created_date. I need to return the highest date in case there are more then one duplicate rows in this table. |
|
|
acollins74
Yak Posting Veteran
82 Posts |
Posted - 2008-11-12 : 11:15:54
|
| There are several ways to do it; most more complex than the next but this is my way and seems to be the most staight forward.delete afrom tablename ainner join tablename bon a.log_id = b.log_id and a.product = b.productwhere a.create_date < b.create_date |
 |
|
|
Almir_7
Starting Member
14 Posts |
Posted - 2008-11-12 : 11:27:03
|
| What if there are more then two instances of the same row in there? Such are the row 6 in my sample data bellow. In which case i need to return rows 1, 2, 5 and 6 because the created_date on row 6 is bigger then the created_date in rows 3 and 4 of the same ticket number.Row Log_id product created_date1 INC1758837 1-Gen 11/4/2008 22:102 INC1332047 AHRS-G 11/1/2008 16:283 INC1765192 AHRS-G 11/5/2008 21:594 INC1765192 AHRS-G 11/6/2008 22:305 INC1772344 1-Gen 11/9/2008 11:036 INC1765192 AHRS-G 11/9/2008 11:10 |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-11-12 : 11:27:44
|
| If u are using sqlserver 2005 then u can use thisCREATE TABLE #T1 ( row int, log_id varchar(64), product varchar(64), created_Date datetime )insert into #t1select 1, 'INC1758837', '1-Gen', '11/4/2008 22:10' union allselect 2, 'INC1332047', 'AHRS-G', '11/1/2008 16:28' union allselect 3, 'INC1765192', 'AHRS-G', '11/5/2008 21:59' union allselect 4, 'INC1765192', 'AHRS-G', '11/6/2008 22:30' union allselect 5, 'INC1772344', '1-Gen', '11/9/2008 11:03'delete dfrom #t1 dleft join ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno, row from #t1 ) q on q.row = d.row and q.sno = 1where q.row is null |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 11:32:08
|
quote: Originally posted by raky If u are using sqlserver 2005 then u can use thisCREATE TABLE #T1 ( row int, log_id varchar(64), product varchar(64), created_Date datetime )insert into #t1select 1, 'INC1758837', '1-Gen', '11/4/2008 22:10' union allselect 2, 'INC1332047', 'AHRS-G', '11/1/2008 16:28' union allselect 3, 'INC1765192', 'AHRS-G', '11/5/2008 21:59' union allselect 4, 'INC1765192', 'AHRS-G', '11/6/2008 22:30' union allselect 5, 'INC1772344', '1-Gen', '11/9/2008 11:03'delete dfrom #t1 dleft join ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno, row from #t1 ) q on q.row = d.row and q.sno = 1where q.row is null
no need og join. the below will dodelete qfrom ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno,* from #t1 ) qwhere q.sno > 1 |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-11-12 : 11:35:35
|
quote: Originally posted by visakh16
quote: Originally posted by raky If u are using sqlserver 2005 then u can use thisCREATE TABLE #T1 ( row int, log_id varchar(64), product varchar(64), created_Date datetime )insert into #t1select 1, 'INC1758837', '1-Gen', '11/4/2008 22:10' union allselect 2, 'INC1332047', 'AHRS-G', '11/1/2008 16:28' union allselect 3, 'INC1765192', 'AHRS-G', '11/5/2008 21:59' union allselect 4, 'INC1765192', 'AHRS-G', '11/6/2008 22:30' union allselect 5, 'INC1772344', '1-Gen', '11/9/2008 11:03'delete dfrom #t1 dleft join ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno, row from #t1 ) q on q.row = d.row and q.sno = 1where q.row is null
no need og join. the below will dodelete qfrom ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno,* from #t1 ) qwhere q.sno > 1
thanx for optimization..one more change even * is not required delete qfrom ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno,* from #t1 ) qwhere q.sno > 1 |
 |
|
|
Almir_7
Starting Member
14 Posts |
Posted - 2008-11-12 : 11:39:47
|
| What if there is no row number column in there? How does the query change?Log_id product created_dateINC1758837 1-Gen 11/4/2008 22:10INC1332047 AHRS-G 11/1/2008 16:28INC1765192 AHRS-G 11/5/2008 21:59INC1765192 AHRS-G 11/6/2008 22:30INC1772344 1-Gen 11/9/2008 11:03INC1765192 AHRS-G 11/9/2008 11:10 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 11:43:32
|
quote: Originally posted by Almir_7 What if there is no row number column in there? How does the query change?Log_id product created_dateINC1758837 1-Gen 11/4/2008 22:10INC1332047 AHRS-G 11/1/2008 16:28INC1765192 AHRS-G 11/5/2008 21:59INC1765192 AHRS-G 11/6/2008 22:30INC1772344 1-Gen 11/9/2008 11:03INC1765192 AHRS-G 11/9/2008 11:10
the row_number function generates the number dynamically. No need of presence of seperate column in table with row number value. Refer below for uses of row_number functionhttp://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx |
 |
|
|
Almir_7
Starting Member
14 Posts |
Posted - 2008-11-12 : 11:44:35
|
| Will this function work in SQL 2000? I'm on SQL 2000 database. |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-11-12 : 11:45:31
|
quote: Originally posted by Almir_7 What if there is no row number column in there? How does the query change?Log_id product created_dateINC1758837 1-Gen 11/4/2008 22:10INC1332047 AHRS-G 11/1/2008 16:28INC1765192 AHRS-G 11/5/2008 21:59INC1765192 AHRS-G 11/6/2008 22:30INC1772344 1-Gen 11/9/2008 11:03INC1765192 AHRS-G 11/9/2008 11:10
for the below solution to work just u need log_id, product,created_date columns.The query works eventhough row column doesn't existdelete qfrom ( select row_number() over ( partition by log_id, product order by created_date desc ) as sno from #t1 ) qwhere q.sno > 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 11:48:05
|
quote: Originally posted by Almir_7 Will this function work in SQL 2000? I'm on SQL 2000 database.
Nope. it wont. then use the belowDELETE tFROM YourTable tLEFT JOIN (SELECT Log_id,product,MAX(created_date) AS maxdate FROM YourTable GROUP BY Log_id,product) t1ON t1.Log_id=t.Log_idAND t1.product=t.productAND t1.maxdate=t.created_dateWHERE t1.Log_id IS NULL |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-11-12 : 11:57:40
|
| delete dfrom table dinner join ( select log_id,product,max(created_Date) as cd from table group by log_id, product ) q on q.log_id = d.log_id and q.product = d.product and q.cd > d.created_date |
 |
|
|
Almir_7
Starting Member
14 Posts |
Posted - 2008-11-12 : 12:49:59
|
| Thanks visakh16. Your query works. The problem is i have more then just these 3 columns and when i plug them in all the rows still get returned because i'm not able to group by just the log_id and product anymore. And if i plug in all the columns as bellow then all the rows get returned.SELECT Log_id, Opened_date, closed_date, product_code, specialist_name, customer_name, customer_email, short_description, client_name, call_center, lang_code, family, do_not_survey, employee_id, enterprise_id, customer_country, customer_region, support_organization, assignee_support_group, BPO, Site, Customer_Site, Tier1, Tier2, Tier3, Resolution_category, entry_id, Audit_created_by, audit_status_new, audit_created_date, MAX(Audit_created_date) AS maxdateFROM dbo.WS_Survey_Staging_DBGROUP BY Log_id, Opened_date, closed_date, product_code, specialist_name, customer_name, customer_email, short_description, client_name, call_center, lang_code, family, do_not_survey, employee_id, enterprise_id, customer_country, customer_region, support_organization, assignee_support_group, BPO, Site, Customer_Site, Tier1, Tier2, Tier3, Resolution_category, entry_id, Audit_created_by, audit_status_new, audit_created_dateHow can i work around this problem? |
 |
|
|
Almir_7
Starting Member
14 Posts |
Posted - 2008-11-12 : 12:58:39
|
| Alright so this does work.DELETE stageFROM dbo.WS_Survey_Staging_DB stageLEFT JOIN (SELECT Log_id,product_code,MAX(audit_created_date) AS maxdate FROM dbo.WS_Survey_Staging_DB GROUP BY Log_id,product_code) temp_stageON temp_stage.Log_id=stage.Log_idAND temp_stage.product_code =stage.product_codeAND temp_stage.maxdate=stage.audit_created_dateWHERE temp_stage.Log_id IS NULLBecause the table i join on has only the two columns that are required and the max date and then once i execute the left join on all the columns and the delete then takes care of the rows that don't have a matching row from the other table.Awsome. Thanks for all your help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 13:02:31
|
You're welcome |
 |
|
|
|
|
|
|
|