Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1063497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:47:30+00:00 2026-05-16T18:47:30+00:00

I have views which look something like this: mysql> select * from p2; +—+———+———+

  • 0

I have views which look something like this:

mysql> select * from p2;
+---+---------+---------+
| k | measure | time_id |
+---+---------+---------+
| D |     200 |       2 |
| E |     201 |       2 |
| F |     203 |       2 |
| A |      20 |       1 |
| B |      22 |       1 |
| C |      23 |       1 |
| D |     100 |       1 |
| E |     101 |       1 |
| F |     103 |       1 |
| G |       4 |       1 |
| H |       7 |       1 |
| I |      10 |       1 |
+---+---------+---------+

(k, time_id) is a unique key, and the above is greatly simplified (there will be many more values of time_id and k). The sort order is time_id DESC (followed by k ASC, but that’s not so important).

I want to find a SELECT statement that will filter it to this:

+---+---------+---------+
| k | measure | time_id |
+---+---------+---------+
| D |     200 |       2 |
| E |     201 |       2 |
| F |     203 |       2 |
| A |      20 |       1 |
| B |      22 |       1 |
| C |      23 |       1 |
| G |       4 |       1 |
| H |       7 |       1 |
| I |      10 |       1 |
+---+---------+---------+

I want to make sure that values for column k are unique, by filtering out rows where the k value has already been used before.

In this example, in the original view rows 0, 1, 2 contained k values D, E and F, but so did rows 6, 7, 8, so rows 6-8 are removed to make the second view.

Is there a SELECT statement that can do this? It feels like it should be straightforward, but I can’t figure out how to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:47:31+00:00Added an answer on May 16, 2026 at 6:47 pm

    You may want to use a derived table:

    SELECT p2.* 
    FROM   p2
    JOIN   (
               SELECT    MAX(time_id) max_time, k 
               FROM      p2 
               GROUP BY  k
           ) d_p2 ON (d_p2.k = p2.k AND d_p2.max_time = p2.time_id);
    

    Or you could also use the “null-self-join” method:

    SELECT    p2.*
    FROM      p2
    LEFT JOIN p2 AS d_p2 ON d_p2.k = p2.k AND d_p2.time_id > p2.time_id
    WHERE     d_p2.k IS NULL;
    

    These should work fine as long as you are sure that time_id is unique for each k. Otherwise you could still get duplicate rows.

    Test case:

    CREATE TABLE p2 (k char(1), measure int, time_id int);
    
    INSERT INTO p2 VALUES ('D', 200, 2);
    INSERT INTO p2 VALUES ('E', 201, 2);
    INSERT INTO p2 VALUES ('F', 203, 2);
    INSERT INTO p2 VALUES ('A',  20, 1);
    INSERT INTO p2 VALUES ('B',  22, 1);
    INSERT INTO p2 VALUES ('C',  23, 1);
    INSERT INTO p2 VALUES ('D', 100, 1);
    INSERT INTO p2 VALUES ('E', 101, 1);
    INSERT INTO p2 VALUES ('F', 103, 1);
    INSERT INTO p2 VALUES ('G',   4, 1);
    INSERT INTO p2 VALUES ('H',   7, 1);
    INSERT INTO p2 VALUES ('I',  10, 1);
    

    Result:

    +------+---------+---------+
    | k    | measure | time_id |
    +------+---------+---------+
    | D    |     200 |       2 |
    | E    |     201 |       2 |
    | F    |     203 |       2 |
    | A    |      20 |       1 |
    | B    |      22 |       1 |
    | C    |      23 |       1 |
    | G    |       4 |       1 |
    | H    |       7 |       1 |
    | I    |      10 |       1 |
    +------+---------+---------+
    9 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ListView I have an ItemTemplate and EditItemTemplate which look something like this,
I have an ASP MVC Sitemap which look something like this <mvcSiteMapNode title=Home imageUrl=home.png
I have a couple of views which present data from a CoreData entity in
In the asp.net mvc 3 application I have two views which have the same
I have these Views on which I add UILabels as text, The Views can
I currently have a tableview, with cell views which have NSTextFields inside of them.
I have two views each of which contain two subviews. Hit detection is working
I have a storyboard which contains views which simply refuse to change. If I
I need to know which views does a user have which privileges on. I'm
I am trying to create a scrollview which will have many views (uiimageviews, uitextfield,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.