Re: the "having" clause
From: Amy (XXXNOSPAMXXX___l.a_at_usa.com)
Date: 03/08/04
- Next message: toylet: "Re: locking a record explicitly"
- Previous message: Tibor Karaszi: "Re: locking a record explicitly"
- In reply to: toylet: "the "having" clause"
- Next in thread: Roji. P. Thomas: "Re: the "having" clause"
- Reply: Roji. P. Thomas: "Re: the "having" clause"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 8 Mar 2004 12:15:53 +0200
Hi toylet,
Logically, The SELECT list is processed after the GROUP BY clause and the
HAVING filter.
So, When SQL Server processes the HAVING clause, the column alias is not yet
'known'.
The only place you can use column aliases you define in the select list is
in the ORDER BY clause which is processed last.
This is not a SQL Server 'freak behaviour', it is by design and supports the
ANSI rules for query processing order.
Search this forum for an article by Celko called 'Select Sequence' in which
he explains the whole thing very clearly.
All you should do is change your query to:
> select xx.pk, xx.amount, count(*) as recno
> from tx xx, tx yy
> where xx.pk>=yy.pk
> group by xx.pk, xx.amount
> having count(*) = 1
HTH
Amy
"toylet" <toylet_at_mail.hongkong.com> wrote in message
news:uHOOlNPBEHA.3472@TK2MSFTNGP09.phx.gbl...
> Why did SQL Server complain about "invalid column recno" in the
> following query? It worked in another database tool I am using.
>
> select xx.pk, xx.amount, count(*) as recno
> from tx xx, tx yy
> where xx.pk>=yy.pk
> group by xx.pk, xx.amount
> having recno = 1
>
> --
> .~. Might, Courage, Vision. In Linux We Trust.
> / v \ http://www.linux-sxs.org
> /( _ )\ Linux 2.4.22-xfs
> ^ ^ 5:52pm up 2 days 2:02 load average: 0.99 0.97 0.96
- Next message: toylet: "Re: locking a record explicitly"
- Previous message: Tibor Karaszi: "Re: locking a record explicitly"
- In reply to: toylet: "the "having" clause"
- Next in thread: Roji. P. Thomas: "Re: the "having" clause"
- Reply: Roji. P. Thomas: "Re: the "having" clause"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|