2014-09-29 85 views
0

我想將兩張臨時表加入一張大臨時表。我想從我的第一個臨時表中獲取所有記錄,並且僅當我的第一個臨時表中不存在ProductID時,才需要第二個臨時表中的記錄。加入兩張臨時表

第一臨時表:

--define temporary table 
declare @tmp table (
     ManagerID int null, 
     ManagerName varchar(250), 
     ProductID int null, 
     ProductName varchar(250), 
     RFIFixedIncomeAttributionID int null, 
     Value decimal(8,4) null, 
     Name varchar(250), 
     Sector varchar(250) 
    ) 

--populate temp table 
insert into @tmp 

select 
m.ManagerID, m.ManagerName, p.ID as 'ProductID', p.ProductName, sa.RFIFixedIncomeAttributionID, sa.Value, sc.Name, 

case when gm.GeographicMandateID = 2 and s1.SubType1ID = 10 and s2.SubType2ID = 39 then 'Core' 
    when gm.GeographicMandateID = 2 and s1.SubType1ID = 10 and s2.SubType2ID = 38 then 'Intermediate' 
end as 'Sector' 

from Products p 

join Managers m on m.ManagerID = p.ManagerID 
left join RFIFixedIncomeAttribution fia on fia.ParentID = p.ID and fia.ParentTypeID = 26 
left join RFIFixedIncomeSectorAllocation sa on sa.RFIFixedIncomeAttributionID = fia.ID 
    and sa.RFIFixedIncomeDataTypeID = 1 
join RFIFixedIncomeSectorCategories sc on sc.ID = sa.RFIFixedIncomeSectorCategoryID 
join SubType1 s1 on s1.SubType1ID = p.SubType1ID 
join SubType2 s2 on s2.SubType2ID = p.SubType2ID 
join GeographicMandates gm on gm.GeographicMandateID = p.GeographicMandateID 

where p.prodclasscategoryid = 4 
and fia.year = 2014 
and fia.quarter = 6/3 
and p.Rank = 1 

order by m.ManagerName, p.ProductName 

--get filtered dataset 
select * from @tmp 
where 
Sector in ('Core') 

二臨時表:

--define temporary table 
declare @tmp2 table (
     ManagerID int null, 
     ManagerName varchar(250), 
     ProductID int null, 
     ProductName varchar(250), 
     RFIFixedIncomeAttributionID int null, 
     Value decimal(8,4) null, 
     Name varchar(250), 
     Sector varchar(250) 
    ) 

--populate temp table 
insert into @tmp2 

select 
m.ManagerID, m.ManagerName, p.ID as 'ProductID', p.ProductName, sa.RFIFixedIncomeAttributionID, sa.Value, sc.Name, 

case when gm.GeographicMandateID = 2 and s1.SubType1ID = 10 and s2.SubType2ID = 39 then 'Core' 
    when gm.GeographicMandateID = 2 and s1.SubType1ID = 10 and s2.SubType2ID = 38 then 'Intermediate' 
end as 'Sector' 

from Products p 

join Managers m on m.ManagerID = p.ManagerID 
join Vehicles v on v.ProductID = p.ID 
join ManagerAccounts ma on ma.VehicleID = v.ID 
join Accounts a on a.MgrAccountID = ma.MgrAccountID 
left join RFIFixedIncomeAttribution fia on fia.ParentID = a.AccountID and fia.ParentTypeID = 6 
left join RFIFixedIncomeSectorAllocation sa on sa.RFIFixedIncomeAttributionID = fia.ID 
    and sa.RFIFixedIncomeDataTypeID = 1 
join RFIFixedIncomeSectorCategories sc on sc.ID = sa.RFIFixedIncomeSectorCategoryID 
join SubType1 s1 on s1.SubType1ID = p.SubType1ID 
join SubType2 s2 on s2.SubType2ID = p.SubType2ID 
join GeographicMandates gm on gm.GeographicMandateID = p.GeographicMandateID 

where p.prodclasscategoryid = 4 
and fia.year = 2014 
and fia.quarter = 6/3 
and p.Rank = 1 

order by m.ManagerName, p.ProductName 

--get filtered dataset 
select * from @tmp2 
where 
Sector in ('Core') 
+2

OK。什麼阻止你? – 2014-09-29 22:16:31

+2

這些是表變量而不是臨時表,表格表以'#'或'##'開始,這些是表變量,因爲它們被命名爲'@ tablename' – 2014-09-29 22:16:44

+1

沒有解析牆O代碼,是否有任何反對'工會全部'?另外,請選擇一個平臺。 Mysql和Sql Server是2個不同的DMBS的 – paqogomez 2014-09-29 22:19:31

回答

1

那些已經長大

  • 聯盟幾點是你想要的項,聯接是完全不同的東西。

  • 您不使用臨時表,您正在使用表變量。不完全相同的東西

  • mysql和mssql是不一樣的東西,標記你的問題作爲一個或另一個,而不是兩個。

    select * from @tmp 
    union all 
    select * from @tmp2 where productID not in (select productID from @tmp) 
    

不知道如果我靠這個查詢在MySQL,因爲它會在條款與不爭...你可以使用join語法茉莉花的答案的後半部分聯盟條款。 OK。

+0

我沒有真正考慮代碼,我認爲它搞砸了。我只看着他想要的結果。這是一個已知的問題。我發佈的樣本是一個已知的解決方案,但不是唯一的解決方案。它是ANSI標準,所以它應該在任何ANSI-sql RDBMS中工作。我的回答是我在這裏拼命試圖做的事情 - 教授編碼,而不是爲他們編寫代碼。 – Jasmine 2014-09-29 23:09:26

+0

我同意......只是覺得他的問題更多是因爲誤會了聯盟vs聯盟 – Twelfth 2014-09-29 23:30:11

+0

很難說。我通常解釋說JOIN將表並排放置,UNION將它們按順序排列。 – Jasmine 2014-09-29 23:54:18

0

這是「發現與其他表中沒有匹配的所有行」的情況下,我們有專門的模式。首先,交換你的表 - 你期望丟失行的表將是第二個或RIGHT表,另一個是LEFT表。

select <columns> 
from table1 
LEFT OUTER JOIN table1.ID = table2.ID 
where table2.ID IS NULL 

OR ...不交換表並使用RIGHT OUTER JOIN - 但這是非標準的。

在代碼中,有一個問題...

and fia.quarter = 6/3 

等同於:

and fia.quarter = 2 

我想你需要一些引號那裏。