May 16th, 2006, 08:12 AM
#1
Thread Starter
Addicted Member
how to get the earlier date in SQL?
hi guys,
hope everyone is doing well.
I have a sql statement that gets records based on country but sometimes I would get two or more records for the same country and I'm interested in the earlier date which is a field in the database.
can someone help please?
thank you,
May 16th, 2006, 08:23 AM
#2
Re: how to get the earlier date in SQL?
Add an and condition to the where clause and perfrom a sub query to get the Min(date).
Sometimes the Programmer
Sometimes the DBA
Mazz1
May 16th, 2006, 09:03 AM
#3
Thread Starter
Addicted Member
Re: how to get the earlier date in SQL?
Originally Posted by
GaryMazzone
Add an and condition to the where clause and perfrom a sub query to get the Min(date).
don't I need to write some kind of IF condition checking for multiple country value then get the earlier date?
I attached a table showing my example. the result should have all countries but min date if douplicate country. please check out and advise.
thank you
Attached Images
May 16th, 2006, 09:11 AM
#4
Re: how to get the earlier date in SQL?
Is all you need is the date and the country? What about the other fields? Do you need those too?
-tg
May 16th, 2006, 09:24 AM
#5
Thread Starter
Addicted Member
Re: how to get the earlier date in SQL?
Originally Posted by
techgnome
Is all you need is the date and the country? What about the other fields? Do you need those too?
-tg
yes techgnome, I need all fields with the condition in my last post.
thank you,
Waely
May 16th, 2006, 10:02 AM
#6
Re: how to get the earlier date in SQL?
Date = (Select Min(Date) From tablename Where CountryName in (Select distinct(CountryName From tableName))
This will probably need to be refined but that is the basics.
Sometimes the Programmer
Sometimes the DBA
Mazz1
May 16th, 2006, 10:27 AM
#7
Re: how to get the earlier date in SQL?
Code:
SELECT D.MinSdate, TN.NofE, TN. [name], TN.country
FROM TableName TN
INNER JOIN (SELECT Min(Sdate) AS MinSdate, Country FROM TableName GROUP BY Country) D
TN.Country = D.Country
See if that gives you what you need....
It's basically a variation of what Gary supplied.
-tg
May 16th, 2006, 12:54 PM
#8
Thread Starter
Addicted Member
Re: how to get the earlier date in SQL?
Originally Posted by
techgnome
Code:
SELECT D.MinSdate, TN.NofE, TN. [name], TN.country
FROM TableName TN
INNER JOIN (SELECT Min(Sdate) AS MinSdate, Country FROM TableName GROUP BY Country) D
TN.Country = D.Country
See if that gives you what you need....
It's basically a variation of what Gary supplied.
-tg
hi tg,
I get this error on the last line " Incorrect syntax near 'TN'" what should i do?
thanks
May 16th, 2006, 12:59 PM
#9
Re: how to get the earlier date in SQL?
Take out the space
Code:
SELECT D.MinSdate, TN.NofE, TN.[name], TN.country
May 16th, 2006, 01:05 PM
#10
Thread Starter
Addicted Member
Re: how to get the earlier date in SQL?
I don't have spaces in my codes plus it says line 4. any ideas?
waely
May 16th, 2006, 01:29 PM
#11
Re: how to get the earlier date in SQL?
I thought I forgot something:
Code:
INNER JOIN (SELECT Min(Sdate) AS MinSdate, Country FROM TableName GROUP BY Country) D
TN.Country = D.Country
should be:
Code:
INNER JOIN (SELECT Min(Sdate) AS MinSdate, Country FROM TableName GROUP BY Country) D
ON TN.Country = D.Country
-tg
May 16th, 2006, 01:52 PM
#12
Thread Starter
Addicted Member
Re: how to get the earlier date in SQL?
tg,
it didn't work, I got douplication of my records. I got the earlier date of them though but douplicated :-(
Last edited by waely; May 16th, 2006 at 02:09 PM .
May 16th, 2006, 02:35 PM
#13
Re: how to get the earlier date in SQL?
I thought that's what you wanted.... or do you only want rows with duplicates?
I'm confused...
-tg
May 16th, 2006, 03:01 PM
#14
Thread Starter
Addicted Member
Re: how to get the earlier date in SQL?
based on my the images attached earlier I have seven records which two of them are dupilcate (US and HUN) and I wanted my final result to be table with 5 records but with
earlier date for these two countries.
I'm sorry if I confused you ...........
May 16th, 2006, 03:17 PM
#15
Re: how to get the earlier date in SQL?
Oh, OK.... then the join needs to be modified a little more:
Code:
INNER JOIN (SELECT Min(Sdate) AS MinSdate, Country FROM TableName GROUP BY Country) D
ON TN.Country = D.Country
AND TN.Sdate = D.MinSdate
-tg
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width