1

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.

New contributor
Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 4
    ORDER BY should be at the end. You don't need them within each of the UNIONed queries. Also, it's unclear why you are wrapping your queries. Why not just select col1, col2 from games where.... union all select col1, col2 from games where... order by name asc
    – Isolated
    Sep 18 at 16:47

5 Answers 5

0

Just wrap your query with another outer query :)

This should be fairly easy by the following solution:

SELECT * FROM (
    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
) ORDER by Name asc;
0

If you want to keep the order of the subqueries you need to add LIMIT to the ORDER BY.

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 
    LIMIT 18446744073709551615
) 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 
    LIMIT 18446744073709551615
) b
;
CREATE tABLe games (Name varchar(10), Year_of_Release int
  , Other_Sales int
  , Global_Sales int)
INSERT INTO games VALUES ('A',2000,1,1),('A',2000,2,1),('A',2000,3,1),('A',1999,1,1),('A',1999,2,1),('A',1999,2,1)
Records: 6  Duplicates: 0  Warnings: 0

    (select Name, Year_of_Release, Other_Sales, Global_Sales
    from games
    where Year_of_Release >=2000
    order by Global_Sales desc, Name asc 
    LIMIT 18446744073709551615)
 
union all
(
    select Name, Year_of_Release, Other_Sales, Global_Sales
    from games
    where Year_of_Release < 2000
    order by Other_Sales desc, Name asc 
    LIMIT 18446744073709551615
) 
;
Name Year_of_Release Other_Sales Global_Sales
A 2000 1 1
A 2000 2 1
A 2000 3 1
A 1999 2 1
A 1999 2 1
A 1999 1 1

fiddle

3
  • i want the result of year>=2000 appear first and after that rows with year<2000 appear....
    – Ali
    Sep 18 at 17:09
  • run the query and you will see that ot will do exactly that
    – nbk
    Sep 18 at 17:17
  • @Ali i added a fiddle to show, that it works
    – nbk
    Sep 18 at 17:39
0

its ordering must be done after the union query, looking like this:

select Name, Year_of_Release, Other_Sales, Global_Sales from (
    select *
    from games
    where Year_of_Release >=2000
) a
union all
select Name, Year_of_Release, Other_Sales, Global_Sales from (
    select *
    from games
    where Year_of_Release < 2000
) b
ORDER BY Global_Sales DESC, Other_Sales DESC,Name asc;

Your query could be done better, without having to use union, example:

SELECT Name, Year_of_Release, Other_Sales, Global_Sales
FROM games
WHERE Year_of_Release >= 2000 OR (Year_of_Release < 2000 AND Global_Sales IS NOT NULL)
ORDER BY Global_Sales DESC, Other_Sales DESC, Name asc ;

If you need to sort the result with year>=2000 first and then the lines with year<2000:

 SELECT Name, Year_of_Release, Other_Sales, Global_Sales
    FROM games
    ORDER BY 
        CASE
            WHEN Year_of_Release >= 2000 THEN 0
            ELSE 1
        END,
        Global_Sales DESC,
        Other_Sales DESC,
        Name asc ;

Global_Sales and Other_Sales are in DESC. If so, these columns will be given priority in the descending classification.

4
  • i want the result of year>=2000 appear first and after that rows with year<2000 appear.... –
    – Ali
    Sep 18 at 17:10
  • Includes this other option for you Sep 18 at 17:33
  • still name sorting not working for year_of_release>=2000!
    – Ali
    Sep 18 at 17:37
  • Global_Sales and Other_Sales are in DESC, are you sure that is it? If so, these columns will be given priority in the descending classification. I will include an example in the case to explain better. Sep 18 at 17:41
0

If this helps...

-- Create a Common Table Expression (CTE) named CTE
WITH CTE AS (
    -- Inside the CTE, select all columns and assign row numbers (rn1 and rn2)
    SELECT
        *,
        -- Calculate rn1: Row number based on descending Global_Sales and ascending Name
        ROW_NUMBER() OVER (PARTITION BY Year_of_Release ORDER BY Global_Sales DESC, Name ASC) AS rn1,
        -- Calculate rn2: Row number based on descending Other_Sales and ascending Name
        ROW_NUMBER() OVER (PARTITION BY Year_of_Release ORDER BY Other_Sales DESC, Name ASC) AS rn2
    FROM
        games -- Select data from the 'games' table
)
-- Main query: Select specific columns from the CTE and order the results
SELECT
    Name,
    Year_of_Release,
    Other_Sales,
    Global_Sales
FROM
    CTE
ORDER BY
    Year_of_Release DESC,
    CASE WHEN Year_of_Release >= 2000 THEN rn1 ELSE rn2 END ASC, -- Conditionally sort by rn1 or rn2
    Name ASC; 
0

Absent an explicit ORDER BY (which is NOT queries connected by a UNION in sequence), the database is free to return rows in whatever order it deems best (where "best" usually means "fastest"), and can even change for the same query and same data from one run to the next.

Additionally, when you have an ORDER BY within a sub query, once that sub query is completed the ordering does not constrain the parent query in any way.

In this case, the query engine likely detected it could satisfy the query with a single pass through the table, and just returned rows in index order.

Now you could wrap what you have in another ORDER BY, but we can do better. In fact, you can greatly simplify the query like this:

SELECT Name, Year_of_Release, Other_Sales, Global_Sales
FROM games
ORDER BY 
    CASE WHEN Year_of_Release >= 2000 THEN 0 ELSE 1 END,
    CASE WHEN Year_of_Release >= 2000 THEN Global_Sales ELSE Other_Sales END DESC,
    Name ASC

It should be further possible to only need the CASE expression once, where you use the 2nd expression and give Global_Sales a bonus or Other_Sales a penalty relative to the range of the data, to force those rows first/last entirely within that single expression. However, that requires knowing more about the data than we can see here.

1
  • it worked perfectly!!! thanks a lot.
    – Ali
    Sep 18 at 20:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.