Solved

SQL - SELECTING RECORDS USING MAX

Posted on 2003-11-07
4
251 Views
Last Modified: 2010-04-17
ACCESS 2000.
I have a table(SALEDATE) contains SALESNUMBER,SALESDATE. Each SALESNUMBER has multiple dates. I want to delete all dates associcated with a SALESNUMBER except the earliest date. Ex:
SALESNUMBER 23466 1/2/2003
                      23466 1/9/2003
                      23466 1/16/2003
I want to keep 1/2/2003. I tried to use MAX unsuccessfully. Any help would be appreciated.
0
Comment
Question by:dxrodela
4 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 9703011
I assume by having multiple dates you mean it has multiple records.

Try
Select SALESNUMBER, min(SALESDATE) as MinSaleDate from SALEDATE Group By SALESNUMBER

or
Select TOP 1 SALESNUMBER, SALESDATE from SALEDATE
GROUP BY SALESNUMBER
ORDER BY SALESDATE ASC

mlmcc
0
 
LVL 3

Expert Comment

by:merphle
ID: 9703485
If you want to actually delete the rows from your table:

delete from SALEDATE sd1
where sd1.SALESDATE <> (select min(sd2.SALESDATE) from SALEDATE sd2 where sd2.SALESNUMBER = sd1.SALESNUMBER)
0
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 125 total points
ID: 9705988
Hello,

Try this,


DELETE FROM SALEDATE
FROM SALEDATE T1
WHERE SALESDATE   <>  (   SELECT MIN( SALESDATE )
                FROM SALEDATE
                WHERE T1.SALESNUMBER = SALESNUMBER )

V.Thandava Krishna.
0
 
LVL 1

Expert Comment

by:Mustak_Shaikh
ID: 9710724
dxrodela,

The following query will definitely work:

delete from saledate
where salesnumber&'|'&salesdate
not in (select salesnumber&'|'&min(salesdate) from saledate group by salesnumber)


Mustak_Shaikh
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 56
oracle query help 18 99
Looking for example pivot year code used in Y2K 4 61
Please help| Getting the syntax error in below query. 2 45
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

20 Experts available now in Live!

Get 1:1 Help Now