2012-06-06 40 views
5

我有一個名爲tblAccInfo的表,下面是表數據。 我需要像下面的輸出。自加入問題

Input 
PolicyNumber BankAc StorageDate VerNum 
400  123  2012-01-01  1 
400  164  2012-01-03  2 
400  860  2012-01-05  3 
6004317654  301  2012-02-05  1 
6004317654  615  2012-03-01  2 
6004317654  253  2012-03-12  3 
6004317654  887  2012-04-03  4 

輸出

PolicyNumber IntialBankAc IntialSDate VerNum LatestBankAc LatestSDate VerNum 
400 123   2012-01-01 1  860   2012-01-05 3 
6004317654 301   2012-02-05 1  887   2012-04-03 4 

我試圖與以下自連接,但沒有成功。請幫我解決這個問題。

Select DISTINCT 
    P.PolicyNumber, 
    P.BankAc [IntialBankAc], 
    P.StorageDate IntialSDate], 
    P.VerNum, 
    P1.BankAc [LatestBankAc], 
    P1.StorageDate [LatestSDate], 
    P1.VerNum 
FROM tblAccInfo P 
INNER JOIN tblAccInfo P1 
ON P1.PolicyNumber=P.PolicyNumber 
AND (P.BankAc<>P1.BankAc AND P.StorageDate<>P1.StorageDate AND P.VerNum<>P1.VerNum) 
+0

@Mark Byers謝謝你的格式化。 – Simhadri

+0

不客氣。儘管如此,請儘量自己做。 –

+0

我嘗試了幾次,請建議特別格式表的最佳方式。 – Simhadri

回答

4

試試這個:

SELECT 
    T1.PolicyNumber, 
    T2.BankAc AS IntialBankAc, 
    T2.StorageDate AS IntialSDate, 
    T2.VerNum AS InitalVerNum, 
    T3.BankAc AS LatestBankAc, 
    T3.StorageDate AS LatestSDate, 
    T3.Vernum AS LatestVerNum 
FROM 
(
    SELECT 
     PolicyNumber, 
     MIN(VerNum) AS MinVerNum, 
     MAX(VerNum) AS MaxVerNum 
    FROM tblAccInfo 
    GROUP BY PolicyNumber 
) AS T1 
JOIN tblAccInfo AS T2 
ON T1.PolicyNumber = T2.PolicyNumber 
AND T1.MinVerNum = T2.VerNum 
JOIN tblAccInfo AS T3 
ON T1.PolicyNumber = T3.PolicyNumber 
AND T1.MaxVerNum = T3.VerNum 

看到它聯機工作:sqlfiddle

+0

是的,我打算髮布這個,但你打敗了我。以前從未見過sqlfiddle。很酷的功能。 – saccharine

+0

馬克Byers我厭倦了不同的測試用例與數據,它看起來不錯。謝謝你的答案。 – Simhadri

0

未測試 - 但應該給你的想法。 (有可能是這樣做的更有效的方式 - 它只是跳出我的辦法)

select initial.policynumber 
,initial.initialbankaccoutn 
,initial.initialstoragedate 
,initial.intialvernum 
,final.latestbankaccount 
,final.lateststoragedate 
,final.latestvernum 
from 
(select a.policynumber, a.bankaccount as initialbankaccount, a.storagedate as initialstoragedate, a.vernum as initialvernum 
from tblAccInfo a1 
    inner join (select min(storagedate) as storagedate, policynumber 
       from tblAccInfo 
       group by policynumber) a 
       on a.policynumber = a1.policynumber 
       and a.storagedate = a1.storagedate) initial 
inner join 
(select b.policynumber, b.bankaccount as latestbankaccount, b.storagedate as lateststoragedate, b.vernum as latestvernum 
from tblAccInfo b1 
    inner join (select min(storagedate) as storagedate, policynumber 
       from tblAccInfo 
       group by policynumber) b 
       on a.policynumber = b1.policynumber 
       and a.storagedate = b1.storagedate) final 
on final.policynumber = initial.policynumber 
1
DECLARE @x TABLE 
(
    PolicyNumber VARCHAR(32), 
    BankAc  INT, 
    StorageDate DATE, 
    VerNum  INT 
); 

INSERT @x VALUES 
('400','123','2012-01-01',1), 
('400','164','2012-01-03',2), 
('400','860','2012-01-05',3), 
('6004317654','301','2012-02-05',1), 
('6004317654','615','2012-03-01',2), 
('6004317654','253','2012-03-12',3), 
('6004317654','887','2012-04-03',4); 

WITH x AS 
(
    SELECT PolicyNumber, BankAc, StorageDate, VerNum, 
     f = ROW_NUMBER() OVER (PARTITION BY PolicyNumber ORDER BY VerNum), 
     l = ROW_NUMBER() OVER (PARTITION BY PolicyNumber ORDER BY VerNum DESC) 
    FROM @x 
) 
SELECT 
    x.PolicyNumber, 
    InitialBankAc = x.BankAc, 
    InitialSDate = x.StorageDate, 
    InitialVerNum = x.VerNum, 
    LatestBankAc = x2.BankAc, 
    LatestSDate  = x2.StorageDate, 
    LatestVerNum = x2.VerNum 
FROM x INNER JOIN x AS x2 
ON x.PolicyNumber = x2.PolicyNumber 
WHERE x.f = 1 AND x2.l = 1 
ORDER BY x.PolicyNumber;