以下查询在第一个分区集之后导致重复的row_numbers。例如,第一组fldPK = 2403和fldCIA = 1我收到以下内容......
2403 1 1
2403 1 2
2403 1 3
2403 1 4
在第二集中我收到......
2403 2 1
2403 2 1 <-- duplicate 1 within the same partition
2403 2 2
2403 2 3
SELECT *, 1 AS fldBF,
ROW_NUMBER() OVER (PARTITION BY fldPK, fldCIA ORDER BY fldPK) AS sCount
INTO #tmpTable
FROM V_qryCSPGA
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
UNION ALL
SELECT *, 1 AS fldBF,
ROW_NUMBER() OVER (PARTITION BY fldPK, fldCIA ORDER BY fldPK) AS sCount
FROM FN_qryCSGBA()
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
ORDER BY fldPK, fldCIA, fldNDat;
为什么会这样?对于fldPK和fldCIA的每一组,我需要一个重新开始行号的计数为1,更合适的数据将是......
2403 3 1
2403 3 2
2403 3 3
2403 3 4
2403 3 5
2403 3 6
2403 3 7
2403 3 8
2403 3 9
答案 0 :(得分:3)
这将停止重复的数字。新的编号是否与你真正想要的完全相同,是另一回事:
; WITH cte AS
( SELECT *, 1 AS fldBF
FROM V_qryCSPGA
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
UNION ALL
SELECT *, 1 AS fldBF
FROM FN_qryCSGBA()
WHERE fldPK IN(SELECT DISTINCT thePK FROM FN_qryAllDTPK())
)
SELECT *,
ROW_NUMBER() OVER (PARTITION BY fldPK, fldCIA ORDER BY fldPK) AS sCount
INTO #tmpTable
FROM cte
ORDER BY fldPK, fldCIA, fldNDat;