是否可以連接兩個具有不同字段值的表格?我使用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。謝謝。
pelase考慮不發佈圖片並花費精力發佈DDL,DML for repro – TheGameiswar
除了上述內容,請努力將代碼的實際格式設置爲易讀 – KindaTechy
您的查詢引用表B中稱爲SL代碼的字段。請在您的表中包含該字段 – KindaTechy