Avatar of mcrmg
mcrmg
 asked on

SQL pivot query 2

Hi,

EE helped me with this question
https://www.experts-exchange.com/questions/28434814/SQL-pivot-query.html?anchorAnswerId=40068498#a40068498

I was wondering if there is a way to display the result based on the pre-defined range (sorted)

state Row_Count   Amount      pct
----- ----------- ----------- ---------
0-0.03     1           10          0.03
0.1-0.4    4           100         0.34
0.4-0.5    3           130         0.45

thx
Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
mcrmg

8/22/2022 - Mon
Scott Pletcher

You can specify:
ORDER BY pct

Or you could include a table, hard-coded if needed, of the ranges with custom sort orders, join to that, then sort on the result.

Not sure specifically what you are looking for, since "0-0.03", etc., aren't shown in the original data.
mcrmg

ASKER
Some of the results are based of the data itself, such as NY, NJ....

Some will need to be based of the predefined ranges....those are not in the data, is there a way around?  thanks
Scott Pletcher

It sounds like you just need a range table to join to and use to determine range and sort.

But I don't know enough details on that to tell you how to do it.  What specifically are you trying to sort by?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mcrmg

ASKER
ok, lets say I have a range table,   col range will need to be on the result, col category will be used for different report and col sort can be used to sort...will this work?   sorry if this is not clear enough  thx


range       Category     sort
----- ----------- ----------- ---------
0-0.5             state          1
0.5-1.0          state          2
>1.0               state          3
0-100             score          1
200-400         score          2
>400               score         3
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mcrmg

ASKER
thank you so very much, exactly what I need..thanks again