Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 排除给定年份的结果_Sql_Sql Server - Fatal编程技术网

Sql 排除给定年份的结果

Sql 排除给定年份的结果,sql,sql-server,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'

我需要将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
加:

加:


您可以简化为:

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
选择
订单年,
案例代码
当'2099'然后'Laura'
当'3099'然后是'John'
当'4099'然后'Jerry'
作为成员结束,
计数(销售)数量销售
从mytable
哪里
订单年份2019或代码“4099”
分组
订单年,
代码

您可以简化为:

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
选择
订单年,
案例代码
当'2099'然后'Laura'
当'3099'然后是'John'
当'4099'然后'Jerry'
作为成员结束,
计数(销售)数量销售
从mytable
哪里
订单年份2019或代码“4099”
分组
订单年,
代码