Solved

SQL Query syntax question

Posted on 2013-01-30
6
201 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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…
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 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

17 Experts available now in Live!

Get 1:1 Help Now