sql - 根据其他几列的条件对不同的一列进行计数

标签 sql sql-server tsql

我有样本数据:: Data-Sample

  Country   Year  Month  CustID   Sales    Point   TestSUM  OrderType 
 Singapore  2018     10  AAA      623.96      520  1143.96          1 
 Singapore  2018     10  AAA       92.16        0    92.16          3 
 Singapore  2018     11  AAA     1168.28  1002.86  2171.14          1 
 Singapore  2018     11  BBB      118.58   103.25   221.83          1 
 Singapore  2018     10  CCC      118.88   103.25   222.13          1 
 Singapore  2018     11  CCC      278.67   217.75   496.42          1 
 Singapore  2018     11  CCC     -108.72   -94.75  -203.47          2 
 Singapore  2018     11  CCC           0        0        0          3 
 Singapore  2018     10  DDD      446.14    385.9   832.04          1 
 Singapore  2018     11  DDD      138.95      121   259.95          1 
 Singapore  2018     11  DDD     -228.07        0  -228.07          2 
 Singapore  2018     10  EEE      905.32    796.5  1701.82          1 
 Singapore  2018     10  EEE     -405.75  -357.75   -763.5          2 
 Singapore  2018     10  EEE       29.66        0    29.66          3 
 Singapore  2018     11  EEE       147.8      127    274.8          1 
 Singapore  2018     10  FFF           0        0        0          3 
 Singapore  2018     10  GGG           0        0        0          3 
 Singapore  2018     10  HHH      120.57    104.5   225.07          1 
 Singapore  2018     10  HHH           0        0        0          3 
 Singapore  2018     11  HHH      189.59   165.25   354.84          1 
 Singapore  2018     10  JJJ      117.12   100.25   217.37          1 
 Singapore  2018     10  JJJ           0        0        0          3 
 Singapore  2018     11  JJJ      291.53   254.25   545.78          1 

我正在尝试根据 3 列中的 2 个条件计算不同的“CustID”:

  1. “销售额”和“积分”不能同时为“0”
  2. “OrderType”仅位于 (1, 2, 3)

因此,我创建了一个附加列“TestSUM”,它对“销售额”和“积分”进行求和,不等于 0,以满足条件 #1

使用下面的代码,在根据我的标准 #1 进行 CustID 的不同计数时,我将能够排除 CustID FFF 和 GGG:

Select 
Country
, o.Year
, o.Month
, sum(o.Sales) 
, sum(o.Points)
, sum(o.Sales + o.Points) as 'TestSUM'
, o.OrderType

From FactOrders O
Join CustTable as C on o.CustID = c.CustID
and o.OrderType in (1,2,3) 

Group by 
Country
, o.Year
, o.Month
, o.OrderType
, c.CustID

having sum(o.Sales + o.Points) <> 0   

Order by
c.CustID

但是,我希望结果实际上是这样的:

  Country   Year  Month  Distinct Count   Sales  
 Singapore  2018     10               6  2048.06 
 Singapore  2018     11               7  1996.61 

如何修改我的脚本以删除“CustID”、“Point”、“TestSUM”、“OrderType”列,但仍保持反射(reflect)标准的不同计数

having sum(o.Sales + o.Points) <> 0 

评论详情:

我希望对每个客户应用 sales+points <>0 标准,但是当我从分组中删除 CustID、Point、TestSum 和 OrderType 并选择 sales+points<>0 时,返回的结果不显示 CustID 的行标准不再适用于每个客户... CustID 的不同计数将包括 sales+points<>0

的行

最佳答案

也许你可以像下面这样做。请注意,我删除了 CustTable 的 JOIN,因为它没有被使用。如果 FactOrders 和 CustTable 之间没有引用完整性,您可以保留它。

我将您的必须移至您正在计算客户的位置,即使他们有一行是 0 和 0。

See live demo

  Select 
     Country=o.Country
     ,Year= o.Year
     ,Month= o.Month
     ,[Distinct Count]=Count(distinct custid)
     ,Sales= sum(o.Sales) 
From FactOrders O
where 
    o.OrderType in (1,2,3) and (o.Sales<>0 or o.Points<>0)
Group by 
   o.Country, o.Year, o.Month

关于sql - 根据其他几列的条件对不同的一列进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53866722/

相关文章:

c# - 指定的强制转换无效 - System.InvalidCastException

sql-server - 错误: There are uncommitted transactions

mysql - SQL 索引性能 - ASC 与 DESC

mysql - 使用 SQL 计算平面表中事件之间的平均时间

sql - 在数据库中查找不同时间之间的同时发生的事件

sql-server - T-SQL 相当于 Oracle 的 %TYPE 运算符?

sql-server - SQL 返回特定年份以来的年份列表

php - 如何使用 PHP 从数据库中获取单个数字

sql - 如何使用使用计数的 sql 子查询

c# - SQL Server 全文搜索 - 大型查询