Solved

sql procedure case sort in desc

Posted on 2014-04-03
18
352 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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
I am getting invalid column name MyIdNbr
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
That was for @Dulton
0
 

Author Comment

by:rivkamak
Comment Utility
@: 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 125 total points
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
you are all doing the case right.
now I want the case to show in desc order.
can we do that?
0
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 125 total points
Comment Utility
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
Comment Utility
Did you see Scott's or my post?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
thank you all for your help.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

763 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

5 Experts available now in Live!

Get 1:1 Help Now