Solved

SQL Query syntax question

Posted on 2013-01-30
6
205 Views
Last Modified: 2013-01-30
Hello all,

I am trying to clean up a table that was bulk loaded with a bunch of duplicate records and I wanted to only keep the MAX(DATEOFFERRED) of the grouped records in the query below.   The problem is the MAX(DATE_OFFERRED) has multiple of the same DATEOFFERRED date.  So what I want to do is only keep the first of the max date offerred grouped records.   Can anyone assist with this?

SELET count(*) FROM Availables t
 JOIN
 (
 SELECT
 Part_No
 ,Qty
 ,Price
 ,MAX(DateOfferred) [Latest Date Offerred]
 FROM
Availables
 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.DateOfferred <> Latest.[Latest Date Offerred]
0
Comment
Question by:sbornstein2
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38838003
what is your sqlserver version?
0
 

Author Comment

by:sbornstein2
ID: 38838009
SQL 2005 for this server
0
 
LVL 39

Accepted Solution

by:
appari earned 225 total points
ID: 38838010
if your SQL version is above 2005 you can try this

Select * from (
Select *, row_number() over(partition by  Part_No  ,Qty  ,Price order by DateOfferred desc) rowID
 FROM Availables) A where A.rowID = 1

Open in new window

0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 

Author Comment

by:sbornstein2
ID: 38838046
How do I get a count of everything but the max records as I want to get an idea how many I can delete.  This is my latest query I had to add more fields for the grouping,

Select count(*) from (
Select *, row_number() over(partition by  Part_No, MF, DC, Qty, Price, CO_ID order by DateOfferred desc) rowID
 FROM Availables) A where A.rowID = 1
0
 

Author Comment

by:sbornstein2
ID: 38838053
So my goal it to get a count of the records I will end up deleting then run a delete statement to delete all the non row desc records.   I only want to delete of course the records there is more than one though as well so if there is only one record I don't want to delete those.
0
 

Author Closing Comment

by:sbornstein2
ID: 38838256
thanks this worked well
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

726 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