2 case statements which are linked

后端 未结 2 916
滥情空心
滥情空心 2021-01-26 01:57

I am relatively new in SQL coding and have a question regarding case statements.

What I want to achieve: I want to create a query being used for calculating correct obso

相关标签:
2条回答
  • 2021-01-26 02:21

    You don't have access to the aliases in the select clause in the same select clause. There is a simple fix and that is using a derived table or a common table expression:

    ;with cte as
    (
        Select 
            [Material]
            ,[Plnt]
            ,case
                when [calculate 5-year demand] = 0
                    then 9.01
                when  [BAAS SO GI (601) 36] = 0
                    then 9.01
                when [MS] <> 'BI' or [MS] <> 'BO' 
                    then ([Stock all SP WH]/([calculate 5-year demand]/5))
                when [MS] = 'BO'
                    then ([Stock all SP WH]/[BAAS SO GI (601) 36])
                when [MS] ='BI'
                    then 0
                else 9.01
            end as [Inventory Reach]
        from [BAAS_PowerBI].[dbo].[Obs]
    )
    
    select [Material]
            ,[Plnt]
            ,[Inventory Reach]
            ,case
                when [Inventory Reach] > 9
                    then 1
                else 0.9
            end as [Devaluation Class]
    from cte
    

    The above code is using a common table expression which is (in this schenario) basically the same as using a derived table, only more readable. Here's the derived table equivalent:

    select [Material]
            ,[Plnt]
            ,[Inventory Reach]
            ,case
                when [Inventory Reach] > 9
                    then 1
                else 0.9
            end as [Devaluation Class]
    from 
    (
        Select 
            [Material]
            ,[Plnt]
            ,case
                when [calculate 5-year demand] = 0
                    then 9.01
                when  [BAAS SO GI (601) 36] = 0
                    then 9.01
                when [MS] <> 'BI' or [MS] <> 'BO' 
                    then ([Stock all SP WH]/([calculate 5-year demand]/5))
                when [MS] = 'BO'
                    then ([Stock all SP WH]/[BAAS SO GI (601) 36])
                when [MS] ='BI'
                    then 0
                else 9.01
            end as [Inventory Reach]
        from [BAAS_PowerBI].[dbo].[Obs]
    ) derived
    
    0 讨论(0)
  • 2021-01-26 02:26

    A column alias is not permitted in the FROM, WHERE, GROUP BY, or HAVING clauses. You can use a subquery or CTE, but SQL Server supports lateral joins via the APPLY keyword. This can be quite useful for introducing a column alias:

    select o.Material, o.Plnt, v.[Inventory Reach],
           (case when v.[Inventory Reach] > 9
                 then 1
                 else 0.9
            end) as [Devaluation Class]
    from [BAAS_PowerBI].[dbo].[Obs] o cross apply
         (values (case when o.[calculate 5-year demand] = 0
                       then 9.01
                       when o.[BAAS SO GI (601) 36] = 0
                       then 9.01
                       when o.[MS] in ('BI', 'BO')
                       then (o.[Stock all SP WH] / (o.[calculate 5-year demand] / 5))
                       when o.[MS] = 'BO'
                       then (o.[Stock all SP WH] / o.[BAAS SO GI (601) 36])
                       when o.[MS] ='BI'
                       then 0
                      else 9.01
                  end
                 )
         ) values([Inventory Reach])
    
    0 讨论(0)
提交回复
热议问题