Re: Using Case colum in where clause
From: Malc (Malc_at_discussions.microsoft.com)
Date: 11/22/04
- Next message: Malc: "Re: Using Case colum in where clause"
- Previous message: Tibor Karaszi: "Re: How to connect as guest?"
- In reply to: Mal .mullerjannie_at_hotmail.com>: "Re: Using Case colum in where clause"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 22 Nov 2004 03:43:04 -0800
Thanks but Hugo came up with that one - it just seems a shame that SQL can't
use a derived column's alias in a where clause. I was wondering if there was
any syntax problem I had - obviously not unfortunately !
"Mal" wrote:
> Ok you dont want to retype the case...
>
> "Mal" wrote:
>
> > Why can't you use this ?
> >
> > select col1, col2,col3
> > -- case
> > -- when col1>col3 then 1
> > -- else 0
> > -- end as A
> > from dummytable a
> > where col2 <> (select case
> > when a.col1>a.col3 then 1
> > else 0 end )
> >
> > "Malc" wrote:
> >
> > > Thx H,
> > > I have used similar to that before but in this case I was wondering if it
> > > could be used in the context:
> > > select
> > > tbl1.col1, tbl1.col2,tbl1.col3
> > > case
> > > when tbl1.col2>tbl1.col3 then 1
> > > else 0
> > > end as A -- I would use a better term - calcvalue or similar
> > >
> > > what I really wanted to do was then use a where clause
> > >
> > > where tbl1.col1 <> A -- calcvalue just used A for ease
> > >
> > > on the basis that the database should have a calculted field col1 that has
> > > a value of 1 if col2> col3
> > >
> > > In prcatoce it is a hell of a lot more complicated, col1 being an overnight
> > > calculated
> > > value and my code is to be run to independantly double check that the
> > > overnight job is correct.. The principle is as above though.
> > > The problem is Hugo that TSQL quite happily shows the name that I give the
> > > case result in the TSQL table but doesn't let me use it.
> > > So....
> > > Can I continue to use CASE in my Select rather than in the FROM
> > > or
> > > If I recode and put it in the FROM...... as A
> > > will it recognise it then and allow me to use it in a where clause ?
> > >
> > > Cheers
> > >
> > >
> > > "Hugo Kornelis" wrote:
> > >
> > > > On Fri, 19 Nov 2004 03:25:04 -0800, Malc wrote:
> > > >
> > > > >I have a case column called A. When I try and use the column A to compare
> > > > >with one of the other columns B it says that A is invalid
> > > > >Invalid column name 'A'
> > > > >I need to list a number of columns from multiple tables, carry out a
> > > > >calculation using the CASE to create a new column and then compare the new
> > > > >column with one on the database.
> > > > >I can get around it by pasting into SQL but ideally I want to return rows
> > > > >where the database value and my CASE column ar not equal.
> > > > >I could of courese repeat all the case information and include a check for
> > > > >the existing colum, but as the case is about 35 lines of code, I'd prefer to
> > > > >use the resulting column instead.
> > > > >Any ideas ?
> > > > >
> > > > >Thx
> > > >
> > > > Hi Malc,
> > > >
> > > > Use a derived table:
> > > >
> > > > SELECT .......
> > > > FROM (SELECT CASE .....
> > > > FROM ......
> > > > WHERE ....) AS x
> > > > WHERE .....
> > > >
> > > > Best, Hugo
> > > > --
> > > >
> > > > (Remove _NO_ and _SPAM_ to get my e-mail address)
> > > >
- Next message: Malc: "Re: Using Case colum in where clause"
- Previous message: Tibor Karaszi: "Re: How to connect as guest?"
- In reply to: Mal .mullerjannie_at_hotmail.com>: "Re: Using Case colum in where clause"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|