2017-01-26 23 views
1

我在創建一個2列的表。 1列包含每個月支付的金額,而另一列包含訂購當月並支付該月份的客戶數量。在計數函數中使用where子句

select sum(paid), count(distinct customer where Order_Month = Paid_Month) 
from DataTable 
group by Paid_Month 

有沒有簡單的方法呢?

回答

4

使用case表達:

select sum(paid), 
     count(distinct case when Order_Month = Paid_Month then customer end) 
from DataTable 
group by Paid_Month;