如何根据另一列的方差值用“是”或“否”填充列

时间:2019-06-07 19:57:53

标签: sql-server case multiple-conditions

我试图基于“差异”列中归因于同一个月同一客户的3种产品的一系列值来填充“移动”列中的“是”或“否”。

在标题为“ Expected Example Result”的图片中,Customer1和第3个月的差异列,您可以看到Customer1在product1上花费了22.24美元,在product2上花费了655.53美元,在product3上花费了319.28美元,这意味着他们将钱从product1和2,然后放入3。

问题是我主要关注从产品3到产品2和1的资金转移,而不是资金如何从产品 转移到

下面的代码是一个可重现的最小工作示例,部分示例是根据我在此处提出的先前问题进行部分修改的。

Need help converting an excel if and sumif formula to my SQL Server code

基本上,代码说明方差的最大值为正,最小值为负,然后标记为“是”,否则为“否”。在某些情况下,即使它是最大方差,我们也不希望“是”,例如,如果product3是最大方差且为正,或者如果product2为负,我们总是希望“否”。

否则,该代码将起作用,除非product1为负且product3为正时,它将product1标记为“是”,而我无法弄清楚如何覆盖它并将其标记为“否”。

我尝试过

when Variance < 0 and Product = 'Product1' 
and Max(Variance) over (partition by Customer, Mnth) > 0 
and Product = 'Product3' 
then 'No'

也尝试过

when Variance < 0 and Product = 'Product1' 
and Variance > 0 and Product = 'Product3' 
then 'No'

希望我还没有解释清楚,但是让我知道是否需要澄清。

with

baseData as 

            (

            Select  Customer, Product,  Month(TransactionDate) as Mnth, sum(TransactionCharges + OtherCharges) as Charges,

                    case when Year(TransactionDate) = 2018 then 'prior'
                    else 'curr' 
                    end as Status

            From Storedfunction1 ('01-01-2018','12-31-2019')
                    where Customer <> 'internal'   --EXCLUDE INTERNAL CUSTOMERS
                    and Product in ('Product1', 'Product2', 'Product3')                     
                    and Month(TransactionDate) < 5

            group by Customer, Product, Month(TransactionDate), Year(TransactionDate), TransactionCharges, OtherCharges

            ),

getVariances as 
            (
            Select Customer, Product, Mnth,

                        isnull(sum(case when Status = 'curr' then Charges end),0) - isnull(sum(case when Status = 'prior' then Charges end),0)  as Variance, 
            case  

                when    isnull(sum(case when Status = 'prior' then Charges end),0) = 0 and isnull(sum(case when Status = 'curr' then Charges end),0) > 0 then 'New'
                when    isnull(sum(case when Status = 'prior' then Charges end),0) >= 0 and isnull(sum(case when Status = 'curr' then Charges end),0) <= 0 then 'Terminated'
                else 'Current'

            end as Standing,

            case  

                when    isnull(sum(case when Status = 'prior' then Charges end),0) > 0 and isnull(sum(case when Status = 'curr' then Charges end),0) - isnull(sum(case when Status = 'prior' then Charges end),0) > 0 then 'Growth'
                when    isnull(sum(case when Status = 'prior' then Charges end),0) > 0 and isnull(sum(case when Status = 'curr' then Charges end),0) - isnull(sum(case when Status = 'prior' then Charges end),0) < 0 then 'Attrited'
                when    isnull(sum(case when Status = 'prior' then Charges end),0) >= 0 and isnull(sum(case when Status = 'curr' then Charges end),0) - isnull(sum(case when Status = 'prior' then Charges end),0) = 0 then 'Static'
                when    isnull(sum(case when Status = 'prior' then Charges end),0) < 0 and isnull(sum(case when Status = 'curr' then Charges end),0) - isnull(sum(case when Status = 'prior' then Charges end),0) > 0 then 'Growth'
                when    isnull(sum(case when Status = 'prior' then Charges end),0) <= 0 and isnull(sum(case when Status = 'curr' then Charges end),0) - isnull(sum(case when Status = 'prior' then Charges end),0) < 0 then 'Attrited'
                else 'NA'

            end as GrowOrAttrit

            From baseData

            group by Customer, Product, Mnth

             )

            Select *, 

                case    

                    when GrowOrAttrit = 'Static' then 'No'                                      
                    when Variance < 0 and Product = 'Product2' then 'No'                        
                    when Min(Variance) over (partition by Customer, Mnth) > 0 and Max(Variance) over (partition by Customer, Mnth) > 0 then 'No'                                                                        
                    When Max(Variance) over (partition by Customer, Mnth) > 0 and Min(Variance) over (partition by Customer, Mnth) < 0 and Product = 'Product2' then 'Yes'
                    when Variance >= 0 and Standing = 'New' then 'No'
                    when Variance >= 0 then 'No'
                    when Max(Variance) over (partition by Customer, Variance) > 0 and Product = 'Product1' then 'No'
                    when Max(Variance) over (partition by Customer, Mnth) > 0 then 'Yes'
                    when Max(Variance) over (partition by Customer, Mnth) > 0 and Product = 'Product1' then 'No'

                    else 'No'

            end as Move

            From getVariances       

            order by 1,2,3

预期的示例结果

Expected

实际结果

Actual

0 个答案:

没有答案