2016-10-08 100 views
0

幫助我是編程新手,我需要獲取每個EXE名稱的最高版本數據。我發現了同樣的文章,但我們有不同的表格結構,所以他們沒有真正幫我解決我的問題。
我有這個表:INNER JOIN MAX函數

表mForm

FormID  FormName   EXEname  CurrentVersion 
-------------------------------------------------------- 
BTC-01  CASH REVIEW  BTC-01.EXE 1.0.2.5 
BTC-02  CASH REQUEST BTC-02.EXE 1.1.2.4 
BTC-03  PAYMENTS  BTC-03.EXE 1.0.0.3 

表mVersionHistory

EXEname   FormName  Version  ReleasedDate ReleaseDescription 
----------------------------------------------------------------------------- 
BTC-01.EXE  CASH REVIEW  1.0.2.5  08-08-16  IT REQ 10063 
BTC-01.EXE  CASH REVIEW  1.0.2.4  08-08-10  IT REQ 10051 
BTC-01.EXE  CASH REVIEW  1.0.2.3  08-08-09  IT REQ 10050 
BTC-02.EXE  CASH REQUEST 1.1.2.4  08-08-13  IT REQ 10003 
BTC-02.EXE  CASH REQUEST 1.1.2.0  08-08-10  IT REQ 10002 
BTC-03.EXE  PAYMENTS  1.0.0.2  08-08-07  IT REQ 10102 
BTC-03.EXE  PAYMENTS  1.0.0.1  08-08-06  IT REQ 10092 

我想回到與T不同的數據他是最高版本。 這樣的:

EXEname   FormName  CurrentVersion Version  ReleasedDate ReleaseDescription 
---------------------------------------------------------------------------------------------- 
BTC-01.EXE  CASH REVIEW  1.0.2.5   1.0.2.5  08-08-16  IT REQ 10063 
BTC-02.EXE  CASH REQUEST 1.1.2.4   1.1.2.4  08-08-13  IT REQ 10003 
BTC-03.EXE  PAYMENTS  1.0.0.3   1.0.0.2  08-08-07  IT REQ 10102 

我可以用我的代碼做到這一點,但問題是,我不能添加描述和RELEASEDATE列,每次我加入他們的時候,所有的數據將顯示出來,因爲他們有不同的數據在Description和ReleaseDate列中。

SELECT 
A.FormID [FORM ID] 
, A.FormName [FORM NAME] 
, A.ExeName [EXE NAME] 
, A.CurrentVersion [CURRENT VERSION] 
, B.RVersion AS [RELEASED VERSION] 
FROM 
mForm A 
INNER JOIN 
(SELECT Exename, MAX(Version) AS RVersion 
FROM mVersionHistory 
GROUP BY ExeName) B 
ON A.ExeName = B.ExeName 
GROUP BY A.FormID, B.FormName, A.CurrentVersion, B.RVersion, A.ExeName 


我應該在哪裏添加描述和RELEASEDATE列? 感謝

+0

表mForm已經有最新版本的每個應用程序的,對不對? – qxg

+0

@qxg是的,他們有最新版本 –

+0

然後,我沒有得到你爲什麼'MAX'版本了。 – qxg

回答

0

應用可以使用ROW_NUMBER窗函數

;WITH cte 
    AS (SELECT A.formid     [FORM ID], 
       A.formname     [FORM NAME], 
       A.exename     [EXE NAME], 
       A.currentversion   [CURRENT VERSION], 
       B.rversion     AS [RELEASED VERSION], 
       ---You can add whatever columns need from mversionhistory table 
       Row_number()OVER(partition BY A.exename ORDER BY b.version DESC) AS Rn 
     FROM mform A 
       INNER JOIN mversionhistory B 
         ON A.exename = B.exename) 
SELECT * 
FROM cte 
WHERE rn = 1 
+0

這實際上就是我剛纔所做的,但謝謝 –

0

可以跨在以後版本的SQL

SELECT 
A.FormID [FORM ID] 
, A.FormName [FORM NAME] 
, A.ExeName [EXE NAME] 
, A.CurrentVersion [CURRENT VERSION] 
, c.RVersion AS [RELEASED VERSION] 
FROM 
mForm A 
cross apply 
(SELECT top 1 Exename, Version AS RVersion 
FROM mVersionHistory B 
where A.ExeName = B.ExeName and a.FormName = b.FormName 
order by version desc) c 
+0

如果我有適當的索引,我會採用這種方法 –