Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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 server 用于查找与where的和的sql查询_Sql Server_Sql Server 2008_Tsql_Sql Server 2008 R2_Sql Server 2012 - Fatal编程技术网

Sql server 用于查找与where的和的sql查询

Sql server 用于查找与where的和的sql查询,sql-server,sql-server-2008,tsql,sql-server-2008-r2,sql-server-2012,Sql Server,Sql Server 2008,Tsql,Sql Server 2008 R2,Sql Server 2012,我有一个数据库表,如下所示: Date Store SubStore ProductsSold 2013-03-01 A AA 33 2013-03-01 B BB 53 2013-03-01 C CC 43 2013-03-01 B BB 11 2013-03-01 A AA 65 2013-03-01

我有一个数据库表,如下所示:

Date      Store SubStore ProductsSold
2013-03-01    A      AA          33
2013-03-01    B      BB          53 
2013-03-01    C      CC          43 
2013-03-01    B      BB          11 
2013-03-01    A      AA          65 
2013-03-01    A      DD          65 
该表没有主键或外键

我想为我的报告写一个查询,在那里我想看到销售的产品 对于3月份的子存储AA、CC和其他所有子存储

大概是这样的:

Substore    ProductsSold
AA               343
CC               332
Others           3432
Select SubStore,  Sum(NumberOfProducts) as Sales
 from 
[dbo].[TableName]
where [Year] = year(@ParamDate) AND [Month] = month(@ParamDate)
and (SubStore = 'AA' or SubStore =  'BB')
有什么办法我能做到吗

我提出了一个类似这样的问题:

Substore    ProductsSold
AA               343
CC               332
Others           3432
Select SubStore,  Sum(NumberOfProducts) as Sales
 from 
[dbo].[TableName]
where [Year] = year(@ParamDate) AND [Month] = month(@ParamDate)
and (SubStore = 'AA' or SubStore =  'BB')

请告知我可以进行哪些更改,以便我也可以看到其他更改。

您可以使用
案例
为您可以分组的门店名称创建一个值:

select
  case when Substore in ('AA','CC') then Substore else 'Others' end as SubStore,
  Sum(ProductsSold) as ProductsSold
from
  [dbo].[TableName]
where
  [Year] = year(@ParamDate) AND [Month] = month(@ParamDate)
group by
  case when Substore in ('AA','CC') then Substore else 'Others' end

我能想到的最简单的事情-

select t.substore, 
SUM(productsSold) as productsSold
from tablename as t
where t.substore in ('AA', 'CC')
group by substore
UNION ALL
select 'Others' as substore, 
SUM(productsSold) as productsSold
from tablename as t
where t.substore not in ('AA', 'CC')