all groups > sql server new users > august 2007 >
You're in the

sql server new users

group:

query help


query help Jim in Arizona
8/30/2007 12:26:47 PM
sql server new users:
In the statement(s) below I'm trying query out 'amount' values that are less
than zero (amount > 0) but I can't actually use 'amount' as it isn't
actually a column. So, how would I go about it?

select
amount = (total-paid)
,datedue = dateadd(day, 30, stmtdate)
from iheader
where
total <> paid
and guarantor = 26752
and stmtdate < '2007-07-31'
-- and amount > 0 THIS WILL NOT WORK

TIA,
Jim

Re: query help (RESOLVED) Jim in Arizona
8/30/2007 12:43:21 PM
Actually, the (total-paid) > 0 works for my purposes.

Total > Paid would not have worked for me since I needed to test for the
final amount after paid is subtracted from total. In some cases, it returned
negative values which I needed to filter out.

Thanks Ron!

[quoted text, click to view]

Re: query help Jim in Arizona
8/30/2007 1:23:44 PM
I was getting this error when I tried doing 'and amount > 0'.

Msg 207, Level 16, State 3, Line 1
Invalid column name 'amount'.



[quoted text, click to view]

Re: query help Jim in Arizona
8/30/2007 2:35:13 PM
[quoted text, click to view]

That was informative, Roy. Thanks.
Actually, I now recall reading about execution order in a book I got not too
long ago (ms press, T-SQL Querying 2005). Perhaps it's time for me to
revisit my book(s).. :)

Re: query help Roy Harvey
8/30/2007 3:36:56 PM
Well you could simply say:
and (total-paid) > 0

but since you already test for:
[quoted text, click to view]

why not simply test for
total > paid

which sure looks like what you are trying to do anyway.

Roy Harvey
Beacon Falls, CT

On Thu, 30 Aug 2007 12:26:47 -0700, "Jim in Arizona"
[quoted text, click to view]
Re: query help Roy Harvey
8/30/2007 4:04:24 PM
On Thu, 30 Aug 2007 22:49:57 +0300, Ekrem Önsoy <ekrem@btegitim.com>
[quoted text, click to view]

It passes a syntax check because, without reference to the underlying
tables, the optimizer is unaware that there is no column named amount
in the table being queried. But it will not execute.

Roy Harvey
Re: query help Roy Harvey
8/30/2007 5:19:40 PM
On Thu, 30 Aug 2007 13:23:44 -0700, "Jim in Arizona"
[quoted text, click to view]

That is expected at execution time. The WHERE cause is evaluated
before any SELECT list columns have been created. All the WHERE
clause has available are actual column names, not calculated values
from the SELECT list If you have a calculation in the SELECT list you
have to repeat the calculation in the WHERE clause rather than
reference it by the alias.

But if you simply use the option to check the syntax, which is all
someone without the tables in place can do, it will pass the syntax
check. For all the optimizer knows the table could have a column
named "amount".

Roy Harvey
Re: query help Ekrem_Önsoy
8/30/2007 10:49:57 PM
Following code works in my environment.

"and amount > 0" included

--
Ekrem Önsoy
MCBDA, MCTS: SQL Server 2005, MCSD.Net, MCSE, MCT



[quoted text, click to view]
AddThis Social Bookmark Button