In sql is there a way to sum an already summed column, oraggregate an already aggregated column. For example
there is a cars table that shows all the cars andmodels sold in one day, table below
I wrote this query to get the sum of all the cars sold:
select car, sum(sold) as sum_of_sold
from cars_sold
group by car
and it returned this table below, which is a sum of all the carssold in that day:
Now the question is is there a way to get the top 2 highestselling cars and then summing up the other cars as 'others', so iftesla sold 90,000 that day they would be 1, if honda sold75,000
they would be 2. and since mazda and toyota combined sold 90,000they would labeled as other, since individually they were not thetop 2 cars sold.
The output should look like this. Again sum an already summedcolumns: write a sql query that outputs this
car tesla honda toyota mazda tesla honda toyota mazda tesla honda toyota mazda model roadster accord camry s3 roadster accord camry s3 roadster accord camry s3 cars sold sold 30,000 25,000 10,000 20,000 30,000 25,000 10,000 20,000 30,000 25,000 10,000 20,000
car tesla honda toyota mazda sum of sold 90,000 75,000 30,000 60,000
Rank other car 1 tesla 2 mazda others amount 90,000 75,000 90,000
In sql is there a way to sum an already summed column, or aggregate an already aggregated column. For example there is a
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am