PostgreSQL Specify the same field to find the last set of records

Source: Internet
Author: User
Tags postgresql

The title is quite tangled, or an example.

Is that I have a bunch of records,

created_date| Key | Content |
1 | Test| Xx?xxxxx|
2 | Test| Xxxxx?xx|
3 | Test|? xxxxxxx|
2 | Hah | xxxxxxx?|
3 | Hah | Xxxx?xxx|

I want to remove the key, which is test and haha, respectively.

created_date| Key | Content |
3 | Test|? xxxxxxx|
3 | Hah | Xxxx?xxx|

I can't use group because I want content.

Finally the problem is described clearly, SQL is tired

Can be solved this way (I used to brush under the oralce exist, now quickly forgotten):

SELECT *
From comment C
WHERE not EXISTS (
SELECT *
From Comment C2
WHERE C.key = C2.key
and C2.created_date > C.created_date
);

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.