Friday, 23 August 2013

SQL Query Sum and total of rows

SQL Query Sum and total of rows

I have a table that has three columns:

LOCATION | ITEM | QUANTITY
--------------------------------
001 RED CAR 1
002 RED CAR 3
003 BLUE CAR 5
002 BLUE CAR 2
001 RED CAR 2
002 RED CAR 5


Im trying to run a query that will tell me how many of each unique item
each location has. The reason I have multiple lines for the same location
is that each location might have multiple people inputting the records at
the same time.
The goal is to get the total number of items and the total that each
location entered.


ITEM | LOCATION 001 | LOCATION 002 | LOCATION 003 | TOTAL
--------------------------------------------------------------
RED CAR 3 8 0 11
BLUE CAR 0 2 5 7


I can not come up with a single SELECT query that will get me both the
total for each location and the total for each item. Im trying to complete
this with a single query rather than running two separate query requests.
Any help would be greatly appreciated.
I have test link to try out some different queries on.
http://www.sqlfiddle.com/#!2/c33cee/1/0

No comments:

Post a Comment