Solved

sql procedure case sort in desc

Posted on 2014-04-03
18
354 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

911 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

18 Experts available now in Live!

Get 1:1 Help Now