0

我有一个名为 Phone_Details 的表,数据如下所示:

Name            DeviceType  InvoiceDate TotalCharges
Aguilera, Alex  Smart Phone 8/3/2012    606.55
Aguilera, Alex  Data Card   8/3/2012    26.17

我希望输出为:

Name            Total Spend    # of devices    Avg Spend    # of Bills >300
Aguilera, Alex  632.72              2            316.36            1

我试过这样做:

Select Name,Sum(Totalcharges), Count(DeviceType),Sum(Totalcharges)/Count(DeviceType)
from dbo.Phone_Details
group by Name

但是我怎样才能得到最后一列呢?

4

1 回答 1

2
  Select Name,
         Sum(Totalcharges) [Total Spend],
         Count(DeviceType) [# of devices],
         Sum(Totalcharges)/Count(DeviceType) [Avg Spend],
         Count(CASE WHEN TotalCharges > 300 then 1 end)  [# of Bills > 300]
    from dbo.Phone_Details
group by Name
于 2012-10-24T20:35:45.100 回答