Frage

[sql advanced sorting] http://www.imagebam.com/image/382513311495980 please click picture for database example (my reputation is too low to post normal pictures

I have the following part of the database, the selected text is correct, the unselected text is the part I wish would work (but i lack experience for this to understand)

Rarity has 4 values, I want to select first the value M and R which seems to be succesful

then comes the tricky part because my experience with sql is very limited and my ask buddy is out of town ha ha.

when premium = 'F' it actually means that it is a normal card and therefor it needs to be linked to the normalbuy and normallsell columns. the foillbuy and foilsell should be ignored for desc porpuse

when premium = 'T' it actually means that it is a normal card and therefor it needs to be linked to the foillbuy and foilsell columns. the normalbuy and normallsell should be ignored for desc porpuse

then I just want it to be sorted on price from high to low

as extra note:

Premium |NormalBuy | Normalsell | FoilBuy| FoilSell | Stock 
    F       9.66  |   15.99    |  27.00 |    35.500 |    1  

this means I have 1 normal card because stock 1 and premium is false and foilbuy and foilsell info can be ignored for sorting purposes

Premium |NormalBuy | Normalsell | FoilBuy| FoilSell | Stock 
    T       0.030  |   0.040    |  0.100 |   0.500  |    1              

this means I have 1 Foil card because stock 1 and premium is True and normalbuy and normalsell info can be ignored for sorting purposes

here is an example how it should look

Rarity|Premium|NormalBuy|Normalsell|FoilBuy|FoilSell|Stock(row order)(leading)

 M         F     0.400     0.700     4.000   10.000    4        1
 M         F     1.420     1.800     4.000   12.000    4        2
 M         F     3.250     3.850     10.000  15.000    4        3
 M         T     1.350     1.700     4.000   16.000    4        4
 M         F     3.600     4.250     12.500  16.000    4        5
 M         F     3.100     4.000     9.000   16.750    4        6
 M         T     6.750     7.750     8.000   17.000    4        7
 M         F     5.500     5.750     14.500  17.500    4        8
 M         T     3.000     5.000     9.000   19.000    4        9
 M         F     14.00     18.000    17.750  28.000    4        10

below should be the result



 M          T     3.000    5.000     9.000   19.000    4        9     19.000
 M          F     14.00    18.000    17.750  28.000    4        10    18.000
 M          T     6.750    7.750     8.000   17.000    4        7     17.000
 M          T     1.350    1.700     4.000   16.000    4        4     16.000 
 M          F     5.500    5.750     14.500  17.500    4        8     5.750
 M          F     3.600    4.250     12.500  16.000    4        5     4.250
 M          F     3.100    4.000     9.000   16.750    4        6     4.000 
 M          F     3.250    3.850     10.000  15.000    4        3     3.850 
 M          F     1.420    1.800     4.000   12.000    4        2     1.800
 M          F     0.400    0.700     4.000   10.000    4        1     0.700

Keine korrekte Lösung

Andere Tipps

To decide which columns to sort on, you need to use either a CASE condition in your order by (potentially multiple), use a UNION with the condition and sort on column index, or use an inner-select to assign the values to a virtual column and then sort on that.

Sorting by CASE:

SELECT ...
FROM ...
ORDER BY CASE WHEN Premium = 'T' THEN FOILBUY ELSE NORMALBUY END, 
         CASE WHEN Premium = 'T' THEN FOILSELL ELSE NORMALSELL END

Sorting using UNION:

SELECT FOILBUY, FOILSELL, ...
FROM ...
WHERE Premium = 'T'
UNION ALL
SELECT NORMALBUY, NORMALSELL, ...
WHERE Premium = 'F'
ORDER BY 1, 2

Sorting using inner select

SELECT BUY, SELL, ...
FROM (
  SELECT CASE WHEN Premium = 'T' THEN FOILBUY ELSE NORMALBUY END AS BUY, 
         CASE WHEN Premium = 'T' THEN FOILSELL ELSE NORMALSELL END AS SELL,
         ...
  FROM ...
)
ORDER BY BUY, SELL

But having a discriminator column which indicates which columns need to be used may indicate a database normalisation problem. You might want to look at your database design if you can't eliminate this.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top