SQL:根据输入派生值

时间:2014-07-08 13:10:26

标签: sql db2

表格交易:business_key = customer_id

customer_id|product_type|amount
-------------------------------
cus1       | shoes      | 100
cus1       | shoes      |  50
cus1       | clothes    | 100
cus2       | clothes    | 500
cus2       |clothes     |  21

table transactionsbycustomer:business_key = customer_id + product_type

customer_id|product_type|amount
-------------------------------
cus1       |shoes       |150
cus1       |clothes     | 50
cus2       |clothes     |521

我需要通过客户'在' Transactions中获得金额的值。使用SQL的表?请帮忙!

1 个答案:

答案 0 :(得分:0)

这是非常基本的SQL。

SELECT customer_id, product_id, sum(amount) as total
FROM transactionsbycustomers
GROUP BY customer_id, product_id