需要特定的SQL Select Stament! [英] Need a specific SQL Select Stament!
问题描述
嗨!这似乎很愚蠢,但我正在为我的数据库寻找一个简单的select语句.场景:
我有一个包含以下内容的数据库:Province,CustomerName和SalesValue.我想将所有CustomerName连同其SalesValue和Province一起输出,然后,对我来说最棘手的部分是:查看客户在每个省中每个省的总销售额.
我感到很愚蠢,这是我为这样的陈述而苦苦挣扎的第一时间.
我可以使用SUM函数编写存储过程,但是它需要输出所有记录.这意味着我无法在特定的CustomerName上为其指定特定的WHERE子句!
前任.字段:CustomerName省份SalesValue TotalSalesPerProvince
输出:Rachel Gauteng 500 1500
HELP !!!
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!!!
推荐答案
尝试查看GROUP BY子句: http://www.w3schools.com/sql/sql_groupby.asp [ ^ ]
它可以满足您的要求,但是我无法在不了解您的数据库和数据的情况下为您提供示例.
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.
1.我想用他们的SalesValue和省份输出所有的CustomerName
1. I want to ouput ALL of the CustomerName with their SalesValue and the Province
Select CustomerName,SalesValue,Province into #Temp
from tbl_Name
2.然后是我最棘手的部分:查看客户在每个省中每个省的总销售额.
从中选择
2. and then, the tricky part for me: view the total sales for each Province the customer has made in each Province.
Select CustomerName,Province ,Sum(SalesValue)from tbl_Name a
inner join #Temp b on a.CustomerValue=b.CustomerValue
group by CustomerName,Province
这篇关于需要特定的SQL Select Stament!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!