2017-03-03 58 views
1

是否可以連接兩個具有不同字段值的表格?我使用MS SQL Server 2008的加入兩個不同字段值的表格

表A(original image,感謝@EdwardRusu的圖像轉換爲文本):

+ ----- + --------- + -------- + ----------- + ------------------ + ---------------- + ----------- + 
| RecID | Member ID | LoanType | LoanSubType | Application Number | Application Date | Loan Amount | 
+ ----- + --------- + -------- + ----------- + ------------------ + ---------------- + ----------- + 
| 3  | 00005  | Regular |    | 201604002   | 2016-02-28  | 39864.00 | 
| 185 | 00005  | Special | Special ... | 201604183   | 2016-10-31  | 10000.00 | 
| 318 | 00005  | Regular |    | 201605063   | 2016-05-18  | 39864.00 | 
| 427 | 00005  | Regular |    | 201608021   | 2016-08-18  | 39872.00 | 
| 486 | 00005  | Special | Special ... | 201609044   | 2016-09-07  | 10000.00 | 
| 589 | 00005  | Regular |    | 201611008   | 2016-11-04  | 39872.00 | 
| 689 | 00005  | Regular |    | 201702004   | 2017-02-02  | 39872.00 | 
+ ----- + --------- + -------- + ----------- + ------------------ + ---------------- + ----------- + 

表B(original image):

+ --------------- + --------- + ------ + ----- + ---------- + -------- + -------- + -------- + 
| ProjectAcctCode | Member ID | TMonth | TYear | TLastDate | TDebit | TCredit | TBalance | 
+ --------------- + --------- + ------ + ----- + ---------- + -------- + -------- + -------- + 
| 105350500  | 00005  | 1  | 2017 | 2017-01-31 | 0.00  | 2952.00 | -2952.00 | 
| 105350500  | 00005  | 5  | 2016 | 2016-05-31 | 73084.00 | 33220.00 | 39864.00 | 
| 105350500  | 00005  | 6  | 2016 | 2016-06-30 | 0.00  | 2951.42 | -2952.42 | 
| 105350500  | 00005  | 7  | 2016 | 2016-07-31 | 0.00  | 3014.14 | -3014.14 | 
| 105350500  | 00005  | 8  | 2016 | 2016-08-31 | 39872.00 | 33905.26 | 5973.55 | 
| 105350500  | 00005  | 9  | 2016 | 2016-09-30 | 0.00  | 2952.00 | -2952.00 | 
| 105350500  | 00005  | 10  | 2016 | 2016-10-31 | 0.00  | 3014.73 | -3014.73 | 
| 105350500  | 00005  | 11  | 2016 | 2016-11-30 | 39872.00 | 33905.26 | 5966.74 | 
| 105351000  | 00005  | 1  | 2017 | 2017-01-31 | 0.00  | 975.03 | -975.03 | 
| 105351000  | 00005  | 5  | 2016 | 2016-05-31 | 5000.00 | 1000.00 | 4000.00 | 
| 105351000  | 00005  | 6  | 2016 | 2016-06-30 | 0.00  | 1000.00 | -1000.00 | 
| 105351000  | 00005  | 7  | 2016 | 2016-07-31 | 0.00  | 1000.00 | -1000.00 | 
| 105351000  | 00005  | 8  | 2016 | 2016-08-31 | 0.00  | 1000.00 | -1000.00 | 
| 105351000  | 00005  | 9  | 2016 | 2016-09-30 | 10000.00 | 1000.00 | 9000.00 | 
| 105351000  | 00005  | 10  | 2016 | 2016-10-31 | 0.00  | 955.82 | -955.82 | 
| 105351000  | 00005  | 11  | 2016 | 2016-11-30 | 0.00  | 965.38 | -965.38 | 
+ --------------- + --------- + ------ + ----- + ---------- + -------- + -------- + -------- + 

我想根據MemberID,LoanType,ApplicationNo,ApplicationDate分組,從TableB中獲取總TBalance。應該根據LoanType和ProjAcctCode AND ApplicationDate和TLastDate連接這兩個表。 LoanType和ProjAcctCode具有不同的值。但是,「常規」Loantype等於「105350500」ProjAcctCode,「SPECIAL LoanType」等同於「105351000」ProjAcctCode。 ApplicationDate應該小於或等於TLastDate。

所以,如果我會爲「正規」的貸款類型的記錄,我應該有這樣的事情(original image):

+ -------- + -------- + ------------- + --------------- + ---------- + ------------ + --------- + --------- + 
| MemberID | LoanType | ApplicationNO | ApplicationDate | LoanAmount | ProjAcctCode | TLastDate | Balance | 
+ -------- + -------- + ------------- + --------------- + ---------- + ------------ + --------- + --------- + 
| 000005 | Regular | 201608021  | 8/18/2016  | 39,872.00 | 105350500 | 8/31/2016 | 39,871.44 | 
+ -------- + -------- + ------------- + --------------- + ---------- + ------------ + --------- + --------- + 

但隨着我的查詢,

SELECT a.MemberID, 
    (SELECT TOP (1) ApplicationNo 
     FROM TABLE A 
     WHERE (MemberID = a.MemberID) AND (ApplicationDate <= b.TLastDate) 
     ORDER BY ApplicationNo DESC) AS ApplicationNo, 
    (SELECT TOP (1) LoanAmount 
     FROM TABLE A AS SAL_APPLICATION_HEADER_1 
     WHERE (MemberID = a.MemberID) AND (ApplicationDate <= b.TLastDate) 
     ORDER BY ApplicationNo DESC) AS LoanAmount, 
    (SELECT TOP (1) ApplicationDate 
     FROM TABLE A AS SAL_APPLICATION_HEADER_2 
     WHERE (MemberID = a.MemberID) 
      AND (ApplicationDate <= b.TLastDate) 
     ORDER BY ApplicationNo DESC) AS ApplicationDate, 
    vwSAL_Balance_SL_1.ProjAcctCode, 
    b.TDebit, 
    b.TCredit, 
    b.TBalance AS Balance, 
    b.TLastDate 
    FROM TABLE A AS a 
    INNER JOIN TABLE B AS b ON a.MemberID = b.SLCode 
    GROUP BY a.MemberID, 
     b.TDebit, 
     b.TCredit, 
     b.TBalance, 
     b.ProjAcctCode, 
     b.TLastDate 
    HAVING (a.MemberID = N'00005') AND (b.TLastDate = '8/31/2016') 

我得到這個結果(original image):

+ -------- + ------------- + ---------- + --------------- + ------------ + -------- + -------- + -------- + ---------- + 
| MemberID | ApplicationNo | LoanAmount | ApplicationDate | ProjAcctCode | TDebit | TCredit | Balance | TLastDate | 
+ -------- + ------------- + ---------- + --------------- + ------------ + -------- + -------- + -------- + ---------- + 
| 00005 | 201608021  | 39872.00 | 2016-08-18  | 105351000 | 0.00  | 1000.00 | -1000.00 | 2016-08-31 | 
| 00005 | 201608021  | 39872.00 | 2016-08-18  | 105350500 | 39872.00 | 33898.45 | 5973.55 | 2016-08-31 | 
+ -------- + ------------- + ---------- + --------------- + ------------ + -------- + -------- + -------- + ---------- + 

這可能太長了,但請h ELP。謝謝。

+3

pelase考慮不發佈圖片並花費精力發佈DDL,DML for repro – TheGameiswar

+1

除了上述內容,請努力將代碼的實際格式設置爲易讀 – KindaTechy

+0

您的查詢引用表B中稱爲SL代碼的字段。請在您的表中包含該字段 – KindaTechy

回答

0

我只是在我的查詢中使用UNION(per LoanType)來獲得期望的結果。

0

tl; dr是的,這是可能的。最後的查詢塊會給你你想要的。

是的,只要您知道兩者之間存在某種關聯,就可以將具有不同字段值的兩個表連接起來。我會引導您完成加入聲明的構建,從而爲您提供所需的內容。您需要三樣東西:

1)匹配MEMBERID和SLCode

2)比賽LoanType和ProjAcctCode

3)匹配的日期


MEMBERID和SLCode

根據你上面的評論,我將假設在SLCode和B中的MemberID是一樣的。

select * 
    from A 
    inner join B 
     on B.MemberID = A.MemberID -- or B.SLCode = A.MemberID 


LoanType和ProjAcctCode

在這個例子中,要使用一個case語句中加入,像

inner join B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end 

這是說,如果LoanType是Regular,然後只加入具有ProjAcctCode = 105350500的記錄;否則,加入具有ProjAcctCode 105351000的記錄。(這裏,LoanType只有兩個狀態,所以使用else子句來捕獲特殊貸款是完全沒有問題的,如果有兩個以上的狀態,那麼你必須使用附加條件)。

select * 
    from A 
    inner join B 
     on B.MemberID = A.MemberID 
     and B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end 


日期

獲取日期過濾器右邊是立竿見影的。

select * 
    from A 
    inner join B 
     on B.MemberID = A.MemberID 
     and B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end 
     and A.ApplicationDate <= B.TLastDate 


集團和聚合

現在,你有表適當地連接,你只需要組和彙總。你可以對A中的任何東西進行分組,因爲這些字段不會改變你想要的聚合,你只需要在你從B中選擇東西時有點聰明。在你的問題中,它看起來像你想ProjAcctCode(這不會改變,所以我們也可以對此進行分組),TLastDate(這確實發生了變化,所以我們需要根據一些標準進行聚合)和TBalance(我們希望從中獲得總和)。我會假設你想要最新TLastDate,讓您的查詢將會像

select A.MemberID, 
     A.LoanType, 
     A.ApplicationNumber, 
     A.ApplicationDate, 
     [anything else you want from A], 
     B.ProjAcctCode -- aggregate not needed because this doesn't change 
     max(B.TLastDate) as TLastDate, 
     sum(B.TBalance) as TBalance 
    from A 
    inner join B 
     on B.MemberID = A.MemberID 
     and B.ProjAcctCode = case A.LoanType when 'Regular' then 105350500 else 105351000 end 
     and A.ApplicationDate <= B.TLastDate 
    group by A.MemberID, 
     A.LoanType, 
     A.ApplicationNumber, 
     A.ApplicationDate, 
     [anything else you selected from A], 
     B.ProjAcctCode 

這將產生如下表

+ -------- + -------- + ----------------- + --------------- + ------------ + ---------- + -------- + 
| MemberID | LoanType | ApplicationNumber | ApplicationDate | ProjAcctCode | TLastDate | TBalance | 
+ -------- + -------- + ----------------- + --------------- + ------------ + ---------- + -------- + 
| 00005 | Regular | 201604002   | 2016-02-08  | 105350500 | 2017-01-31 | 36913.19 | 
| 00005 | Special | 201604183   | 2016-10-31  | 105351000 | 2017-01-31 | -2896.23 | 
| 00005 | Regular | 201605063   | 2016-05-18  | 105350500 | 2017-01-31 | 36913.19 | 
| 00005 | Regular | 201608021   | 2016-08-18  | 105350500 | 2017-01-31 | 3014.75 | 
| 00005 | Special | 201609044   | 2016-09-07  | 105351000 | 2017-01-31 | 6103.77 | 
| 00005 | Regular | 201611008   | 2016-11-04  | 105350500 | 2017-01-31 | 3014.74 | 
+ -------- + -------- + ----------------- + --------------- + ------------ + ---------- + -------- + 

:與ApplicationNumber 201702004沒有按」貸款t出現在最終結果中,因爲它被日期過濾出來(即其應用日期大於B中的所有TLastDate)。

+0

謝謝你。我只是在我的查詢中使用UNION(按照LoanType)來獲得期望的結果。 – angel

相關問題