我可以在案例陈述中使用“AS”关键字吗

Posted

技术标签:

【中文标题】我可以在案例陈述中使用“AS”关键字吗【英文标题】:Can I use "AS" Keyword in Case Statement 【发布时间】:2015-01-13 10:57:57 【问题描述】:

我想创建一个包含两个新列 SC 和 NC 的视图。

我想编写一个比较 country1 和 Supplier Country 的逻辑,如果匹配,那么我想比较 MOQ。如果匹配,那么我希望将相应 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

【问题讨论】:

【参考方案1】:

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

你不能这样做,你只能在 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

【讨论】:

错误:每个视图或函数中的列名必须是唯一的。视图或函数“viewCountry”中的列名“SC”被指定了多次。 每行都有几条重复记录。我得到 3 条记录。我从 Union ALL 更改为 Union 如果没有 UNION ALL 或 UNION.,我怎样才能得到相同的结果?我不想使用联合,因为它会给出多个结果。我想一个人的时候用简单的盒子。 使用 group by 或 distinct =x,你可以试试,或者我可以为 u =D 这样做

以上是关于我可以在案例陈述中使用“AS”关键字吗的主要内容,如果未能解决你的问题,请参考以下文章

我可以在案例陈述中使用.include?()吗?红宝石

选择案例陈述和数据验证

投英文会议需要去陈述吗

为什么我们需要突破案例陈述?

Verilog 中的案例陈述?

SQL关键字“AS”的用途是啥?