案例陈述 - 每个表中的列名必须是唯一的

时间:2015-10-20 16:45:45

标签: sql-server tsql

我正在编写sql来执行一些数据迁移和操作。我正在编写一些有点复杂的逻辑来从源数据映射到目标数据。从本质上讲,源中的两个字段将映射到目标中的一个字段,但显然某些字段优先。我的逻辑看起来有点像这样:

case
   when d.PURPOSE_NAME = 'Downsize' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Renewal'
   when d.PURPOSE_NAME = 'Early Renewal' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Renewal'
   --- more
end as Deal_Type__c,
case
   when d.PURPOSE_NAME = 'Downsize' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Downsize'
   when d.PURPOSE_NAME = 'Early Renewal' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Early Renewal'
   --- more
end as Deal_SubType__c,

-- these values take precedence
case
   when a.DEAL_TYPE_NAME = '30-Day Letter' then 'Amendment'
   when a.DEAL_TYPE_NAME = 'ACL' then 'Amendment'
   when a.DEAL_TYPE_NAME = 'Assignment' then 'Amendment'
end as Deal_Type__c,
case
   when a.DEAL_TYPE_NAME = '30-Day Letter' then 'Option to Relet'
   when a.DEAL_TYPE_NAME = 'ACL' then 'Termination'
   when a.DEAL_TYPE_NAME = 'Assignment' then 'Assignment'
end as Deal_SubType__c,

这个逻辑对我有意义,但我得到了错误:

  

每个表中的列名必须是唯一的。列名   &#39; Deal_Type__c&#39;在表格&#39;机会&#39;被指定不止一次。

如何使用TSQL将源数据中的两个字段映射到目标表中的一个字段,使用 case 语句?

2 个答案:

答案 0 :(得分:0)

你可能会使用类似的东西(简化演示,不需要CASE):

SELECT
  1 AS Deal_Type__c,
  2 AS Deal_Type__c
INTO Opportunity;
  

每个表中的列名必须是唯一的。列名称&#39; Deal_Type__c&#39;   在表格&#39;机会&#39;被指定不止一次。

问题在于你的别名,它们必须是截然不同的。

将别名更改为distinct_names以将两个值保存在同一行中:

SELECT
  1 AS Deal_Type__c_1,
  2 AS Deal_Type__c_2
INTO Opportunity;

使用UNION ALL将值保存在不同的行中:

SELECT
  1 AS Deal_Type__c
INTO Opportunity
UNION ALL
SELECT
  2 AS Deal_Type__c;

或优先(你的意思不是NULL)???

SELECT
   ISNULL( CASE WHEN ... END,
           CASE WHEN ... END ) AS Deal_Type__c
INTO Opportunity;

像:

SELECT ISNULL(case
               when a.DEAL_TYPE_NAME = '30-Day Letter' then 'Amendment'
               when a.DEAL_TYPE_NAME = 'ACL' then 'Amendment'
               when a.DEAL_TYPE_NAME = 'Assignment' then 'Amendment'
              end,
              case
                when d.PURPOSE_NAME = 'Downsize' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Renewal'
                when d.PURPOSE_NAME = 'Early Renewal' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Renewal'
                --- more
             end) AS Deal_Type__c
    -- same for Deal_SubType__C
INTO Opportunity;

修改

使用HABO的建议你可以将两种情况结合起来:

SELECT case
         when a.DEAL_TYPE_NAME = '30-Day Letter' then 'Amendment'
         when a.DEAL_TYPE_NAME = 'ACL' then 'Amendment'
         when a.DEAL_TYPE_NAME = 'Assignment' then 'Amendment'
         when d.PURPOSE_NAME = 'Downsize' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Renewal'
         when d.PURPOSE_NAME = 'Early Renewal' and (a.DEAL_TYPE_NAME <> '30-Day Letter' and a.DEAL_TYPE_NAME <> 'ACL' and a.DEAL_TYPE_NAME <> 'Assignment') then 'Renewal'
         --- more
        end AS Deal_Type__c
      -- same for Deal_SubType__C
INTO Opportunity;

答案 1 :(得分:0)

错误是因为第一个和第三个案例有&#34; Deal_Type__c&#34;作为列名,与第二和第四名相同,使用&#34; Deal_SubType__c&#34;。

如果您需要两个(或更多)数据集,则需要相同的列,例如UNION语句