sql 获取重复记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 获取重复记录相关的知识,希望对你有一定的参考价值。

## Obtaining Records with Duplicate Fields

id | product_id | name | upgrade_id 
---|------------|------|------------
11244 | 809 | Alarm | 10
11245 | 809 | DL1 | 17
11246 | 809 | DL1 | 17
11250 | 809 | LHD | 27
11251 | 809 | LHD | 27
11250 | 809 | PH - Proble Hole | 30

* In the table above, this product with `id = 809` has duplicate upgrades of `DH1` and `LHD`
* The SQL to return duplicate records from this table `optionizes` is as follows:

```sql
SELECT upgrade_id, COUNT(*)
FROM optionizes
GROUP BY upgrade_id
HAVING COUNT(*) > 1
```

* You are querying the `optionizes` table for all its records (selecting `upgrade_id` and `COUNT(*)` fields)
  * Group them by the column name `upgrade_id`, which will update the number of rows returned to `COUNT(*)`
  * Having a `COUNT(*)` > 1 will ensure only duplicate records will appear in this new table

* You can add more columns and join additional tables:

```sql
select o.upgrade_id, u.name, p.sku, count(*)
from optionizes o
inner join upgrades u on o.upgrade_id = u.id
inner join products p on o.product_id = p.id
group by p.sku, o.upgrade_id, u.name
having count(*) > 1;
```
select t.sku, t.product_id as prod_id, opts.id as optionize_id, t.name, opts.*
--select distinct(t.sku)
from (
	select o.upgrade_id, u.name, p.id as product_id, p.sku, u.web_name, count(*)
	from optionizes o
	inner join upgrades u on o.upgrade_id = u.id
	inner join products p on o.product_id = p.id
	group by p.id, o.upgrade_id, u.name, p.sku, u.web_name
	having count(*) > 1
) t
inner join optionizes opts on opts.upgrade_id = t.upgrade_id and opts.product_id = t.product_id
--inner join upgrade_prices up on opts.upgrade_id = up.upgrade_id and up.pricelevel_id = 4 and up.year = '2019'
--where t.product_id = 5147 and name = 'DL1'
--where 
order by sku, name;



-- NEED TO SELF JOIN A TABLE FORMED BY A SUBQUERY?
with subquery as (
	select t.sku, t.product_id as prod_id, opts.id as optionize_id, t.name, opts.*
	--select distinct(t.sku)
	from (
		select o.upgrade_id, u.name, p.id as product_id, p.sku, u.web_name, count(*)
		from fsi_optionizes o
		inner join upgrades u on o.upgrade_id = u.id
		inner join products p on o.product_id = p.id
		group by p.id, o.upgrade_id, u.name, p.sku, u.web_name
		having count(*) > 1
	) t
	inner join optionizes opts on opts.upgrade_id = t.upgrade_id and opts.product_id = t.product_id
	--inner join fsi_upgrade_prices up on opts.upgrade_id = up.upgrade_id and up.pricelevel_id = 4 and up.year = '2019'
	--where t.product_id = 5147 and name = 'DL1'
	--where 
	order by sku, name
)
select q1.sku, q1.optionize_id, q1.name
from subquery q1
join subquery q2 on q1.prod_id = q2.prod_id and q1.name = q2.name
where q1.optionize_id in (
	select min(q1.optionize_id) from subquery q1 where q1.prod_id = q2.prod_id and q1.name = q2.name
)
group by q1.sku, q1.optionize_id, q1.name
order by q1.sku, q1.name;

以上是关于sql 获取重复记录的主要内容,如果未能解决你的问题,请参考以下文章

SQL获取最后一个日期时间记录[重复]

SQL游标获取重复记录

无法获取记录 SQL [重复]

sql 获取重复记录

sql仅获取这些日期之间的记录[重复]

如果所需的列值重复 [重复],则 SQL 查询以获取顶部记录