rivkamak
asked on
double case statement
I have a case statement I am trying to use to prioritize the ordering.
How can I add 2 clauses to the case?
When I do this is works:
CASE WHEN state = @stateAb THEN 0 ELSE 1 END
when I try to do this, I don't get it returned state = only
CASE WHEN state = @stateAb and usedForCity = 0 THEN 0 ELSE 1 END
How can I add 2 clauses to the case?
When I do this is works:
CASE WHEN state = @stateAb THEN 0 ELSE 1 END
when I try to do this, I don't get it returned state = only
CASE WHEN state = @stateAb and usedForCity = 0 THEN 0 ELSE 1 END
>when I try to do this, I don't get it returned state = only
Offhand the syntax appears correct, assuming state and @stateAb does not contain a NULL value. If there are NULLs, then you'll have to use the ISNULL function to handle them. Can you post your full T-SQL?
In case it helps, an example of nesting CASE statements is in SQL Server CASE Solutions, do a find on 'CASE blocks can also be nested within themselves'.
>use to prioritize the ordering.
Also, the section below that deals with CASE in an ORDER BY clause.
Offhand the syntax appears correct, assuming state and @stateAb does not contain a NULL value. If there are NULLs, then you'll have to use the ISNULL function to handle them. Can you post your full T-SQL?
In case it helps, an example of nesting CASE statements is in SQL Server CASE Solutions, do a find on 'CASE blocks can also be nested within themselves'.
>use to prioritize the ordering.
Also, the section below that deals with CASE in an ORDER BY clause.
ASKER
select top 100 percent * into #t1 from k4kReviewsForSite2013 ORDER BY
-- First sort position
CASE WHEN state = @stateAb THEN 0 ELSE 1 END
select row_number() over( order by (select 1)) rn, * into #t2
from #T1
--select * from #t1
--select * from #t2
select * from #T2 where rn between @startNum and @endNum --order by idforstate
when I write to the temporary table it works the first way.
when when I add the second clause, i lose the correct ordering.
Define 'the correct ordering'.
ASKER
right ordering is only if state still shows up first.
Perhaps you could post some sample data and your expected results to help clarify your intent for us?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window