SQL-如何在汇总图中创建新列时使用案例

问题描述 投票:0回答:1

我在网上找到了一个非常好的查询以创建摘要图表:

select 
    year([invoice posted date]) as SalesYear,
    month ([invoice posted date]) as SalesMonth,
    round(sum([INV Line Final Price]), 0) as TotalSales
from 
    [Invoice_Data22618]
group by 
    year([invoice posted date]), month ([invoice posted date])
order by 
    year([invoice posted date]), month ([invoice posted date])

这将返回类似以下内容的结果:

SalesYear | SalesMonth | TotalSales
2017      |  11           xxxxx
2017      |  12           xxxxx           
2018         1            xxxxx
2018         2            xxxxx

我现在想沿着以下行添加一个新列,即NewCustomerSales:

case 
   when [customer date created] between '2017-11-01' and 2017-11-30' 
      then round(sum([INV Line Final Price]), 0) as NewCustSales, 
      else 0

[我试图弄清楚如何获得NewCustSales来搜索SalesYearSalesMonth,以便我可以看到每个月我的销售额中有多少来自新客户还是现有客户。创建的发票过帐日期和客户日期的格式均为YYYY-MM-DD HH:MM:SS.000

感谢您的帮助!

sql tsql
1个回答
1
投票

我认为这会稍微复杂一点,因为您需要从池中过滤掉非新客户。这样的事情应该起作用:

© www.soinside.com 2019 - 2024. All rights reserved.