Skip to content
Advertisement

Get percentage of total using sub selection

I have a query that sums sold units and groups them by store. See code and/or SQL-fiddle below:

http://www.sqlfiddle.com/#!5/95f29/6

However, I also want the percentage sold by each unit to the total and have therefore created a column named “sold_total”. After the output of this query is generated I need to save it and do another query to get the percentage, see below:

There’s got to be a way to “merge” these two queries into one, and from my understanding, you could use sub-queries which I have tried but given the small SQL-knowledge I haven’t got it t work.

Any help out there?

Advertisement

Answer

I think, I have a solution for you. You can use CTE. Here is my code given Below

Note: This code is written in SQLite. Please check the link => sqlfiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement