SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sort order for multiple columns randomly
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pchuen
Starting Member

Malaysia
11 Posts

Posted - 05/15/2013 :  02:57:19  Show Profile  Reply with Quote
Hi,
I am new to sql scripting, I have the following data. Example:
Top row is my field names:
state agency Qty
KDH WSL 1
KUL WSL 1
JHR RTL 1
MLK RTL 1

I need to sort by:
No.1 : Agency (i) RTL (ii) WSL
No.2 : State (i) JHR (ii) MLK (iii) KUL (iv) KDH

Please help me. Thank you very much in advance.

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/15/2013 :  03:45:07  Show Profile  Reply with Quote

SELECT *
FROM table
ORDER BY CASE Agency WHEN 'RTL' THEN 1 WHEN 'WSL' THEN 2 END 

SELECT *
FROM table
ORDER BY CASE State WHEN 'JHR' THEN 1 WHEN 'MLK' THEN 2 WHEN 'KUL' THEN 3 WHEN 'KDH' THEN 2 END 


A more flexible solution would be to create sortorder column in states/agency tables and use it in above query by joining to them on relevant fields. This will ensure you just needing to tweak value in table rather than changing the query each time.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

pchuen
Starting Member

Malaysia
11 Posts

Posted - 05/15/2013 :  23:35:40  Show Profile  Reply with Quote
thanks Visakh for your quick response. I tried the below as i need to sequence by column based on specific records, would the below be fine?


SELECT SWFYEAR AS FisYear, SWAC01 AS Agency, SWADDS AS State, SWSRP1 AS Brand, SWSRP2 AS SubBrand, SWSRP4 AS Range, SWURRF as ACTBGT,SUM(SWC9QTY1) AS JanQty,
SUM(SWC9QTY2) AS FebQty
FROM PRODDTA.F55SWF where swsrp4 not in ('QTR','HQT')
GROUP BY SWFYEAR, SWAC01, SWADDS, SWSRP1, SWSRP2, SWSRP3, SWSRP4, SWSRP0, SWURRF
ORDER BY (CASE WHEN SWADDS = 'JHR' THEN 1 WHEN SWADDS = 'MLK' THEN 2 ELSE 3 END), State, FisYear DESC,
(CASE WHEN SWSRP1 = 'PAL' THEN 1 ELSE 2 END)
Go to Top of Page

pchuen
Starting Member

Malaysia
11 Posts

Posted - 05/15/2013 :  23:48:26  Show Profile  Reply with Quote
quote:
Originally posted by pchuen

thanks Visakh for your quick response. I tried the below as i need to sequence by column based on specific records, would the below be fine?


SELECT SWFYEAR AS FisYear, SWAC01 AS Agency, SWADDS AS State, SWSRP1 AS Brand, SWSRP2 AS SubBrand, SWSRP4 AS Range, SWURRF as ACTBGT,SUM(SWC9QTY1) AS JanQty,
SUM(SWC9QTY2) AS FebQty
FROM PRODDTA.F55SWF where swsrp4 not in ('QTR','HQT')
GROUP BY SWFYEAR, SWAC01, SWADDS, SWSRP1, SWSRP2, SWSRP3, SWSRP4, SWSRP0, SWURRF
ORDER BY (CASE WHEN SWADDS = 'JHR' THEN 1 WHEN SWADDS = 'MLK' THEN 2 ELSE 3 END), State, FisYear DESC,
(CASE WHEN SWSRP1 = 'PAL' THEN 1 ELSE 2 END)

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/16/2013 :  00:11:17  Show Profile  Reply with Quote
quote:
Originally posted by pchuen

thanks Visakh for your quick response. I tried the below as i need to sequence by column based on specific records, would the below be fine?


SELECT SWFYEAR AS FisYear, SWAC01 AS Agency, SWADDS AS State, SWSRP1 AS Brand, SWSRP2 AS SubBrand, SWSRP4 AS Range, SWURRF as ACTBGT,SUM(SWC9QTY1) AS JanQty,
SUM(SWC9QTY2) AS FebQty
FROM PRODDTA.F55SWF where swsrp4 not in ('QTR','HQT')
GROUP BY SWFYEAR, SWAC01, SWADDS, SWSRP1, SWSRP2, SWSRP3, SWSRP4, SWSRP0, SWURRF
ORDER BY (CASE WHEN SWADDS = 'JHR' THEN 1 WHEN SWADDS = 'MLK' THEN 2 ELSE 3 END), State, FisYear DESC,
(CASE WHEN SWSRP1 = 'PAL' THEN 1 ELSE 2 END)


Looks fine to me

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

pchuen
Starting Member

Malaysia
11 Posts

Posted - 05/16/2013 :  23:19:45  Show Profile  Reply with Quote
thanks a lot for your kind help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/16/2013 :  23:56:46  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000