-1
我想創建一個視圖,其中包含兩個新列作爲SC和NC。我可以在Case Statement中使用「AS」關鍵字
我想寫一個比較country1和Supplier Country的邏輯,如果匹配那麼我想要MOQ進行比較。如果匹配,那麼我希望將相應起訂量的成本作爲NC存儲在視圖中,SC
這是否適合做這件事。最多有3個國家,5個Moq,3個供應商國家,3個供應商MOQ。
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
錯誤:每個視圖或函數中的列名必須是唯一的。視圖中的列名'SC'或函數'viewCountry'被多次指定。 – Seeker
oops,代碼已更新,忘記了這是一個視圖。 –
我得到幾行重複記錄。我得到3條記錄。我從聯盟全部更改爲聯盟 – Seeker