Solved

SQL Query syntax question

Posted on 2013-01-30
6
207 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
[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
  • 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
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.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 shrink a transaction log file down to a reasonable size.

626 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