Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! This might seem stupid but I am looking for a simple select statement for my database. Scenario:

I have a database that contains: Province, CustomerName and a SalesValue. I want to ouput ALL of the CustomerName with their SalesValue and the Province and then, the tricky part for me: view the total sales for each Province the customer has made in each Province.

I feel stupid and this is the firs time I struggled with such a statement.
I can write a stored procedure with the SUM function but it needs to output all of the records. Which means I cant give it a specific WHERE clause on a specific CustomerName!

ex. Fields: CustomerName Province SalesValue TotalSalesPerProvince

Output: Rachel Gauteng 500 1500

HELP!!!
Posted
Comments
Zoltán Zörgő 16-Oct-12 8:36am    
This makes no sense: you either select SalesValue or the sum of SalesValue based on some grouping. Or where is TotalSalesPerProvince coming from? Plus: you speek about database, but I see it as a table. Please provide the schema of your table or tables (with their relations) and a more complex sample for input tables and the query result you want.

Try looking at the GROUP BY clause: http://www.w3schools.com/sql/sql_groupby.asp[^]

It does what you want, but I can't give you an example without knowing more about your database and data.
 
Share this answer
 
1. I want to ouput ALL of the CustomerName with their SalesValue and the Province

SQL
Select CustomerName,SalesValue,Province into #Temp
from tbl_Name



2. and then, the tricky part for me: view the total sales for each Province the customer has made in each Province.

SQL
Select CustomerName,Province ,Sum(SalesValue)from tbl_Name a
inner join #Temp b on a.CustomerValue=b.CustomerValue
group by CustomerName,Province
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900