我可以在 Case 语句中使用 "AS" 关键字吗

Can I use "AS" Keyword in Case Statement

我想创建一个视图,其中包含两个新列:SC 和 NC。

我想写一个比较 country1 和 Supplier Country 的逻辑,如果匹配则我想要比较 MOQ。如果匹配,那么我希望将相应最小起订量的成本存储在 View as NC, SC

这个方法适合做那个吗。

最多 3 个国家,5 个最小起订量,3 个供应商国家,3 个供应商最小起订量。

create view viewCountry as
select dbo.SCN_Vw_ProjectDetailsReport_Revision.*,'' as SC, '' as NC,
case 
when 
country1=supplier_country1 
then 
(
case 
when 
lot_size=negotiated_lot_size then lot_size1_1 as SC, Negotiated_cost as NC
when 
moq2=negotiated_lot_size then lot_size2_1 as SC, Negotiated_cost as NC
when 
moq3=negotiated_lot_size then lot_size5_1 as SC, Negotiated_cost as NC
when 
moq4=negotiated_lot_size then lot_size10_1 as SC, Negotiated_cost as NC
when 
moq5=negotiated_lot_size then lot_size25_1 as SC, Negotiated_cost as NC
else NULL as SC, NULL as NC
) end as SC,NC
when 
country1=supplier_country2 
then
(
case 
when 
lot_size=negotiated_lot_size2 then lot_size1_1 as SC, Negotiated_cost2 as NC
when 
moq2=negotiated_lot_size2 then lot_size2_1 as SC, Negotiated_cost2 as NC
when 
moq3=negotiated_lot_size2 then lot_size5_1 as SC, Negotiated_cost2 as NC
when 
moq4=negotiated_lot_size2 then lot_size10_1 as SC, Negotiated_cost2 as NC
when 
moq5=negotiated_lot_size2 then lot_size25_1 as SC, Negotiated_cost2 as NC
else NULL as SC, NULL as NC
)
when 
country3=supplier_country3 
then
(
case 
when 
lot_size=negotiated_lot_size3 then lot_size1_1 as SC, Negotiated_cost3 as NC
when 
moq2=negotiated_lot_size3 then lot_size2_1 as SC, Negotiated_cost3 as NC
when 
moq3=negotiated_lot_size3 then lot_size5_1 as SC, Negotiated_cost3 as NC
when 
moq4=negotiated_lot_size3 then lot_size10_1 as SC, Negotiated_cost3 as NC
when 
moq5=negotiated_lot_size3 then lot_size25_1 as SC, Negotiated_cost3 as NC
else NULL as SC, NULL as NC
)
from SCN_Vw_ProjectDetailsReport_Revision

你不能那样做,你只能在 'End' CASE 语句的末尾设置你的列名,下面是我最好的示例,可以通过你提供的信息创建。

你不能那样做,你只能在 'End' CASE 语句的末尾设置你的列名,下面是我最好的示例,可以通过你提供的信息创建。

create view viewCountry as
select dbo.SCN_Vw_ProjectDetailsReport_Revision.*,'' as SC, '' as NC,
    from SCN_Vw_ProjectDetailsReport_Revision
    UNION ALL

select dbo.SCN_Vw_ProjectDetailsReport_Revision.*,
 case 
when 
country1=supplier_country1 
then 
(
case 
when 
lot_size=negotiated_lot_size then lot_size1_1
when 
moq2=negotiated_lot_size then lot_size2_1
when 
moq3=negotiated_lot_size then lot_size5_1
when 
moq4=negotiated_lot_size then lot_size10_1
when 
moq5=negotiated_lot_size then lot_size25_1
else NULL END
) END 'SC',
 case 
when 
country1=supplier_country1 
then 
(
case 
when 
lot_size=negotiated_lot_size then Negotiated_cost 
when 
moq2=negotiated_lot_size then Negotiated_cost 
when 
moq3=negotiated_lot_size then Negotiated_cost 
when 
moq4=negotiated_lot_size then Negotiated_cost 
when 
moq5=negotiated_lot_size then Negotiated_cost 
else NULL END
) END 'NC'


    from SCN_Vw_ProjectDetailsReport_Revision
    UNION ALL

       select dbo.SCN_Vw_ProjectDetailsReport_Revision.*,
  ,    
CASE 
when 
country1=supplier_country2 
then
(
case 
when 
lot_size=negotiated_lot_size2 then lot_size1_1
when 
moq2=negotiated_lot_size2 then lot_size2_1
when 
moq3=negotiated_lot_size2 then lot_size5_1 
when 
moq4=negotiated_lot_size2 then lot_size10_1 
when 
moq5=negotiated_lot_size2 then lot_size25_1 
else NULL  END
) END 'SC'
,

CASE 
when 
country1=supplier_country2 
then
(
case 
when 
lot_size=negotiated_lot_size2 then Negotiated_cost2 
when 
moq2=negotiated_lot_size2 then Negotiated_cost2 
when 
moq3=negotiated_lot_size2 then Negotiated_cost2  
when 
moq4=negotiated_lot_size2 then Negotiated_cost2  
when 
moq5=negotiated_lot_size2 then Negotiated_cost2  
else NULL  END
) END 'NC'


    from SCN_Vw_ProjectDetailsReport_Revision
    UNION ALL

       select dbo.SCN_Vw_ProjectDetailsReport_Revision.*,
    CASE
when 
country3=supplier_country3 
then
(
case 
when 
lot_size=negotiated_lot_size3 then lot_size1_1
when 
moq2=negotiated_lot_size3 then lot_size2_1 
when 
moq3=negotiated_lot_size3 then lot_size5_1 
when 
moq4=negotiated_lot_size3 then lot_size10_1 
when 
moq5=negotiated_lot_size3 then lot_size25_1 
else NULL END
) END 'SC'
,

CASE
when 
country3=supplier_country3 
then
(
case 
when 
lot_size=negotiated_lot_size3 then Negotiated_cost3 
when 
moq2=negotiated_lot_size3 then Negotiated_cost3  
when 
moq3=negotiated_lot_size3 then Negotiated_cost3  
when 
moq4=negotiated_lot_size3 then Negotiated_cost3  
when 
moq5=negotiated_lot_size3 then Negotiated_cost3  
else NULL END
) END 'NC'


    from SCN_Vw_ProjectDetailsReport_Revision