MIN - returns nothing?

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

  1. #1

    Default MIN - returns nothing?

    SELECT MIN(StartTime)
    FROM tblLocationBookings
    HAVING (MIN(StartTime) > CONVERT(DATETIME, '4/20/2004 10:30:00 AM',
    102))

    I expect the above to return the next "Booking" after 10:30 am on 4/20/2004.
    It returns nothing
    There is data in the table; 4 of which are on 4/20/2004 with Start Times
    after 10:30. Is there something about smalldatetime, I don't know? Or am I
    doing something completely wrong?
    The purpose (if you care) is to populate a <select> with all the times
    available up to the next booking.


    CREATE TABLE [dbo].[tblLocationBookings] (
    [ID] [int] IDENTITY (1, 1) NOT NULL ,
    [LocationID] [int] NOT NULL ,
    [StartTime] [smalldatetime] NOT NULL ,
    [EndTime] [smalldatetime] NULL ,
    [Description] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [BookerID] [int] NULL
    ) ON [PRIMARY]
    GO

    Thanks
    TomB



    TomB Guest

  2. Similar Questions and Discussions

    1. "make Makefile.PL" returns "...up to date"; make returns "no target to make"
      I'm attempting to install a perl module (AppConfig-1.56) on a FreeBSD 4.9 system. It has both perl 5.5.3 and 5.8.3 and several modules are already...
    2. #26132 [Bgs]: pg_fetch_object returns NULL on serial and INT in record but returns strings NP
      ID: 26132 User updated by: steven at pearavenue dot com Reported By: steven at pearavenue dot com Status: Bogus Bug...
    3. #26132 [Opn->Bgs]: pg_fetch_object returns NULL on serial and INT in record but returns strings NP
      ID: 26132 Updated by: iliaa@php.net Reported By: steven at pearavenue dot com -Status: Open +Status: ...
    4. #26132 [Opn]: pg_fetch_object returns NULL on serial and INT in record but returns strings NP
      ID: 26132 User updated by: steven at pearavenue dot com Reported By: steven at pearavenue dot com Status: Open Bug...
    5. #26132 [NEW]: pg_fetch_object returns NULL on serial and INT in record but returns strings NP
      From: steven at pearavenue dot com Operating system: Redhat 9.0/Apache 2.0 PHP version: 4.3.4 PHP Bug Type: PostgreSQL...
  3. #2

    Default Re: MIN - returns nothing?

    TomB wrote:
    > SELECT MIN(StartTime)
    > FROM tblLocationBookings
    > HAVING (MIN(StartTime) > CONVERT(DATETIME, '4/20/2004 10:30:00
    > AM', 102))
    >
    > I expect the above to return the next "Booking" after 10:30 am on
    > 4/20/2004.
    Let's examine what this query does:

    It first calculates the MIN(StartTime). It then compares that calculated
    value to '4/20/2004 10:30:00 AM' (the CONVERT is not necessary - but date
    literals should be supplied in YYYYMMDD format to avoid ambiguity).

    So in this case, the min(StartTime) is likely to be less than the compared
    datetime in your HAVING clause, so nothing is returned.

    What you really want to do is filter the data used to calculate the min
    value BEFORE the min value is calculated. That means you need to use the
    WHERE clause:

    SELECT MIN(StartTime)
    FROM tblLocationBookings
    WHERE StartTime > '20040420 10:30:00 AM'


    WHERE filters before aggregates are calculated. HAVING filters AFTER
    aggregates are calculated. Here is an example where HAVING would be useful.
    Say yhou wanted to see booking starttimes by location, but only for
    locations whose next booking is after '20040420 10:30:00 AM'. This query:

    SELECT LocationID,MIN(StartTime)
    FROM tblLocationBookings
    GROUP BY LocationID

    Might produce these results:
    1 2/25/2004 9:30 AM
    2 4/21/2004 9:30 AM
    3 4/1/2004 10:30 AM
    4 3/29/2004 9:00 AM

    This modification:
    SELECT LocationID,MIN(StartTime)
    FROM tblLocationBookings
    GROUP BY LocationID
    HAVING Min(StartTime) > '20040420 10:30:00 AM'

    will yield this result:
    2 4/21/2004 9:30 AM

    HTH,
    Bob Barrows


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  4. #3

    Default Re: MIN - returns nothing?

    Thanks. I had no idea why there even was a Having since it "seemed" to do
    the same as Where.

    Your explanation, explains why my query was working on some tests but not
    others.

    Thanks again.

    Tom B

    "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
    news:%239baYauJEHA.2776@TK2MSFTNGP12.phx.gbl...
    > TomB wrote:
    > > SELECT MIN(StartTime)
    > > FROM tblLocationBookings
    > > HAVING (MIN(StartTime) > CONVERT(DATETIME, '4/20/2004 10:30:00
    > > AM', 102))
    > >
    > > I expect the above to return the next "Booking" after 10:30 am on
    > > 4/20/2004.
    >
    > Let's examine what this query does:
    >
    > It first calculates the MIN(StartTime). It then compares that calculated
    > value to '4/20/2004 10:30:00 AM' (the CONVERT is not necessary - but date
    > literals should be supplied in YYYYMMDD format to avoid ambiguity).
    >
    > So in this case, the min(StartTime) is likely to be less than the compared
    > datetime in your HAVING clause, so nothing is returned.
    >
    > What you really want to do is filter the data used to calculate the min
    > value BEFORE the min value is calculated. That means you need to use the
    > WHERE clause:
    >
    > SELECT MIN(StartTime)
    > FROM tblLocationBookings
    > WHERE StartTime > '20040420 10:30:00 AM'
    >
    >
    > WHERE filters before aggregates are calculated. HAVING filters AFTER
    > aggregates are calculated. Here is an example where HAVING would be
    useful.
    > Say yhou wanted to see booking starttimes by location, but only for
    > locations whose next booking is after '20040420 10:30:00 AM'. This query:
    >
    > SELECT LocationID,MIN(StartTime)
    > FROM tblLocationBookings
    > GROUP BY LocationID
    >
    > Might produce these results:
    > 1 2/25/2004 9:30 AM
    > 2 4/21/2004 9:30 AM
    > 3 4/1/2004 10:30 AM
    > 4 3/29/2004 9:00 AM
    >
    > This modification:
    > SELECT LocationID,MIN(StartTime)
    > FROM tblLocationBookings
    > GROUP BY LocationID
    > HAVING Min(StartTime) > '20040420 10:30:00 AM'
    >
    > will yield this result:
    > 2 4/21/2004 9:30 AM
    >
    > HTH,
    > Bob Barrows
    >
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >

    TomB 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