Solved

double case statement

Posted on 2014-04-07
7
356 Views
Last Modified: 2014-05-07
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
0
Comment
Question by:rivkamak
7 Comments
 
LVL 6

Expert Comment

by:Dulton
Comment Utility
Try nesting the case statements.... what you're trying works in later versions of SQL, not familiar with 2005 enough to say if this was added later. the below should work though.
case when state=@stateAb THEN CASE WHEN usedForCity=0 THEN 0 ELSE 1 END ELSE 1 END

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>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.
0
 

Author Comment

by:rivkamak
Comment Utility
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

Open in new window


when I write to the temporary table  it works the first way.
when when I add the second clause, i lose the correct ordering.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Define 'the correct ordering'.
0
 

Author Comment

by:rivkamak
Comment Utility
right ordering is only if state still shows up first.
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
Perhaps you could post some sample data and your expected results to help clarify your intent for us?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
It may depend on nulls in the usedForCity column, try these

-- if usedForCity is null send it downward in ordering
CASE WHEN state = @stateAb and ISNULL(usedForCity,1) = 0 THEN 0 ELSE 1 END

or

-- if usedForCity is null send it upward in ordering
CASE WHEN state = @stateAb and ISNULL(usedForCity,0) = 0 THEN 0 ELSE 1 END


If state can be null also then more may be required, e.g.

-- if usedForCity is null send it downward in ordering
CASE WHEN ISNULL(state,'') = @stateAb and ISNULL(usedForCity,1) = 0 THEN 0 ELSE 1 END

Sample data and expected results would help us help you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now