Professional Documents
Culture Documents
Having 2
Having 2
Having 2
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=160299
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
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.
Username:
Password:
Save Password
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
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
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
1 de 2
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=160299
______________________ McBeef
Starting Member 8 Posts
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
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
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
Jump To:
2000-2009 SQLTeam Publishing, LLC
2 de 2