Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

> in Select Case

Status
Not open for further replies.

Kliot

Programmer
Jan 10, 2003
622
US
I have a question about using greater and less than in a case statement.


CASE value
WHEN > 10 THEN Something
WHEN < 10 THEN SomethingElse
...
END

This doesn't work. The "> <" is not valid syntax, the following statement would work:

CASE
WHEN value > 10 THEN Something
WHEN value < 10 THEN SomethingElse
...
END

My Question is "value" is actually a query and it doesn't seem right to me that I should repeat the rather complex query for every wHEN clause. What's the best way to handle this?

Thanks
 
Use CROSS APPLY technique (SQL Server 2005 and up), e.g.
Code:
select SomeFields, CASE when F.Result > 10 then ... when ... end

from myTable
CROSS APPLY (Some Query goes here related to the main table) F

See example of CROSS APPLY (although I think INNER JOIN in this case is better solution - or may be the same, I'll explore later):




PluralSight Learning Library
 
Thanks for the tip, I'm not familiar with Cross Apply, I'll give it a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top