Results 1 to 15 of 15

Thread: how to get the earlier date in SQL?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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,

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to get the earlier date in SQL?

    Quote 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 Attached Images  

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to get the earlier date in SQL?

    Quote 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

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: how to get the earlier date in SQL?

    Quote 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

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to get the earlier date in SQL?

    Take out the space
    Code:
    SELECT D.MinSdate, TN.NofE, TN.[name], TN.country

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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.

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    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 ...........

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width