Solved

SQL delete syntax with group by

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

615 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