根据另外几列上的条件计算不同的一列

时间:2018-12-20 10:22:43

标签: 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',用于将'Sales'和'Points'不等于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”,“点”,“ TestSUM”,“ OrderType”列,但仍保持反映标准的不同计数

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

评论详情:

我希望每位客户应用sales + points <> 0条件,但是当我从group by中删除CustID,Point,TestSum和OrderType并选择sales + points <> 0时,返回的结果不按CustID显示行标准不再适用于每个客户... CustID的独特计数将包括其中sales + points <> 0

的行

2 个答案:

答案 0 :(得分:0)

可能您可以按照以下步骤进行操作。请注意,由于未使用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

答案 1 :(得分:0)

这会产生演示结果。

SELECT * 
FROM `user_notifications` 
WHERE DATE_FORMAT(created_at, '%Y') = '2019' 
ORDER BY `created_at` DESC
LIMIT 0,10

但是,这不能满足您所说的“销售”和“积分”可能都不都是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 + o.Points<>0
Group by 
   o.Country, o.Year, o.Month