Solved

sql procedure case sort in desc

Posted on 2014-04-03
18
356 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 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 40

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 40

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Using a hta file with MSSQL agent to schedule a script job 4 41
Re-appearing SQL Server Agent jobs 7 30
sql server query 18 42
Amazon RDS Server load 2 16
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

828 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