Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql procedure case sort in desc

Posted on 2014-04-03
18
Medium Priority
?
363 Views
Last Modified: 2014-04-07
I have a complicated stored procedure when I want to sort out 1 column first, then the rest of the table.
How can I request that just the first case request be sorted DESC.
select  top 100 percent * into #t1 from k4kReviewsForSite2013 ORDER BY
   -- First sort position
   CASE WHEN city = @stateAb THEN 0 ELSE 1 END

 select row_number() over( order by (select 1)) rn, *  into #t2

Open in new window

0
Comment
Question by:rivkamak
  • 6
  • 5
  • 2
  • +3
18 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39975901
select *,    CASE WHEN city = @stateAb THEN 0 ELSE 1 END SortOrder into #t1 from k4kReviewsForSite2013

when you do the next select you'll need to do your order by there
eg:

select * from #t1
order by sortOrder Desc
   -- First sort position
0
 

Author Comment

by:rivkamak
ID: 39975946
Let's say I am passing in city 'mesa'.
there are 3 records out of the 1000 that  have city = mesa.

Before I started I got
 1-mesa
2-mesa
3-mesa
4-other
5-other

With your script I am getting back
5-other
4-other
3-mesa
2-mesa
1-mesa

What I need to get back is
3-mesa
2-mesa
1-mesa
4-other
5-other
0
 
LVL 6

Assisted Solution

by:Dulton
Dulton earned 500 total points
ID: 39976015
What we'll do, is build a case statement that returns -1 when we find the city you want. if it's not the city you want, we'll just return 1.
whatever we return (1 or -1) we'll multiply by the ID # of your row. then order by this row. it'll put all of the rows of interest into negative territory. so we only then need to sort ascending.

sorry, had to edit to add in the select *. otherwise not returning any data.

select  *
,(MyIdNbr * Cast(Case WHEN [City] = @StateAb THEN -1 ELSE 1 END AS INT)) AS [OrderField]
from k4kReviewsForSite2013 
Order By OrderField

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39976040
select *,    CASE WHEN city = @stateAb THEN 0 ELSE 1 END SortOrder into #t1 from k4kReviewsForSite2013

-- took out DESC
select * from #t1
order by sortOrder
0
 

Author Comment

by:rivkamak
ID: 39976055
I am getting invalid column name MyIdNbr
0
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39976062
can you post your full query along with your DDL?  I don't see myIdnbr anywhere in your code.
0
 
LVL 6

Expert Comment

by:Dulton
ID: 39976067
I don't know what your table/column structure is, so I used a generic column name [MyIdNumber]

in your prevsious post, you cite data like 1 - mesa, 2- mesa....

Replace [MyidNumber] with whatever column you have that stores that 1 and 2 and so on.
0
 

Author Comment

by:rivkamak
ID: 39976069
That was for @Dulton
0
 

Author Comment

by:rivkamak
ID: 39976112
@: Kyle Abrahams, when I load just the statement  you put it works nicely
When I add it to the rest , it doesnt' work anymore

 /*select  top 100 percent * from k4kReviewsForSite2013 where state = @stateAb order by id2*/
select *,    CASE WHEN city = @stateAb THEN 0 ELSE 1 END SortOrder into #t1 from k4kReviewsForSite2013 

-- took out DESC
--select * from #t1
order by sortOrder


 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


drop table #t1
drop table #t2

Open in new window

0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 500 total points
ID: 39976327
SELECT *
FROM k4kReviewsForSite2013
ORDER BY
    CASE WHEN state = @stateAb THEN 0 ELSE 1 END,
    CASE WHEN state = @stateAb THEN -id2 ELSE id2 END
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 500 total points
ID: 39976353
Based on sample data in your post# 39975946, I assume you want to sort in descending order for messa (or any given city) and sort in ascending order for all other cities.
Also messa records should display first in the list.
You can apply the ORDER BY like this.
declare @stateAb varchar(10)
select @stateAb = 'messa'
declare @table table (ID int,city varchar(20))
insert @table values (1,'messa'),(2,'messa'),(3,'messa'),(4,'newyork'),(5,'newyork'),(6,'other'),(7,'newyork'),(8,'other'),(9,'messa')
select *
  from @table 
 order by case when city = @stateAb then 0 else 1 end,
       case when city = @stateAb then -1 else 1 end*id
/*
ID	city
9	messa
3	messa
2	messa
1	messa
4	newyork
5	newyork
6	other
7	newyork
8	other
*/

Open in new window

0
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39976777
select row_number() over( order by (select 1)) rn, *  into #t2
from #T1
order by sortOrder, idforstate



select * from #T2 where rn between @startNum and @endNum
0
 
LVL 32

Expert Comment

by:awking00
ID: 39978141
If you're just wanting to have 'mesa' come first, followed by the remaining cities, why not (simplified) -
select * from
(select t.*, case when city = 'mesa' then 0 else 1 end as priority
 from table t)
order by priority, city
0
 

Author Comment

by:rivkamak
ID: 39978393
you are all doing the case right.
now I want the case to show in desc order.
can we do that?
0
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39978411
to order by desc all you need to do is add "Desc" after the column name.

eg:

select * from table
order by
col1, col2 DESC,col3

means
order by col1 ascending
then by col2 descending order
then by col3 ascending

if no order is specified the default is ascending.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39978666
Did you see Scott's or my post?
0
 
LVL 32

Expert Comment

by:awking00
ID: 39978690
SQL> select * from cities;
CITY
--------------------
albany
baltimore
mesa
new york
tulsa

SQL> select city from
  2  (select city, case when city = 'mesa' then 0 else 1 end priority
  3   from cities)
  4  order by priority, city;

CITY
--------------------
mesa
albany
baltimore
new york
tulsa
0
 

Author Closing Comment

by:rivkamak
ID: 39983422
thank you all for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

916 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