Need help on SQL using date

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Need help on SQL using date

    Hi to all the sifu,
    i have this table that have "Type" and "Date".
    Data are store as
    Type Date
    CP 20 Mar 2003
    CF 22 Mar 2003
    EX 20 Mar 2003
    CP 19 Mar 2003
    EX 18 Mar 2003

    im trying to create a query for data which is Date <= 22 Mar 2003
    and is distinct by Type and Date
    the result that i want is

    Type Date
    CP 20 Mar 2003
    CF 22 Mar 2003
    EX 20 Mar 2003

    The nearest i can get is to do a query distinct by Type
    then loop one by one on this query
    "Select top 1 Type, Date from tbl
    where Type = 'loop of the distinct type'
    order by date desc"

    any sifu can help ?
    thanks
    Spike Guest

  2. Similar Questions and Discussions

    1. JSObject returns wrong date. How can Iextract correct date from digital signature?
      I'm trying to extract name and date from digital signatures by using JSObject in Excel VBA, but JSObject returns wrong date. Year, month, hour and...
    2. #39245 [NEW]: date function generate wrong date with 1162083600 timestamp
      From: lohner at aldea dot hu Operating system: Linux PHP version: 5.1.6 PHP Bug Type: Date/time related Bug description: ...
    3. converting date into database date format(newbie)
      Hi! U can convert "8-Aug-03" into mysql date which requires yyyy-mm-dd format as below. <?php date("Y-m-d",strtotime("8-Aug-03")); ?>
    4. How do I manipulate a date variable to a specific date array?
      Hi, I use the getdate() function to return today's date in an array. I do this as I need to separate the day/month/year as to display them in a...
  3. #2

    Default Re: Need help on SQL using date

    This was almost a very well-posed question. The only information missing is:
    What is the type and version of the database you are using?
    What is the datatype of the "Date" column? The format is irrelevant unless
    it's a character field.

    Bob Barrows

    Spike wrote:
    > Hi to all the sifu,
    > i have this table that have "Type" and "Date".
    > Data are store as
    > Type Date
    > CP 20 Mar 2003
    > CF 22 Mar 2003
    > EX 20 Mar 2003
    > CP 19 Mar 2003
    > EX 18 Mar 2003
    >
    > im trying to create a query for data which is Date <= 22 Mar 2003
    > and is distinct by Type and Date
    > the result that i want is
    >
    > Type Date
    > CP 20 Mar 2003
    > CF 22 Mar 2003
    > EX 20 Mar 2003
    >
    > The nearest i can get is to do a query distinct by Type
    > then loop one by one on this query
    > "Select top 1 Type, Date from tbl
    > where Type = 'loop of the distinct type'
    > order by date desc"
    >
    > any sifu can help ?
    > thanks


    Bob Barrows Guest

  4. #3

    Default Re: Need help on SQL using date

    Hi Bob,
    Im using MSSQL 7.
    The "Date" is a datetime format which is 3/20/2003,
    i put in 20 march 2003 is for easy understand.
    will this info help?
    thanks

    "Bob Barrows" <reb_01501@yahoo.com> wrote in message news:<eorEt$DfDHA.616@TK2MSFTNGP11.phx.gbl>...
    > This was almost a very well-posed question. The only information missing is:
    > What is the type and version of the database you are using?
    > What is the datatype of the "Date" column? The format is irrelevant unless
    > it's a character field.
    >
    > Bob Barrows
    >
    > Spike wrote:
    > > Hi to all the sifu,
    > > i have this table that have "Type" and "Date".
    > > Data are store as
    > > Type Date
    > > CP 20 Mar 2003
    > > CF 22 Mar 2003
    > > EX 20 Mar 2003
    > > CP 19 Mar 2003
    > > EX 18 Mar 2003
    > >
    > > im trying to create a query for data which is Date <= 22 Mar 2003
    > > and is distinct by Type and Date
    > > the result that i want is
    > >
    > > Type Date
    > > CP 20 Mar 2003
    > > CF 22 Mar 2003
    > > EX 20 Mar 2003
    > >
    > > The nearest i can get is to do a query distinct by Type
    > > then loop one by one on this query
    > > "Select top 1 Type, Date from tbl
    > > where Type = 'loop of the distinct type'
    > > order by date desc"
    > >
    > > any sifu can help ?
    > > thanks
    Spike Guest

  5. #4

    Default Re: Need help on SQL using date

    Yes, this information helps. Unfortunately, there are a few more questions I
    now realize I should have asked now that I've reread your question.

    You have two "CP" records in your sample data. In your desired result, you
    indicate that you want the 20 Mar 2003 date returned. How did you decide
    that this was the date you wanted to be returned. Is it because this is the
    later of the two dates? If so, that's easy to do:
    select Type, max([Date]) from table group by Type

    Or is it (as I suspect from your loop suggestion) because it's the "first"
    date you showed in your sample data? If so, you are not going to be able to
    achieve this unless you have another column in your table that provides the
    order of entry. Don't forget, in a relational database, a table is defined
    as a "set of unordered rows of data". This means: just because the row with
    20 Mar 2003 may have been entered into the table before the row with 19 Mar
    2003, this does not mean that the data is stored in that order on the disk,
    or that the data will be returned in that order by a select statement
    without an ORDER BY clause. So even if you did attempt a cursor-based
    (looping) solution, there is no guarantee that the process would give you
    your desired results, unless you can order the rows being processed by a
    third column that indicates the order of entry.

    So, lets' assume you have an IDENTITY column, or better, a datetime column
    that contains the date and time of the row's entry. Here is how you would
    utilize that column:

    Select t.Type,[Date] From table t inner join
    (Select Type, min(EntryOrderColumn) As FirstEntry
    From table
    Group By Type) As q
    on t.Type = q.Type AND t.EntryOrderColumn = q.FirstEntry

    HTH,
    Bob Barrows



    Spike wrote:
    > Hi Bob,
    > Im using MSSQL 7.
    > The "Date" is a datetime format which is 3/20/2003,
    > i put in 20 march 2003 is for easy understand.
    > will this info help?
    > thanks
    >
    > "Bob Barrows" <reb_01501@yahoo.com> wrote in message
    > news:<eorEt$DfDHA.616@TK2MSFTNGP11.phx.gbl>...
    >> This was almost a very well-posed question. The only information
    >> missing is:
    >> What is the type and version of the database you are using?
    >> What is the datatype of the "Date" column? The format is irrelevant
    >> unless
    >> it's a character field.
    >>
    >> Bob Barrows
    >>
    >> Spike wrote:
    >>> Hi to all the sifu,
    >>> i have this table that have "Type" and "Date".
    >>> Data are store as
    >>> Type Date
    >>> CP 20 Mar 2003
    >>> CF 22 Mar 2003
    >>> EX 20 Mar 2003
    >>> CP 19 Mar 2003
    >>> EX 18 Mar 2003
    >>>
    >>> im trying to create a query for data which is Date <= 22 Mar 2003
    >>> and is distinct by Type and Date
    >>> the result that i want is
    >>>
    >>> Type Date
    >>> CP 20 Mar 2003
    >>> CF 22 Mar 2003
    >>> EX 20 Mar 2003
    >>>
    >>> The nearest i can get is to do a query distinct by Type
    >>> then loop one by one on this query
    >>> "Select top 1 Type, Date from tbl
    >>> where Type = 'loop of the distinct type'
    >>> order by date desc"
    >>>
    >>> any sifu can help ?
    >>> thanks


    Bob Barrows Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139