M
M
maks789452020-02-19 15:46:51
MySQL
maks78945, 2020-02-19 15:46:51

Why is the result not sorted correctly?

Good afternoon, there is such a query that receives the names of all companies from different tables, combines them and sorts them, but for some reason I don’t understand, the sorting is not correct. Tell me what I'm doing wrong

SELECT
     t.company, SUM(t.dogovor_count_object) AS dogovor_count_object, SUM(t.dogovor_price) AS dogovor_price
FROM (
       SELECT
            company, SUM(dogovor_count_object) AS dogovor_count_object, ROUND(SUM(dogovor_price), 2) AS dogovor_price
       FROM
            tab1
       GROUP BY company
UNION
        SELECT
             company, SUM(dogovor_count_object) AS dogovor_count_object, ROUND(SUM(dogovor_price), 2) AS dogovor_price
        FROM
              tab2
        GROUP BY company
                ......
            ) AS t
GROUP BY t.company
ORDER BY t.dogovor_price DESC

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maks78945, 2020-02-19
@maks78945

Bavashi , It turned out to solve the problem like this:

SELECT * FROM
(SELECT
     t.company, SUM(t.dogovor_count_object) AS dogovor_count_object, SUM(t.dogovor_price) AS dogovor_price
FROM (
       SELECT
            company, SUM(dogovor_count_object) AS dogovor_count_object, ROUND(SUM(dogovor_price), 2) AS dogovor_price
       FROM
            tab1
       GROUP BY company
UNION
        SELECT
             company, SUM(dogovor_count_object) AS dogovor_count_object, ROUND(SUM(dogovor_price), 2) AS dogovor_price
        FROM
              tab2
        GROUP BY company
                ......
            ) AS t
GROUP BY t.company) AS t1
ORDER BY t1.dogovor_price DESC

R
Rsa97, 2020-02-19
@Rsa97

And how do you imagine sorting grouped data by a non-aggregated field?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question