can anybody help?
there’s a table containing data about games…including:
name VARCHAR(255)
platform VARCHAR(255)
year_of_release YEAR
genre VARCHAR(255)
publisher VARCHAR(255)
na_sales DOUBLE(10,2)
other_sales DOUBLE(10,2)
global_sales DOUBLE(10,2)
when i run this query:
select Name, Year_of_Release, Other_Sales, Global_Sales from (
select *
from games
where Year_of_Release >=2000
order by Global_Sales desc, Name asc
) a
union all
select Name, Year_of_Release, Other_Sales, Global_Sales from (
select *
from games
where Year_of_Release < 2000
order by Other_Sales desc, Name asc
) b
;
i want the rows with year_of_release >=2000 appear first and after that rows with year_of_release<2000 appear.for the first group i want to sort by Global_sale and for second one by Other_sale…and if two rows was equal sale, sort by name in asc form.
Thanks for help.
i tried almost everything on the web,didn’t work.
3
Answers
If you want to keep the order of the subqueries you need to add
LIMIT
to theORDER BY
.its ordering must be done after the union query, looking like this:
Your query could be done better, without having to use union, example:
If you need to sort the result with year>=2000 first and then the lines with year<2000:
Just wrap your query with another outer query 🙂
This should be fairly easy by the following solution: