具有MAX功能的INNER JOIN

时间:2016-10-08 06:52:14

标签: sql sql-server sql-server-2012

帮助我是编程新手我需要获取每个EXE名称的最高版本数据。我发现了相同的文章,但我们有不同的表格结构,所以他们并没有真正帮助我解决我的问题 我有这张桌子:

  

表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
  

我想返回具有最高版本的不同数据。   像这样:

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

我可以使用我的代码来完成,但问题是,我无法添加Description和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


我应该在哪里添加Description和ReleaseDate列? 感谢

2 个答案:

答案 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

答案 1 :(得分: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