Having 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

SQL Server Forums - mass delete with Group By criteria

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=160299

SQL Download Security, Caching, Auditing&Masking Real-time Protection, Try Now!

Download.GreenSQL.com/free-trial www.synametrics.com/

JaySQL by Synametrics A Must-Have tool for database programmers and administrators UDID Activation - iOS 5.0 UDID Activations - ios 5.0 registrations are activated today.

www.udidfast.com

Home | Weblogs | Forums | SQL Server Links

Search:
Active Forum Topics | Popular Articles | All Articles by Tag | SQL Server Books | About

Site Sponsored By: TraceTune.com - The online version of ClearTrace. Quickly identify which SQL statements use the most CPU and disk.

SQL Server Forums


Profile | Register | Active Topics | Members | Search | Forum FAQ

Username:

Password:

Save Password

Register Now and get your question answered!


All Forums SQL Server 2008 Forums Transact-SQL (2008) mass delete with Group By criteria Author McBeef
Starting Member 8 Posts

Forgot your Password?

New Topic Reply to Topic Printer Friendly Topic

Posted - 05/05/2011 : 15:22:51

I have a transaction log table called WORKDONE that consists of the columns SERIALNUMBER, WORKDONE, and DATESTAMP. I also made a "group by" query of WORKDONE that groups and does a count for each combination of those three. The query is called qryDailyCount and here is the SQL for the query (FirstWord is a function that just takes the first word, in this case the date portion, ignoring the time): SELECT SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) AS DATESTAMP, COUNT(*) AS QTY FROM dbo.WORKDONE GROUP BY SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) What I'd like to do is have a SQL statement that would a the records from WORKDONE based on the QTY from qryDailyCount. For example to delete all records from WORKDONE if their qryDailyCount QTY > 4. To restate if a serial number has more than 4 records of a given work on a given day, then all records of that serial-work-day combination will be deleted. It would be possible to look at the results of qryDailyCount and write a number of delete statements based on that, but it would be lot easier if there way to accomplish the same automatically with a single statement.

jimf
Flowing Fount of Yak Knowledge USA 2367 Posts

Posted - 05/05/2011 : 15:27:36

DELETE FROM Workdone w INNER JOIN (SELECT SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) AS DATESTAMP, COUNT(*) AS QTY FROM dbo.WORKDONE GROUP BY SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) HAVING COUNT(*) > 4 ) t on w.serialnumber = t.serialnumber Jim Test this a select statement first! Everyday I learn something that somebody else already knew

ms65g
Constraint Violating Yak Guru

Posted - 05/05/2011 : 15:32:00

quote:
497 Posts

Originally posted by jimf DELETE FROM Workdone w INNER JOIN (SELECT SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) AS DATESTAMP, COUNT(*) AS QTY FROM dbo.WORKDONE GROUP BY SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) HAVING COUNT(*) > 4 ) t on w.serialnumber = t.serialnumber Jim Test this a select statement first! Everyday I learn something that somebody else already knew

Is your DELETE statement correct? But seems like folowing is correct:

1 de 2

31/01/2012 11:02 p.m.

SQL Server Forums - mass delete with Group By criteria

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=160299

______________________ McBeef
Starting Member 8 Posts

Posted - 05/05/2011 : 17:03:18

I tried the delete statement. It deletes all records for those serial numbers and I wanted to only delete those records with that serialwork-date combination. For example, here is qryCountDay results for serial # 54321: 54321 54321 54321 54321 54321 54321 43" CHANNEL 3/14/2011 154322 CORNER 3/14/2011 4 B PANEL 3/14/2011 2 SLIDE 3/14/2011 2 19" CHANNEL 3/14/2011 4 Re-Weld 4/12/2011 1

after I run the delete, all 54321 records are deleted from the system, good and bad alike. Only want to delete 54321 records that 43" CHANNEL on 3/13/2011 (for which there 154322 records in WORKDONE). I want to leave the remaining good 54321 records untouched. McBeef
Starting Member 8 Posts

Posted - 05/05/2011 : 17:12:54

This appears to work: DELETE FROM w FROM dbo.WORKDONE w INNER JOIN (SELECT SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) AS DATESTAMP, COUNT(*) AS QTY FROM dbo.WORKDONE GROUP BY SERIALNUMBER, WORKDONE, dbo.FirstWord(DATESTAMP) HAVING COUNT(*) > 4 ) t on w.SERIALNUMBER = t.SERIALNUMBER and w.WORKDONE = t.WORKDONE and dbo.FirstWord(w.DATESTAMP) = t.DATESTAMP

sunitabeck
Flowing Fount of Yak Knowledge USA 2234 Posts

Posted - 05/05/2011 : 17:15:56

Join on whichever criteria you want to delete on. For example, if you wanted to delete regardless of the date, but only those that match workdone, then in the outer join, use

If you want to delete only those that match the serial number, workdone AND date, then join on all three as in

Topic New Topic Reply to Topic Printer Friendly


SQL Server Forums This page was generated in 0.05 seconds.

Jump To:
2000-2009 SQLTeam Publishing, LLC

2 de 2

31/01/2012 11:02 p.m.

You might also like