排除给定年份的结果

时间:2019-02-25 21:54:38

标签: sql sql-server

我需要从Jerry(订单年)中排除2019,只显示他在2018年的销售额。我可以在代码中添加些什么,以删除Jerry的2019年销售额。我正在使用SSMS 2016。

select orderyear, case when code = '2099' then 'Laura'
                       when code = '3099' then 'John'
                       when code = '4099' then 'Jerry'
                  end as 'Members', count(sales)numberofsales
from mytable
group by orderyear, case when code = '2099' then 'Laura'
                         when code = '3099' then 'John'
                         when code = '4099' then 'Tony'
                    end

结果:

orderyear            members                numberofsales
  2018                John                       200
  2019                John                       100
  2018                Laura                      300
  2019                Laura                      350
  2018                Jerry                      400
  2019                Jerry                      450

要求:

orderyear            members                numberofsales
  2018                John                       200
  2019                John                       100
  2018                Laura                      300
  2019                Laura                      350
  2018                Jerry                      400

2 个答案:

答案 0 :(得分:1)

添加:

where not (code = '4099' and orderyear = 2019)

答案 1 :(得分:1)

您可以简化为:

select 
  orderyear, 
  case code 
    when '2099' then 'Laura'
    when '3099' then 'John'
    when '4099' then 'Jerry'
  end as Members, 
  count(sales) numberofsales
from mytable
where
  orderyear <> 2019 or code <> '4099'
group by 
  orderyear, 
  code