2015-11-04 42 views
0

示例表:TSQL獲得最大和最小日期一個單獨的,但不是唯一的記錄

test_date | test_result | unique_ID 
12/25/15 | 100   | 50 
12/01/15 | 150   | 75 
10/01/15 | 135   | 75 
09/22/14 | 99   | 50 
04/10/13 | 125   | 50 

我需要找到第一和最後一個測試日期以及測試結果通過用戶匹配所述的日期。所以,我可以按ID分組,但不能測試結果。

SELECT MAX(test_date)[need matching test_result], 
     MIN(test_date) [need matching test_result], 
     unique_id 
from [table] 
group by unique_id 

謝謝!

+0

你應該包括你的願望輸出。 –

回答

0

我會推薦窗口函數。下面就返回每個ID 2行的信息:

select t.* 
from (select t.*, 
      row_number() over (partition by unique_id order by test_date) as seqnum_asc, 
      row_number() over (partition by unique_id order by test_date desc) as seqnum_desc 
     from table t 
    ) t; 

一行,使用條件聚合(或pivot如果你喜歡):

select unique_id, 
     min(test_date), max(case when seqnum_asc = 1 then test_result end), 
     max(test_date), max(case when seqnum_desc = 1 then test_result end) 
from (select t.*, 
      row_number() over (partition by unique_id order by test_date) as seqnum_asc, 
      row_number() over (partition by unique_id order by test_date desc) as seqnum_desc 
     from table t 
    ) t 
group by unique_id; 
+0

我想你需要'order by test_date desc'在你的一個'row_number()' –

0
Create TABLE #t 
    (
     test_date date , 
     Test_results int, 
     Unique_id int 
    ) 

INSERT INTO #t 
VALUES ('12/25/15',100,50), 
     ('12/01/15',150,75), 
     ('10/01/15',135,75), 
     ('09/22/14',99,50), 
     ('04/10/13',125,50) 

select 'MinTestDate' as Type, a.test_date, a.Test_results, a.Unique_id 
from #t a inner join ( 
select min(test_date) as test_datemin, max(test_date) as test_datemax, unique_id from #t 
group by unique_ID) b 
on a.test_date = b.test_datemin 

union all 

select 'MaxTestDate' as Type, a.test_date, a.Test_results, a.Unique_id from #t a 
inner join ( 
select min(test_date) as test_datemin, max(test_date) as test_datemax, unique_id from #t 
group by unique_ID) b 
on a.test_date = b.test_datemax 
+0

對不起,我是新來的,我的格式是廢話,我不知道如何鏈接SQL查詢,所以很好像你們有..但我希望你可以複製粘貼這個,它會看起來更好.. – Herman

+0

選擇文本和使用'ctrl-K'或在每行前添加4個空格 –

+0

非常棒,謝謝 – Herman

0

考慮使用自組合聯接和派生表:

SELECT t1.unique_id, minTable.MinOftest_date, t1.test_result As Mintestdate_result, 
     maxTable.MaxOftest_date, t2.test_result As Maxtestdate_result 

FROM TestTable AS t1 
INNER JOIN 
      (
      SELECT Min(TestTable.test_date) AS MinOftest_date, 
        TestTable.unique_ID 
      FROM TestTable 
      GROUP BY TestTable.unique_ID 
      ) As minTable 
    ON (t1.test_date = minTable.MinOftest_date 
    AND t1.unique_id = minTable.unique_id) 

INNER JOIN TestTable As t2  
INNER JOIN 
      (
      SELECT Max(TestTable.test_date) AS MaxOftest_date, 
        TestTable.unique_ID 
      FROM TestTable 
      GROUP BY TestTable.unique_ID 
      ) AS maxTable 
    ON t2.test_date = maxTable.MaxOftest_date 
    AND t2.unique_ID = maxTable.unique_ID 

ON minTable.unique_id = maxTable.unique_id; 

輸出

unique_id MinOftest_date Mintestdate_result MaxOftest_date Maxtestdate_result 
50    4/10/2013     125  12/25/2015     100 
75    10/1/2015     135  12/1/2015     150 
相關問題