How do I remove duplicates in paging

2019-09-09 06:55发布

table1 & table2:

table1 & table2 http://aftabfarda.parsfile.com/1.png

SELECT     *
FROM         (SELECT DISTINCT dbo.tb1.ID, dbo.tb1.name, ROW_NUMBER() OVER (ORDER BY tb1.id DESC) AS row
FROM         dbo.tb1 INNER JOIN
                      dbo.tb2 ON dbo.tb1.ID = dbo.tb2.id_tb1) AS a
WHERE     row BETWEEN 1 AND 7
ORDER BY id DESC

Result:

Result... http://aftabfarda.parsfile.com/3.png

(id 11 Repeated 3 times)

How can I have this output:

ID  name    row
--  ------  ---
11  user11  1
10  user10  2
9   user9   3
8   user8   4
7   user7   5
6   user6   6
5   user5   7

2条回答
一夜七次
2楼-- · 2019-09-09 07:24

You could apply distinct before row_number using a subquery:

select  *
from    (
        select  row_number() over (order by tbl.id desc) as row
        ,       *
        from    (
                select  distinct t1.ID
                ,       tb1.name
                from    dbo.tb1 as t1
                join    dbo.tb2 as t2
                on      t1.ID = t2.id_tb1
                ) as sub_dist
        ) as sub_with_rn
where   row between 1 and 7
查看更多
在下西门庆
3楼-- · 2019-09-09 07:31

Alternatively to @Andomar's suggestion, you could use DENSE_RANK instead of ROW_NUMBER and rank the rows first (in the subquery), then apply DISTINCT (in the outer query):

SELECT DISTINCT
  ID,
  name,
  row
FROM (
  SELECT
    t1.ID,
    t1.name,
    DENSE_RANK() OVER (ORDER BY t1.ID DESC) AS row
  FROM dbo.tb1 t1
    INNER JOIN dbo.tb2 t2 ON t1.ID = t2.id_tb1
) AS a
WHERE row BETWEEN 1 AND 7
ORDER BY ID DESC

Similar, but not quite the same, although both might boil down to the same query plan, I'm just not sure. Worth testing, I think.

And, of course, you could also try a semi-join instead of a proper join, in the form of either IN or EXISTS, to prevent duplicates in the first place:

SELECT
  ID,
  name,
  row
FROM (
  SELECT
    ID,
    name,
    ROW_NUMBER() OVER (ORDER BY ID DESC) AS row
  FROM dbo.tb1
  WHERE ID IN (SELECT id_tb1 FROM dbo.tb2)
  /* Or:
  WHERE EXISTS (
    SELECT *
    FROM dbo.tb2
    WHERE id_tb1 = dbo.tb1.ID
  )
  */
) AS a
WHERE row BETWEEN 1 AND 7
ORDER BY ID DESC
查看更多
登录 后发表回答