Solved

sql procedure case sort in desc

Posted on 2014-04-03
18
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +3
18 Comments
 
LVL 40

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 125 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 40

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 40

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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 125 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 125 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 40

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 40

Accepted Solution

by:
Kyle Abrahams earned 125 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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