2012-09-27 176 views
0

我需要從兩個不同的表中獲取名爲SymNumber的列。兩者都將使用類似的標準進行選擇。以下是我迄今爲止,它不工作...從兩個不同的表中選擇相同的列T-SQL

DECLARE @date datetime 

SELECT @date = LastChangeDate 
FROM QueueUpdates 

SELECT DISTINCT u.CompanyCode, u.AgentId 
FROM SymNumberToAgentId u, AgentIdToTradingPartner a, TradingPartner t, AgentContEd c 
WHERE u.SymNumber in 
(SELECT SymNumber FROM 
(SELECT a.SymeNumber, c.SymNumber 
FROM AgentProductTraining a, AgentContEd c 
WHERE a.LastChangeDate >= @date and c.LastChangeDate >= @date) a) and 
u.AgentId = a.AgentId and a.TradingPartnerCode = t.TradingPartnerCode and t.TradingPartnerCode = 'SE2' 

GO 

讓我頭疼的部分是內部查詢下面列出。我認爲調整這部分將允許我從兩個表中獲取SymNum列。

(SELECT SymetraNumber FROM 
(SELECT a.SymetraNumber, c.SymetraNumber 
FROM AgentProductTraining a, AgentContEd c 
WHERE a.LastChangeDate >= @date and c.LastChangeDate >= @date) a) 

回答

4

UNIONUNION ALL看起來不錯的選擇在這裏:

(
    SELECT SymetraNumber 
    FROM AgentProductTraining a 
    WHERE a.LastChangeDate >= @date 
    UNION 
    SELECT SymetraNumber 
    FROM AgentContEd c 
    WHERE c.LastChangeDate >= @date 
) 
相關問題