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关键字没有给我正确的结果。请建议
答案 0 :(得分:7)
DISTINCT正在为您提供正确的结果,记录与您指定的所有列截然不同。你想要什么呢?根据ID和描述只是唯一的?哪个AnotherID
想要从每个小组中获取?
如果您只是想要一个任意的话,可以MIN
使用MAX
或GROUP BY
:
SELECT ID, Description, MIN(AnotherID)
FROM EntryPointViews EPV
GROUP BY ID, Description
答案 1 :(得分:1)
我认为您正在寻找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;