为“CTE_YYYY”多次指定“xxxx”列

时间:2016-03-04 18:47:15

标签: sql-server

我是Microsoft SQL Server的新手。我正在尝试使用CampaignID加入两个具有名为LEFT OUTER JOIN的公用密钥的表。我需要在不同的查询中重用结果,因此我决定使用CTE_Results捕获结果集。例如,

-- This is my CTE script
WITH CTE_Results AS
(
  SELECT t1.CampaignID, t2.CampaignID, t1.Name, t2.Vendor
  FROM CampaignDetails AS t1
  LEFT OUTER JOIN CampaignOnlineDetails AS t2
  ON t1.CampaignID = t2.CampaignID
)

-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results

但是,当我跑到上面时,我得到了:

The column `CampaignID` was specified multiple times for `CTE_Results`. 

从阅读旧的StackOverflow问题和答案开始,似乎自CampaignID两个表中的CampaignID之后,我必须使用表别名来指定我想要的哪个(哪个表)SELECT ForwardedPort port = new ForwardedPortDynamic("127.0.0.1", 5011); // proxy SshClient client = new SshClient("192.32.2.1", "admin", "admin"); //ssh client.KeepAliveInterval = new TimeSpan(0, 0, 60); client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 60); try { client.ErrorOccurred += delegate (object sender, Renci.SshNet.Common.ExceptionEventArgs e) { Console.WriteLine("Client error: " + e.Exception.Message); }; client.HostKeyReceived += delegate (object sender, Renci.SshNet.Common.HostKeyEventArgs e) { Console.WriteLine("Connected to server"); Console.WriteLine("Key Exchanged"); }; port.RequestReceived += delegate (object sender, Renci.SshNet.Common.PortForwardEventArgs e) { Console.WriteLine("Requested connection to " + e.OriginatorHost + ":" + e.OriginatorPort); }; client.Connect(); if (client.IsConnected) { client.SendKeepAlive(); client.AddForwardedPort(port); port.Start(); HttpWebRequest rq = (HttpWebRequest)HttpWebRequest.Create("http://whoer.net"); rq.Method = "GET"; rq.ContentType = "application/x-www-form-urlencoded"; using (HttpWebResponse rp = (HttpWebResponse)rq.GetResponse()) { string result = new StreamReader(rp.GetResponseStream()).ReadToEnd(); Console.WriteLine(result); }; } else { Console.WriteLine("Client did not connect"); } } catch (Exception e) { Console.WriteLine(e); } 。但我认为我做到了这一点,甚至看起来错误仍然存​​在。

我有没有办法在我的CTE中选择并保留两个CampaignID?如果是这样,应该改变什么?谢谢你的答案!

2 个答案:

答案 0 :(得分:5)

您在CTE中选择了CampaignID两次,使用不同的alias name来解决问题

WITH CTE_Results
     AS (SELECT t1.CampaignID AS cd_CampaignID,
                t2.CampaignID AS cod_CampaignID,
                t1.NAME,
                t2.Vendor
         FROM   CampaignDetails AS t1
                LEFT OUTER JOIN CampaignOnlineDetails AS t2
                             ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM   CTE_Results 

或使用此

WITH CTE_Results(cd_CampaignID, cod_CampaignID, NAME, Vendor)
     AS (SELECT t1.CampaignID,
                t2.CampaignID,
                t1.NAME,
                t2.Vendor
         FROM   CampaignDetails AS t1
                LEFT OUTER JOIN CampaignOnlineDetails AS t2
                             ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM   CTE_Results 

答案 1 :(得分:1)

您需要对CTE中的CampaignID列进行别名,或者在CTE声明中定义返回的列名称。否则就像创建一个包含两个具有相同名称的列的表。

示例列别名:

WITH CTE_Results AS
(
  SELECT t1.CampaignID as 'CampaignID1', t2.CampaignID as 'CampaignID2', t1.Name, t2.Vendor
  FROM CampaignDetails AS t1
  LEFT OUTER JOIN CampaignOnlineDetails AS t2
  ON t1.CampaignID = t2.CampaignID
)

或在CTE声明中:

WITH CTE_Results (CampaignID1, CampaignID2, [Name], Vendor) AS 
(
  SELECT t1.CampaignID, t2.CampaignID , t1.Name, t2.Vendor
  FROM CampaignDetails AS t1
  LEFT OUTER JOIN CampaignOnlineDetails AS t2
  ON t1.CampaignID = t2.CampaignID
)