sql选择最后一个但每个组中的一个记录

时间:2018-04-10 09:55:00

标签: sql sql-server

请帮我构建以下查询: 我有一个来自选择查询的联接表:

select nevlista.nevID as ColA, nkhk.nevktghID as ColB, nkhk.ktghelyID as ColC 
from nevlista
inner join nkhk on nevlista.nevID = nkhk.nevID
where nevlista.nevID = nkhk.nevID

这给出了以下结果:

 ColA   ColB ColC
 90002  629 6
 90003  835 9
 90003  875 12
 90003  112 12
 90004  424 17
 90004  570 1
 90004  905 17
 90005  648 1
 90005  649 17
 90005  523 17
 and so on...

现在,我需要来自ColA中每个相同ID-s的组,来自ColB的最后一个最高值(以及来自ColC的值)。 (ColA中相同ID-s的数量不同,通常有2到5个具有相同ID的记录)。

3 个答案:

答案 0 :(得分:2)

最简单的方法是使用colB desc命令的colA划分Row_number的cte:

;With cte as
(
    select  nevlista.nevID as ColA, 
            nkhk.nevktghID as ColB, 
            nkhk.ktghelyID as ColC,
            ROW_NUMBER() OVER(PARTITION BY nevlista.nevID ORDER BY nkhk.nevktghID DESC) as rn
    from nevlista
    inner join nkhk on nevlista.nevID = nkhk.nevID
    where nevlista.nevID = nkhk.nevID
)

select ColA, ColB, ColC
FROM CTE
WHERE rn = 2 -- assiming every nevlista.nevID appears at least twice in the cte

答案 1 :(得分:2)

您可以将排名函数row_number() tie 方法

一起使用
select top(1) with ties nl.nevID as cola, n.nevktghID as colb, n.ktghelyID as colc
from nevlista nl 
inner join nkhk n on nl.nevID = n.nevID
order by row_number() over (partition by nl.nevID order by n.nevktghID  desc)

并且,我怀疑在where

之后不需要使用joins子句

答案 2 :(得分:0)

考虑将group bymax

一起使用
select nevlista.nevID as ColA, nkhk.nevktghID as ColB, nkhk.ktghelyID as ColC 
from nevlista
join nkhk on nevlista.nevID = nkhk.nevID
join
(
    select nevID, max(nevktghID) max_nevktghID
    from nkhk 
    group by nevID
) t1 on nkhk.nevID = t1.nevID and
        nkhk.nevktghID = t1.max_nevktghID

如果你有以下索引,它可以是considerably faster on SQL Server

CREATE NONCLUSTERED INDEX ix_nkhk _nevID_nevktghID
  ON nkhk (nevID,nevktghID) INCLUDE (ktghelyID)