Ask a Question related to ASP Database, Design and Development.
-
TomB #1
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
-
"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... -
#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... -
#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: ... -
#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... -
#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... -
Bob Barrows [MVP] #2
Re: MIN - returns nothing?
TomB wrote:
Let's examine what this query does:> 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 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
-
TomB #3
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...useful.> 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> 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



Reply With Quote

