请以SQL
开头帮助Im juste鉴于下表:
CUSTOMER TRANSACTION_CODE AMOUNT
-------- ---------------- -------
001 WE01 100.00
001 WE02 80.00
002 WE01 120.00
002 WE02 22.00
我想用SQL查询来总结同一客户的交易(我不需要显示交易代码)
CUSTOMER AMOUNT
-------- ---------
001 180.00
002 144.00
我使用group by
和SUM
进行了不同的测试,但我无法获得正确的语法 - 您的帮助会像往常一样受到欢迎
答案 0 :(得分:0)
您必须使用group by
对一个或多个字段进行分组,并在语句的select
部分使用sum
函数汇总列中的值。
在这里,您可以阅读有关group by
的所有信息由CUSTOMER和TRANSACTION_CODE分组
SELECT CUSTOMER, TRANSACTION_CODE, SUM(AMOUNT) as AMOUNT
FROM TABLE
GROUP BY CUSTOMER, TRANSACTION_CODE
仅由CUSTOMER分组
SELECT CUSTOMER, TRANSACTION_CODE, SUM(AMOUNT) as AMOUNT
FROM TABLE
GROUP BY CUSTOMER, TRANSACTION_CODE
答案 1 :(得分:0)
你应该按照顾客分组
select customer, sum(amount) as amount
from my_table
group by customer
order by customer
答案 2 :(得分:0)
你走在正确的轨道上!
试试这个:
select customer, sum(amount)
from your_table
group by customer
答案 3 :(得分:0)
这是一个简单的分组查询。试试这个:
SELECT customer, SUM(amount) AS amount
FROM myTable
GROUP BY customer
编辑以下是联接的示例:
SELECT table1.customer, SUM(table1.amount) AS amount
FROM table1
INNER JOIN table2 ON table2.col1 = table1.col1
GROUP BY table1.customer
答案 4 :(得分:0)
您需要在select和group by customer中使用聚合求和功能。
SELECT Customer, SUM(amount)
From Tablename
GROUP BY Customer;