我有

    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

最佳答案

尝试使用简单的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;

关于mysql - SELECT DISTINCT在一个列上考虑另一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53322683/

10-09 22:24