Solved

SQL delete syntax with group by

Posted on 2013-01-21
3
283 Views
Last Modified: 2013-01-21
Hello all,

I have a table that has four fields (Part_No, Qty, Price, DateEntered).  I need to group on everything but DateEntered and I want to first get a select so I can run an insert statement into an archive table for all the records 'except' the max date entered for that grouped records.  

Then I need to delete these records so I only end up with the grouped records with the max date entered.

Thanks for any help.
0
Comment
Question by:sbornstein2
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38801001
you cannot use DELETE and GROUP BY at the same level.

what you can do is to delete all but the one having the MAX() value via subquery:
DELETE t
   FROM yourtable t
  WHERE NOT EXISTS( SELECT NULL FROM yourtable l where l.part_no = t.part_no AND l.dateentered > t.dateentered 

Open in new window


an article about "distinct/group by" is here, it may also help:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 375 total points
ID: 38801007
INSERT ArchiveTable
SELET * FROM [YourTable] t
JOIN
(
SELECT
Part_No
,Qty
,Price
,MAX(DateEntered) [Latest Date Entered]
FROM
[YourTable]
GROUP BY
Part_No
,Qty
,Price
) Latest
ON
t.Part_No = Latest.Part_No
AND t.Qty = Latest.Qty
AND t.Price = Latest.Price
AND t.DateEntered <> Latest.[Latest Date Entered]


DELETE t
FROM [YourTable] t
JOIN
(
SELECT
Part_No
,Qty
,Price
,MAX(DateEntered) [Latest Date Entered]
FROM
[YourTable]
GROUP BY
Part_No
,Qty
,Price
) Latest
ON
t.Part_No = Latest.Part_No
AND t.Qty = Latest.Qty
AND t.Price = Latest.Price
AND t.DateEntered <> Latest.[Latest Date Entered]
0
 

Author Closing Comment

by:sbornstein2
ID: 38801140
thanks this should work well
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question