2012-09-04 72 views
1

我需要獲取特定軟件版本的開始日期和結束日期。該表具有以下數據:如何根據節點對版本進行分組?

Row ID Node_ID logtime       Version 
201191 121212 11-OCT-11 12.13.48.495000000 AM v2.0.26496.311  
470128 121212 08-DEC-11 10.45.04.696000000 AM v2.0.26496.312 
495049 121212 23-JAN-12 06.06.09.226000000 PM v2.0.26496.313 
630076 121212 30-APR-12 03.21.12.296000000 PM v2.1.2344.31 
643917 121212 31-MAY-12 10.59.32.655000000 AM v2.1.2514.31 

預期輸出:

Node_ID Version   logtime_start     logtime_end 
121212 v2.0.26496.311 11-OCT-11 12.13.48.495000000 AM 08-DEC-11 10.45.04.696000000 AM 
121212 v2.0.26496.312 08-DEC-11 10.45.04.696000000 AM 23-JAN-12 06.06.09.226000000 PM 
121212 v2.0.26496.313 23-JAN-12 06.06.09.226000000 PM 30-APR-12 03.21.12.296000000 PM 
121212 v2.1.2344.31 30-APR-12 03.21.12.296000000 PM 31-MAY-12 10.59.32.655000000 AM 
121212 v2.1.2514.31 31-MAY-12 10.59.32.655000000 AM today's date 

正如你可以看到v2.0.26496.311使用從第1行和行2 logTime Description該條給我的日期範圍這個特殊的版本被使用。

你有什麼想法或方向我應該使用?

回答

0

在SQL Server 2008+你可以做類似如下:

 -- Some test data 
     declare @V table(ID int identity, Node_ID int, logtime datetime, Version varchar(50)); 
     insert @V values(121212, '2011-OCT-11 12:13:48', 'v2.0.26496.311'); 
     insert @V values(121212, '08-DEC-11 10:45:04', 'v2.0.26496.312'); 
     insert @V values(121212, '23-JAN-12 18:06:09', 'v2.0.26496.313'); 
     insert @V values(121212, '30-APR-12 03:21:12', 'v2.1.2344.31'); 
     insert @V values(121212, '31-MAY-12 10:59:32', 'v2.1.2514.31'); 

     -- a temporary named result set, known as a common table expression 
     with CTE (ROWID, Node_ID, [Version], logtime) 
     as 
     (
      select ROW_NUMBER() OVER (Order by ID), Node_ID, Version, logtime 
      from @V 
     ) 

     select 
      a.Node_ID, 
      a.Version, 
      Start = min(a.logtime), 
      [End] = min(b.logtime) 
     from CTE a 
      left join CTE b on b.ROWID = a.ROWID + 1 
     group by a.Node_ID, a.Version 
     order by a.Node_ID, a.Version, min(a.logtime) desc 
+0

感謝方向,但我使用了LEAD功能,它解決了我的問題。 – vwbeetle

0

選擇NODE_ID,
版本,
logTime Description該條爲logtime_start,
NVL(LEAD(logTime Description該條,1)OVER(ORDER通過node_id,logtime),systimestamp)作爲來自audit_values的logtime_end ;

相關問題