删除 sql 查询中的重复行
Remove duplicated rows in sql query
我有一个 table 具有以下结构
[id] [int] IDENTITY(1,1) NOT NULL,
[account_number] [int] NOT NULL,
[account_name] [varchar(100)] NULL,
[account_chapter] [varchar(20)] NULL,
可以有很多行具有相同的 account_number,但 account_name 和 account_chapters 不同。
例如,我们可以有如下内容:
id account_number account_name account_chapter
12 1111 Name01 chapter01
13 1111 Name02 chapter02
14 2222 Name03 chapter07
15 2222 Name05 chapter11
16 7777 Name06 chapter44
我想要的是针对每个 account_number 的查询,仅过滤 table 中的第一次出现。例如,上面的查询必须转换为以下内容:
id account_number account_name account_chapter
12 1111 Name01 chapter01
14 2222 Name03 chapter07
16 7777 Name06 chapter44
这是我写的查询:
with req01 as (select distinct account_number from accounts)
select * from req01 full join (select * from accounts) as p on p.account_number = req01.account_number
它没有产生预期的结果。
有什么帮助吗?
谢谢。
使用ROW_NUMBER
:
SELECT TOP 1 WITH TIES *
FROM accounts
ORDER BY ROW_NUMBER() OVER (PARTITION BY account_number ORDER BY account_chapter);
或者,以更典型的方式使用 ROW_NUMBER
:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY account_number
ORDER BY account_chapter) rn
FROM accounts
)
SELECT id, account_number, account_name, account_chapter
FROM cte
WHERE rn = 1;
请注意,这两个答案都假定 account_chapter
版本确定哪个“重复项”实际上是第一个。
通常,table 中的“第一次”出现将由 date/time 或标识列确定。你只有身份,所以你似乎想要:
select a.*
from accounts a
where a.id = (select min(a2.id)
from accounts a2
where a2.account_number = a.account_number
);
我有一个 table 具有以下结构
[id] [int] IDENTITY(1,1) NOT NULL,
[account_number] [int] NOT NULL,
[account_name] [varchar(100)] NULL,
[account_chapter] [varchar(20)] NULL,
可以有很多行具有相同的 account_number,但 account_name 和 account_chapters 不同。
例如,我们可以有如下内容:
id account_number account_name account_chapter
12 1111 Name01 chapter01
13 1111 Name02 chapter02
14 2222 Name03 chapter07
15 2222 Name05 chapter11
16 7777 Name06 chapter44
我想要的是针对每个 account_number 的查询,仅过滤 table 中的第一次出现。例如,上面的查询必须转换为以下内容:
id account_number account_name account_chapter
12 1111 Name01 chapter01
14 2222 Name03 chapter07
16 7777 Name06 chapter44
这是我写的查询:
with req01 as (select distinct account_number from accounts)
select * from req01 full join (select * from accounts) as p on p.account_number = req01.account_number
它没有产生预期的结果。
有什么帮助吗? 谢谢。
使用ROW_NUMBER
:
SELECT TOP 1 WITH TIES *
FROM accounts
ORDER BY ROW_NUMBER() OVER (PARTITION BY account_number ORDER BY account_chapter);
或者,以更典型的方式使用 ROW_NUMBER
:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY account_number
ORDER BY account_chapter) rn
FROM accounts
)
SELECT id, account_number, account_name, account_chapter
FROM cte
WHERE rn = 1;
请注意,这两个答案都假定 account_chapter
版本确定哪个“重复项”实际上是第一个。
通常,table 中的“第一次”出现将由 date/time 或标识列确定。你只有身份,所以你似乎想要:
select a.*
from accounts a
where a.id = (select min(a2.id)
from accounts a2
where a2.account_number = a.account_number
);