all groups > sql server new users > march 2005 >
You're in the

sql server new users

group:

how to refer to col names?


how to refer to col names? r
3/30/2005 1:27:11 PM
sql server new users:
I am creating a VIEW, and in it I can successfully create a column named
YTD_OSO. The data in this column is created by doing calculations on two
other fields that are money-type (in another test they were float). So
far, so good. See snip of code below:

CASE
WHEN aipdn.D017_all.YTD_TotOrdAmt > 0 then (aipdn.D017_all.YTD_StkOrdAmt /
aipdn.D017_all.YTD_TotOrdAmt * 100)
ELSE 0
END AS YTD_OSO

However, if I add more to try to refer to the value of YTD_OSO any further,
I run into problems. First, if I refer to the column name WITHOUT QUOTES
around it, Query Analyzer tells me that 'YTD_OSO' is an invalid name and
won't make my VIEW.

If I put quotes around the column name, the VIEW is created, but when I try
to view the results I get the message:

"Syntax error converting the varchar value 'YTD_OSO' to a column of data
type int."

I am ASSUMING the YTD_OSO column that is created is of some numeric type
when it's created, being it contains only numbers (money values, actually).
If not, is there some way to force it as a money-type field? or something
numeric? Or am I not correctly referring to the column in my later portion
of the query (see below)?

CASE
WHEN 60 < YTD_OSO AND YTD_OSO < 90 THEN ((YTD_OSO * .01) * 100) - 60
WHEN YTD_OSO > 90 THEN 30
ELSE 0
END AS BASEPOINTS

(I've tried using 'YTD_OSO' (which gives error on run about varchar value
and data type int), and without quotes or using [ ] or # # says YTD_OSO is
an invalid name!

Any idea where the problem is here?

Re: how to refer to col names? r
3/30/2005 2:40:58 PM
Oh my.... so after hours of looking for answers, I finally stumble upon a
thread that discusses how I can't refer to an alias within the same query
because the results haven't yet been calculated. (And there was some
digression about Access and the evils of Microsoft, but that's a story for
another day.)

So I'm left to do this in about 4 query "steps". Is that really taboo? Is
there another way to do this? Or am I doing all I can?

I need to:

1) create a YTD value
2) create a base point value based on what the YTD is
3) create a bonus point based on the YTD AND the base point
4) add the base and bonus points together.

4 query steps sounding right for this kind of a task? I'd do it in my
application, but part of this includes using the resulting list to determine
a "rank" number for everyone in the list. So I really need to see the whole
ball-o-wax here.

SIGH.



[quoted text, click to view]

Re: how to refer to col names? Josh
3/31/2005 9:16:34 AM
[quoted text, click to view]

Have you looked at User defined Functions? Sometimes they can make
complex multistep processes simple. They can be used in queries.

AddThis Social Bookmark Button