【问题标题】:Count distinct one column based on criteria on another few columns根据其他几列的条件计算不同的一列
【发布时间】:2019-05-20 20:38:10
【问题描述】:

我有样本数据:: 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”列,但仍保持我的不同计数反映标准

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

评论详情:

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

的行

【问题讨论】:

    标签: sql sql-server tsql


    【解决方案1】:

    也许你可以像下面那样做。请注意,我删除了 JOIN 到 CustTable,因为它没有被使用。如果 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
    

    【讨论】:

      【解决方案2】:

      这会产生您的演示结果。

      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 + o.Points<>0
      Group by 
         o.Country, o.Year, o.Month
      

      但是,这不能满足您声明的要求,即销售额和积分可能不都是 0。如果销售额 = -10 且积分 = 10,即使两者都不等于 0,这也将加起来为 0。

      以下应该是您要查找的内容:

      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 and o.Points <> 0    --  because, -10 + 10 = 0
      Group by 
         o.Country, o.Year, o.Month
      

      【讨论】:

      • 你对第一个选项是正确的,我错过了,但对于这个例子,它给出了正确的答案,而第二个脚本产生了正确的 CustID 计数但错误的销售额总和,因为它排除了行其中只有销售额 =0 或积分 =0
      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 2013-04-15
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多