我有下表
idOrder | idCust |Name | OrderDate | affId | Sku | desc |quantity |unitPrice | subtotal
224332 | 4657 |Jeff |6/5/2014 11:09 | 1 |1344 |--- | 6 |41.25 | 247.5
224332 | 4657 |Jeff |6/5/2014 11:09 | 1 |1388 |--- | 3 |41.25 | 123.75
224388 | 4656 |Jane |6/5/2014 08:09 | 1 |1388 | --- | 6 |41.25 | 247.5
224332 | 4659 |Tom |7/5/2014 11:09 | 1 |1222 |--- | 6 |41.25 | 247.5
224332 | 4657 |Jeff |7/7/2014 11:09 | 1 |1344 |--- | 3 |41.25 | 123.75
224388 | 4656 |Jane |7/5/2014 08:09 | 1 |1222 |--- | 6 |41.25 | 247.5
我需要总结一下表格 所有6月购买的总购买量为当月的sku#1344 所以我会在6月份的新表中有以下内容
IdCust | Name| Total Purchase (dollar amount) | Total of sku# 1334 (dolar amount)|
4657 |Jeff | sum of all the above for june | some of just that sku for june|
4656 |Jane | sum of all the above for june | some of just that sku for june|
4659 |Tom | sum of all the above for june | some of just that sku for june|
答案 0 :(得分:1)
您可以使用条件聚合轻松完成此操作:
select idCust, Name, sum(quantity * unitPrice) as TotalPurchase,
sum(case when sku = 1344 then quantity * unitPrice end) as TotalPurchase_1344
from table t
where date >= '2014-06-01' and date < '2014-07-01'
group by idCust, Name;