SELECT
*
FROM (
SELECT
row_number() OVER (ORDER BY ID) AS [rownum],
UserID, wfoKey
FROM
tbl_user_statistics2 Where (wfoKey = '4b1fbf7aa7')
) T
WHERE
rownum BETWEEN (@opt_start) AND (@opt_end)
以上查询有效并将所有UserID从第1行(opt_start)提取到10,000(opt_end)。在该数据集中,那些UserID是重复的。我现在想要为该行范围(1到10,000)中的记录提取唯一的UserID。用户可以选择任何范围,以便他们可以选择10,0000到20,0000,因此查询应该获得该范围内的唯一UserID,依此类推。我不需要结果中的rownum。
示例数据:
1 dced6f0d-3a67-456d78cb7-fb73c8db0c13 4b1fbf7aa7
2 dced6f0d-3a67-456d78cb7-fb73c8db0c13 4b1fbf7aa7
3 6360f485-4f4d-4c9a98b0e-74fea312a25d 4b1fbf7aa7
4 6360f485-4f4d-4c9a98b0e-74fea312a25d 4b1fbf7aa7
结果将是:
dced6f0d-3a67-456d78cb7-fb73c8db0c13 4b1fbf7aa7
6360f485-4f4d-4c9a98b0e-74fea312a25d 4b1fbf7aa7
不确定如何继续或者可能有比row_number()更好的方法。任何建议都会非常感激。
答案 0 :(得分:1)
这可以通过获取现有查询并将其转换为内嵌视图(T2
)然后从中查询不同记录来解决:
SELECT DISTINCT UserID, wfoKey FROM
(SELECT
*
FROM (
SELECT
row_number() OVER (ORDER BY ID) AS [rownum],
UserID, wfoKey
FROM
tbl_user_statistics2 Where (wfoKey = '4b1fbf7aa7')
) T
WHERE
rownum BETWEEN (@opt_start) AND (@opt_end)) T2
答案 1 :(得分:0)
;With CTE1
AS
(
SELECT * , rn = ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY wfoKey)
FROM tbl_user_statistics2
Where wfoKey = '4b1fbf7aa7'
),
CTE2
AS
(
SELECT ID, UserID, wfoKey
, RowNum = ROW_NUMBER() OVER (ORDER BY wfoKey)
FROM CTE1
WHERE rn = 1
)
SELECT ID, UserID, wfoKey
FROM CTE2
WHERE RowNum BETWEEN (@opt_start) AND (@opt_end)
答案 2 :(得分:0)
您可以通过userID
添加一个名为分区的作品 ;with cte as(
SELECT
*
FROM (
SELECT
row_number() OVER (partition by USerID ORDER BY ID) AS [rownum],
UserID, wfoKey
FROM
tbl_user_statistics2 Where (wfoKey = '4b1fbf7aa7')
) T
WHERE
rownum BETWEEN (@opt_start) AND (@opt_end)
)
Select * from cte
where rownum =1