tags:

views:

50

answers:

3

I have this table ATTRIBUTE

id     name       um
12    capacity  MB;GB;TB

And this table2 :

id    id_attribute  id_product    name       value     um
 1        12         40        hdd maxtor     30       GB
 2        12         41        hdd maxtor     40       GB
 3        12         42        hdd y           1       TB

How can i select from table2 in this order : 30GB 40GB 1TB?

Thanks a lot!

+2  A: 

Hello,

"SELECT * FROM table2 ORDER BY um ASC, value ASC" What this does: First orders by UM, then orders by value. This only works because [by accident] GB is a string "smaller" than TB. I suggest switching these with numeric values, because MB is smaller than GB, but the string ain't.

So do another table with this fields: um_id, um_name, um_order, then replace UM with um_id.

Regards, Gabriel

Gabriel Poama-Neagra
+1 for suggesting to use numeric values. (But you could format your code better ;))
Felix Kling
This wouldn't account for the case brought up by Michael where they get 1200vGB compared to 1 TB
Jeff Beck
If I recall correctly, if column UM is of type `enum()`, the order will be as specified in `enum()`, e.g., if it is `enum('B', 'KB', 'MB', 'GB', 'TB')`, then 'B' < 'KB' < 'MB' < 'GB' < 'TB'.
binaryLV
Jeff, don't really see a reason why you would write 1024MB instead of 1GB. I was trying to give a simple solution to a simple problem, not trying to solve the general problem caused by measure units
Gabriel Poama-Neagra
+2  A: 
select value, um 
from tbl

order by 

case um 
when 'KB' then 1
when 'MB' then 2
when 'GB' then 3
when 'TB' then 4
else 5
end,

value

But if you want to take into account that 1200 GB > 1 TB, and must be sorted last, do this:

select value, um 
from tbl

order by value *

case um 
when 'KB' then 1000
when 'MB' then 1000000
when 'GB' then 1000000000
when 'TB' then 1000000000000
else 1000000000000000
end

[EDIT]

Capitalizing on Jeff Beck's table:

select tbl.value, tbl.um 
from tbl
join unit /* name Jeff's table as unit */
on unit.um = tbl.um

order by 
    unit.type -- this will nicely "group" the related type
    ,tbl.value * unit.magnitude
Michael Buen
You wanted value * not um * before the case statement correct?
Jeff Beck
It's good, but I want to use ATTRIBUTE.UM order, and TABLE2.VALUE because I will have cm;dm;m or g,Kg,T ... Thanks a lot
cosy
You should consider putting the UM into it's own table because you may not always want to use measures that are consistently spaced apart. And may need to do the conversions as it's not uncommon to get something like 1200 GB which will need to convert to compare that to 1 TB
Jeff Beck
@Jeff Beck thanks for pointing out the multiplication expression
Michael Buen
Thanks a lot Michael Buen, i appreciate your final answer !
cosy
+2  A: 

You will want this to work with MB as well so what I would do is create a function to convert the two columns value and UM into the mb result. So the function would multiply the value by 1 for mb and 1000 for gb and 1000000 for tb. You can then order on the function. But this will not be ideal performance, but with this meta style db I'm not sure exactly how else to optimize it.

To expand on this you could create a translation table for all your UMs that the function worked on so it could support this for other things then just size.

So as an example add a table like

|Type  |UM |Magnitude|
______________________
|Size  |GB |1000     |
|Size  |MB |1        |
|Size  |TB |1000000  |
|Weight|g  |1        |
|Weight|kg |1000     |

Then make the function look up the UM and the magnitude and multiply it would allow you to always order correctly.

Also you can change your attribute table to point at the measurement type instead of having that ; separated list.

Jeff Beck
Thanks a lot Jeff Beck, you are the man! :)
cosy