Link to home
Start Free TrialLog in
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
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

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.
Avatar of mcrmg
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
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?
Avatar of 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
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

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