Solved

SQL Query syntax question

Posted on 2013-01-30
6
202 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 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

21 Experts available now in Live!

Get 1:1 Help Now