2019-04-03 SQL Group By某列,预先对该列进行一个预处理,提炼出共有的信息,即关键字case when 列名什么条件 then 赋值 else 赋值 end as 新列名

 select sum(发行金额) from(
 select PoolNameFormat,count(cast(ItemValue as decimal(19,4))) as 发行笔数,sum(cast(ItemValue as decimal(19,4))) as 发行金额 from (
 select *,
 case when PoolName like '%2015%'then 2015
  when PoolName like '%2016%'then 2016  
  when PoolName like '%2017%'then 2017  
  when PoolName like '%2018%'then 2018  
  when PoolName like '%2019%'then 2019  
  else 1990
  end as  PoolNameFormat
  FROM [InvestSuite].[dbo].[PoolStatistics] as ps
  inner join [InvestSuite].[dbo].[AssetPool] as ap
  on ps.PoolID=ap.ID
  where ps.ItemId=N'400' and cast(ps.ItemValue as decimal(19,4))<1000-- between 1000 and 10000
  )a
  group by PoolNameFormat 
  )a

  

原文地址:https://www.cnblogs.com/theDataDigger/p/10650948.html