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 9139945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:27:08+00:00 2026-06-17T09:27:08+00:00

Given a table (Table) as follows (sorry about the CSV style since I don’t

  • 0

Given a table (“Table”) as follows (sorry about the CSV style since I don’t know how to make it look like a table with the Stack Overflow editor):

id,member,data,start,end
1,001,abc,12/1/2012,12/31/2999
2,001,def,1/1/2009,11/30/2012
3,002,ghi,1/1/2009,12/31/2999
4,003,jkl,1/1/2012,10/31/2012
5,003,mno,8/1/2011,12/31/2011

If using Ruby Sequel, how should I write my query so I will get the following dataset in return.

id,member,data,start,end
1,001,abc,12/1/2012,12/31/2999
3,002,ghi,1/1/2009,12/31/2999
4,003,jkl,1/1/2012,10/31/2012

I get the most current (largest end date value) record for EACH (distinct) member from the original table.

I can get the answer if I convert the table to an Array, but I am looking for a solution in SQL or Ruby Sequel query, if possible. Thank you.

Extra credit: The title of this post is lame…but I can’t come up with a good one. Please offer a better title if you have one. Thank you.

  • 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-06-17T09:27:08+00:00Added an answer on June 17, 2026 at 9:27 am

    The Sequel version of this is a bit scary. The best I can figure out is to use a subselect and, because you need to join the table and the subselect on two columns, a “join block” as described in Querying in Sequel. Here’s a modified version of Knut’s program above:

    require 'csv'
    require 'sequel'
    
    # Create Test data     
    DB = Sequel.sqlite()
    DB.create_table(:mytable){
      field :id
      String :member
      String :data
      String :start # Treat as string to keep it simple
      String :end   # Ditto
    }
    CSV.parse(<<xx
      1,"001","abc","2012-12-01","2999-12-31"
      2,"001","def","2009-01-01","2012-11-30"
      3,"002","ghi","2009-01-01","2999-12-31"
      4,"003","jkl","2012-01-01","2012-10-31"
      5,"003","mno","2011-08-01","2011-12-31"
    xx
    ).each{|x|
      DB[:mytable].insert(*x)
    }
    
    # That was all setup, here's the query
    ds = DB[:mytable]
    result = ds.join(ds.select_group(:member).select_append{max(:end).as(:end)}, :member=>:member) do |j, lj, js|
      Sequel.expr(Sequel.qualify(j, :end) => Sequel.qualify(lj, :end))
    end
    puts result.all
    

    This gives you:

    {:id=>1, :member=>"001", :data=>"abc", :start=>"2012-12-01", :end=>"2999-12-31"}
    {:id=>3, :member=>"002", :data=>"ghi", :start=>"2009-01-01", :end=>"2999-12-31"}
    {:id=>4, :member=>"003", :data=>"jkl", :start=>"2012-01-01", :end=>"2012-10-31"}
    

    In this case it’s probably easier to replace the last four lines with straight SQL. Something like:

    puts DB[
      "SELECT a.* from mytable as a 
      join (SELECT member, max(end) AS end FROM mytable GROUP BY member) as b 
      on a.member = b.member and a.end=b.end"].all
    

    Which gives you the same result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id)); I'd like to
Given a table like: CREATE TABLE MyTable ( MyColumn NUMBER NOT NULL ); I
Given a table (id, col1, col2), does it make sense to create the following
Given a parent + reference tables where Reference table is as follows Ref_ID PARENT_ID
I want to retrieve rows from a dependent table that follows a given criteria
Sorry I know similar question have been asked many times before but like most
I have a table like follows: mysql> select * from tries; +----+--------+-----------+ | id
I have table as follows emp_id, emp_name, emp_address, emp_salary, emp_rating I would like to
Given a table like this, where x and y have a unique constraint. id,x,y
Given table: ID ONE TWO X1 15 15 X2 10 - X3 - 20

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.