Solved

SQL delete syntax with group by

Posted on 2013-01-21
3
280 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now