我有
Id Number
----------- -----------
950 20213666062
951 20213666062
每个“数字”我只想要一次,即ID最高的一次:
Id Number
----------- -----------
951 20213666062
SELECT
rarN.intIdRARNomina AS Id,
rarN.chrCUIL AS Number
FROM dbo.PVN_RAR p
INNER JOIN dbo.PVN_RARNomina rarN ON p.intIdRAR = rarN.intIdRAR
INNER JOIN PISCYS.dbo.SYA_UltimosContratoCliente uc ON p.intNroContrato = uc.intNroContrato
WHERE
p.intIdRAR = 4639
答案 0 :(得分:1)
尝试使用简单的GROUP BY
查询:
SELECT MAX(Id) AS Id, Number
FROM yourTable
GROUP BY Number;
在更新后的问题/查询中:
SELECT
MAX(rarN.intIdRARNomina) AS Id,
rarN.chrCUIL AS Number
FROM dbo.PVN_RAR p
INNER JOIN dbo.PVN_RARNomina rarN
ON p.intIdRAR = rarN.intIdRAR
INNER JOIN PISCYS.dbo.SYA_UltimosContratoCliente uc
ON p.intNroContrato = uc.intNroContrato
WHERE
p.intIdRAR = 4639
GROUP BY
Number;
答案 1 :(得分:0)
如果您的列多于此,则需要 correlation 子查询,否则只有group by
和max()
才能达到期望的结果:
select t.*
from table t
where t.id = (select max(t1.id) from table t1 where t1.Number = t.Number);
但是,最新版本还提供了一个另外的选项,可以使用row_number()
来实现。
答案 2 :(得分:0)
另一种替代方法是使用自我联接:
select t.*
from table t
inner join (select max(id) as DID, number from table group by number) t2
on t.ID = t2.DID