473,422 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

Problem in HAVING Clause with Date

NeoPa
32,556 Expert Mod 16PB
I have some SQL which runs fine without the HAVING clause, yet when I uncomment either of the HAVING clause versions in the SQL it errors.
Expand|Select|Wrap|Line Numbers
  1. SELECT   TOP 10
  2.          [OH_LEDGER]
  3.         ,CAST(Max([OH_ENTERED_DATE]) AS DateTime) AS [MaxDate]
  4.  
  5. FROM     [Progress].[dbo].[ORDER_HEADERS]
  6.  
  7. GROUP BY [OH_LEDGER]
  8.  
  9. --HAVING   ([MaxDate]<Cast('2010-02-10' AS DateTime))
  10. --HAVING   ([MaxDate]<#02/10/2010#)
  11.  
  12. ORDER BY [MaxDate] DESC
  13. GO
Line #9 gives an error of :
Msg 207, Level 16, State 1, Line 9
Invalid column name 'MaxDate'.


Line #10 gives an error of :
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '#'.

My SQL experience is mainly in Access so I do struggle in areas where that differs from T-SQL. I'm getting there, but I cannot for the life of me, see why this is reacting as it is. It all seems fine to me. Clearly my eyes need some more experience.

If anyone can throw any light then I'd be grateful.
Jul 14 '10 #1
3 2697
NeoPa
32,556 Expert Mod 16PB
It suddenly occurred to me that a problem I thought was Access (Jet) specific, where aliases are not available to reference within the SQL, may be the cause of the non-recognition of [MaxDate] even though it seems to work fine in the ORDER BY clause. This turned out to be the cause of that problem (Line #9). I still have no idea why that would be the case for a HAVING clause but not for the ORDER BY clause.

I still have no idea why ANSI-92 standard date literal representation is causing a problem for line #10.
Jul 14 '10 #2
ck9663
2,878 Expert 2GB
You can't use the alias in any other part of the same SELECT statement. On the HAVING clause, use the entire expression that comprises the MaxDate alias... CAST(Max([OH_ENTERED_DATE]) AS DateTime)

Happy Coding!!!

~~ CK
Jul 15 '10 #3
NeoPa
32,556 Expert Mod 16PB
ck9663: You can't use the alias in any other part of the same SELECT statement.
Hi CK. Thanks for posting.

I'm curious what you mean by the statement quoted. Are you referring to the SQL statement or the SELECT clause?

Sorry to sound picky, but I can't make it work either way I look at it. The alias is working in the ORDER BY clause of the SQL statement, but not in the HAVING clause. I would have thought that it would also work in the SELECT clause, but I haven't tested that (I'm posting from home at the moment). By the way, I did find that your suggested change worked (I tested it yesterday when I posted my second message, but didn't make my meaning very clear in that post).

Did the other part of the problem make any sense to you by the way?
Jul 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Graham Simms | last post by:
I have tried to run a query where the reuslts are restricted by a HAVING clause with 2 conditions, but the second condition seems to be ignored. for example SELECT userid, AVG(position) FROM...
2
by: scottelkin | last post by:
I am trying to find all affiliates that have more commissions from this week to the prior week. The problem is in the having part where "aff2.affiliateid = aff.affiliateid". SQL Server just...
3
by: Jim | last post by:
I'm having a problem with a date query..im trying to pull customer data based on a date specified from a form to 3 months prior to the date specified. So lets say in the form I specified 1/2/2004....
1
by: Dalan | last post by:
I have attempted to resolve a problem regarding erroneous output using a Between And parameter on several Access 97 queries, but to no avail. The queries are used for report output and...
2
by: sanderson82 | last post by:
Hi I am having problems with the HAVING clause. I know it can use aliases but the alias I am using has a space in it, eg 'Device ID' My querry looks like (simplified, devID is a calculation) ...
1
by: G Gerard | last post by:
Hello I am having some problem comparing dates with the SQL statement below MySQL = "SELECT Format(Date, 'yyyy/mmmm/dd') as FROM TblDates WHERE _ Format(Date, 'yyyy/mmmm/dd') =...
3
by: pralaypramanik | last post by:
can a having clause be used withou a corresponding group by??
1
by: vanandwiz | last post by:
Please find the code below. SELECT COMPANY.NAME, COUNT(ITEM.ID) FROM ITEM, COMPANY WHERE ITEM.COMPANYID = COMPANY.ID GROUP BY ROLLUP(COMPANY.NAME)
8
by: rbukkara | last post by:
Hi guys, I have some trouble with the following query. Please look into this and lemme know the solution ASAP. It certainly involves aggregations and the 'having clause' BROKER( ID integer...
3
by: Philia | last post by:
Hello, I'm having problem migrating to postgreSQL from MySQL, some of my queries does not work in postgres. Query(simplified): SELECT t1.bus_stop, t1.time, (case when t1.time >2200 then...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.