Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question