如何在sql server中获取不同的记录

How to get distinct records in sqlserver

FUNDFAMILY_ID    FUND_NAME                REPORTSECTION_ID           
--------------------------------------------------------------
172295295    VANGUARD GROWTH FUND (Retail)  126345
172295295    VANGUARD GROWTH FUND (Retail)  126354
--------------------------------------------------------------

但我需要得到如下结果:

FUNDFAMILY_ID     FUND_NAME                   REPORTSECTION_ID
--------------------------------------------------------------
172295295   VANGUARD GROWTH FUND (Retail)   126345

在存储过程中,我有一个如下所示的查询,它给出了重复的行

    INSERT INTO #TABLE_REPORTSECTIONDATA (FUND_NAME, FUNDFAMILY_ID, REPORTSECTION_ID)

                SELECT DISTINCT @FUNDNAME_TEMP , @FUNDID_TEMP, EntryPointViewsId
                FROM EntryPointViews EPV
                ORDER BY EntryPointViewsId

DISTINCT 关键字没有给我正确的 results.please 建议

DISTINCT 为您提供了正确的结果,记录与您指定的所有列不同。你想要什么?只是根据 ID 和描述是唯一的?每组 AnotherID 想选哪个?

如果你只是想要一个任意的,你可以使用 MINMAXGROUP BY:

SELECT ID, Description, MIN(AnotherID)
FROM EntryPointViews EPV
GROUP BY ID, Description

我认为您正在寻找 ROW_NUMBER() 分区,此查询应该适合您:

SELECT FUNDFAMILY_ID, FUND_NAME, REPORTSECTION_ID
FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY FUNDFAMILY_ID ORDER BY REPORTSECTION_ID) AS RN, FUNDFAMILY_ID, FUND_NAME, REPORTSECTION_ID, EntryPointViewsId
    FROM EntryPointViews EPV
    ) AS T
WHERE RN = 1
ORDER BY EntryPointViewsId;