In sql is there a way to sum an already summed column, or aggregate an already aggregated column. For example there is a

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

In sql is there a way to sum an already summed column, or aggregate an already aggregated column. For example there is a

Post by answerhappygod »

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
In Sql Is There A Way To Sum An Already Summed Column Or Aggregate An Already Aggregated Column For Example There Is A 1
In Sql Is There A Way To Sum An Already Summed Column Or Aggregate An Already Aggregated Column For Example There Is A 1 (38.75 KiB) Viewed 26 times
I wrote this query to get the sum of all the cars sold:
select car, sum(sold) as sum_of_solds
from cars_sold
group by car
and it returned this table below, which is a sum of all the carssold in that day:
In Sql Is There A Way To Sum An Already Summed Column Or Aggregate An Already Aggregated Column For Example There Is A 2
In Sql Is There A Way To Sum An Already Summed Column Or Aggregate An Already Aggregated Column For Example There Is A 2 (10.98 KiB) Viewed 26 times
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 query that outputs this
In Sql Is There A Way To Sum An Already Summed Column Or Aggregate An Already Aggregated Column For Example There Is A 3
In Sql Is There A Way To Sum An Already Summed Column Or Aggregate An Already Aggregated Column For Example There Is A 3 (10.34 KiB) Viewed 26 times
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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply