Oracle clears duplicate data for a field (selects the maximum of another field period in duplicate data)

Source: Internet
Author: User

Requirements: Asset repair table in the same asset may be repaired to continue to apply for maintenance, this time the maintenance status needs to determine the maintenance status according to the most recent repair times, so duplicate data under the same Asset ID (Maintenance approval, maintenance approval failed), or may not appear (not apply for repair), Therefore, you need to query the asset repair table for the data that is not duplicated in the data and duplicate data in the request for repair date, the method is as follows:

The asset table is as follows:

1, all the data in the maintenance of assets
SELECT * FROM Asset_maintain t

The results are as follows:

2. Data that may be the same in asset maintenance
Select A.* from Asset_maintain a inner joins Asset_maintain B on a.asset_id=b.asset_id and A.rowid!=b.rowid

The results are as follows:

3. One of the largest storage dates in a query for equal data
SELECT * from Asset_maintain where maintain_start_date in (select Max (n.maintain_start_date) from Asset_maintain N Group B Y n.asset_id having count (n.asset_id) > 1)

The results are as follows:

4. Querying all data in the maintenance of assets (excluding the same data)
SELECT * from Asset_maintain t where t.asset_id not in (select a.asset_id from Asset_maintain a inner join Asset_maintain B on a.asset_id=b.asset_id and A.rowid!=b.rowid)

Finally, two subqueries of the same field, different data are associated:

(SELECT * from Asset_maintain t where t.asset_id not in (select a.asset_id from Asset_maintain a inner join Asset_maintain B on a.asset_id=b.asset_id and A.ROWID!=B.ROWID), UNION ALL
(SELECT * from Asset_maintain where maintain_start_date in (select Max (n.maintain_start_date) from Asset_maintain N Group by N.ASSET_ID have count (n.asset_id) > 1))

The results are as follows:

Oracle clears duplicate data for a field (selects the maximum of another field period in duplicate data)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.