Solved

double case statement

Posted on 2014-04-07
7
361 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
ID: 39983442
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
ID: 39983476
>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
ID: 39983499
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 39983515
Define 'the correct ordering'.
0
 

Author Comment

by:rivkamak
ID: 39983531
right ordering is only if state still shows up first.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39983641
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
ID: 39985080
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

948 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

22 Experts available now in Live!

Get 1:1 Help Now