Connecting Tech Pros Worldwide Help | Site Map

CASE alias in WHERE

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 04:09 AM
A.V.C.
Guest
 
Posts: n/a
Default CASE alias in WHERE

Hello,

I found members of this group very helpful for my last queries.
Have one problem with CASE. I can use the column name alias in Order By Clause
but unable to use it in WHERE CLAUSE.
PLS TELL ME IF IT IS POSSIBLE TO USE IT IN WHERE CLAUSE AND SOME ALTERNATIVE.

QUERY:

SELECT
M.SECS =
CASE
WHEN NO_OF_SEC IS NULL THEN -1
WHEN NO_OF_SEC =0 THEN 1
ELSE NO_OF_SEC
END
FROM DOWNLOAD_MASTER M
WHERE M.SECS < 100
ORDER BY M.SECS



Hoping for a immediate reply.
thanks in advance

  #2  
Old July 20th, 2005, 04:09 AM
David Portas
Guest
 
Posts: n/a
Default Re: CASE alias in WHERE

References to column aliases are only valid in the ORDER BY clause. You can
work around this by putting the expression into a derived table:

SELECT secs
FROM
(SELECT secs =
CASE
WHEN no_of_sec IS NULL THEN -1
WHEN no_of_sec = 0 THEN 1
ELSE no_of_sec
END
FROM DOWNLOAD_MASTER) AS M
WHERE secs < 100
ORDER BY secs

--
David Portas
SQL Server MVP
--


  #3  
Old July 20th, 2005, 04:09 AM
Hugo Kornelis
Guest
 
Posts: n/a
Default Re: CASE alias in WHERE

On 5 Jul 2004 04:02:38 -0700, A.V.C. wrote:
[color=blue]
>Hello,
>
>I found members of this group very helpful for my last queries.
>Have one problem with CASE. I can use the column name alias in Order By Clause
>but unable to use it in WHERE CLAUSE.
>PLS TELL ME IF IT IS POSSIBLE TO USE IT IN WHERE CLAUSE AND SOME ALTERNATIVE.
>
>QUERY:
>
>SELECT
>M.SECS =
> CASE
> WHEN NO_OF_SEC IS NULL THEN -1
> WHEN NO_OF_SEC =0 THEN 1
> ELSE NO_OF_SEC
> END
>FROM DOWNLOAD_MASTER M
>WHERE M.SECS < 100
>ORDER BY M.SECS
>
>
>
>Hoping for a immediate reply.
>thanks in advance[/color]

Hi A.V.C.,

Before answering your question, one remark about your query. I advise you
to remove "M." before "SECS". You are using an alias; not a column name.
The name "M.SECS" looks as if you're referring to a column named SECS in
the table named (or aliased) M. I expect the above query to throw an error
because column SECS can't be found in the table DOWNLOAD_MASTER. The order
by clause will probably not throw an error, but that is only because table
names (or aliases) are largely mostly disregarded by SQL Server when
evaluating an roder by clause.

To answer your question: no, this is not possible. To understand why, it
helps to know how an SQL query gets evaluated. Note that this is a
conceptual description; a good RDBMS will change the order of operation to
optimize; as long as the results remain the same that is not a problem.

Step 1: Evaluate FROM clause, build intermediate table from all rows in
the tables used, joined together on the conditions given. If old style
join syntax is used (with the ON conditions in the WHERE clause), this
step will yield the full carthesian product of all tables used.

Step 2: Evaluate WHERE clause, remove rows that don't match the criteria
from intermediate table.

Step 3: Evaluate GROUP BY clause, group rows together according to the
specified arguments.

Step 4: Evaluate HAVING clause, remove groups that don't match the
criteria from intermediate table.

Step 5: Evaluate SELECT clause, build result set to be returned from the
data in the intermediate table.

Step 6: Evaluate ORDER BY, perform sorting.

Officially, columns that are not included in the SELECT clause are not
available for sorting. Many products (like SQL Server) do allow this, but
it is a non-standard extension of the ISO/ANSI SQL-92 specification (and I
don't think that later SQL specifications included this).

Since the alias of a columns or expression is only effective from step 5
but the WHERE clause is evaluated as step 2, it is clear that an alias
can't be used in the WHERE clause.


You also ask for alternatives. In your case, you could try either "WHERE
COALESCE(NO_OF_SEC, -1) < 100" or "WHERE NO_OF_SEC < 100 OR NO_OF_SEC IS
NULL". In more complex cases, you might have to repeat the CASE expression
in the WHERE clause. If you dislike that redundancy, you can always use
the derived table approach. For your query, the equivalent with a derived
table would look like this:

SELECT SECS
FROM (SELECT SECS = CASE
WHEN NO_OF_SEC IS NULL THEN -1
WHEN NO_OF_SEC = 0 THEN 1
ELSE NO_OF_SEC
END
FROM DOWNLOAD_MASTER) AS D
WHERE SECS < 100
ORDER BY SECS
(untested)


Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
  #4  
Old July 20th, 2005, 04:09 AM
A.V.C.
Guest
 
Posts: n/a
Default Re: CASE alias in WHERE

Thank you David Portas and Hugo Kornelis

I agree with the point(M.) mentioned by Hugo Kornelis.
I appreciate your way of writing descriptive answers.

Thanks once again.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.