Solved

SQL - SELECTING RECORDS USING MAX

Posted on 2003-11-07
4
239 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

759 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

19 Experts available now in Live!

Get 1:1 Help Now