Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL delete syntax with group by

Posted on 2013-01-21
3
Medium Priority
?
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

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 1500 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

688 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