2010-10-20 31 views
0

表1(歷史數據)我想表明從1排

SiteName OutcomeType SpeciesType  Count DateType 
------------------------------------------------------------- 
S1   Adopted   Dog    3  0 
S2   Adopted   Cat    12  0 
S1   Transferred  Puppy   2  0 
S1   Transferred  Dog    5  0 

表2(當前數據)

SiteName OutcomeType  SpeciesType  Count DateType 
-------------------------------------------------------------- 
S1   Adopted   Dog    9  1 
S2   Adopted   Dog    10  1 
S1   Transferred  Dog    12  1 

** DateType記錄:0,表示記錄日期,1-當前日期
我將上述兩個表格數據合併到一個表格中,並將其稱爲主表格(這是存儲過程中的臨時表格)。其中將如下所示。

SiteName OutcomeType  SpeciesType  Count DateType 
------------------------------------------------------------- 
S1   Adopted   Dog    3  0 
S2   Adopted   Cat    12  0 
S1   Adopted   Dog    9  1 
S2   Adopted   Dog    10  1 
S1   Transferred  Puppy   2  0 
S1   Transferred  Dog    5  0 
S1   Transferred  Dog    12  1 

這裏基本上我試圖對歷史和當前的數據
用戶將進入2個日期比較(如:Oct2009,Oct2010)如上圖所示
所有數據現在是在1個表提取。

從主表(存儲過程中的臨時表)中注意到,
有一種物種類型:站點S1的Puppy,它來自歷史日期。
我該如何匹配當前表格記錄中的記錄(物種類型Puppy不存在的地方)。

我想在1行中顯示上述記錄,如下所示。

SiteName1 OutecomeType1 AgeSpecies1 Count1 History SiteName2 OutcomeType2 AgeSpecies2 Count2 Current Difference 
S1 Adopted Dog 3 yes S1 Adopted Dog 9 yes 6 
S1 Transferred Puppy 2 yes - - - - - - 
S2 Adopted Cat 12 yes - - - - - - 
S1 Transferred Dog 5 yes S1 Transferred Dog 12 yes 7 
- - - - - S2 Adopted Dog 10 yes 

10 

回答

1

我會爲每個表編寫一個查詢,以獲取該表的基本計數以用作派生表。然後,我將使用完整的外連接將這兩個表連接在一起。然後只需從每個派生表中抽取所需的字段。只要確保派生表查詢只爲每個siteName拉一行。