2014-07-08 47 views
-4

表交易:business_key = CUSTOMER_IDSQL:推導基於輸入值

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

表transactionsbycustomer:business_key = CUSTOMER_ID + PRODUCT_TYPE

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

我需要「transactionsbycustomer導出用於值'表使用SQL?請幫忙!

+0

你的意思是這樣的:'SELECT amount FROM transactionsbycustomer WHERE customer_id =「cus1」';?你特別需要什麼? – JiFus

+0

請根據樣本數據添加預期輸出示例。 –

+0

我想你正在考慮'GROUP BY'子句。 – mustaccio

回答

0

這是非常基本的SQL。

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