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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:49:36+00:00 2026-06-14T07:49:36+00:00

I have a users table, which has a one-to-many relationship with a user_purchases table

  • 0

I have a users table, which has a one-to-many relationship with a user_purchases table via the foreign key user_id. That is, each user can make many purchases (or may have none, in which case he will have no entries in the user_purchases table).

user_purchases has only one other field that is of interest here, which is purchase_date.

I am trying to write a Sequel ORM statement that will return a dataset with the following columns:

  • user_id
  • date of the users SECOND purchase, if it exists

So users who have not made at least 2 purchases will not appear in this dataset. What is the best way to write this Sequel statement?

Please note I am looking for a dataset with ALL users returned who have >= 2 purchases

Thanks!

EDIT FOR CLARITY

Here is a similar statement I wrote to get users and their first purchase date (as opposed to 2nd purchase date, which I am asking for help with in the current post):

   DB[:users].join(:user_purchases, :user_id => :id)
              .select{[:user_id, min(:purchase_date)]}
              .group(:user_id)
  • 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-14T07:49:37+00:00Added an answer on June 14, 2026 at 7:49 am

    You don’t seem to be worried about the dates, just the counts so

    DB[:user_purchases].group_and_count(:user_id).having(:count > 1).all
    

    will return a list of user_ids and counts where the count (of purchases) is >= 2. Something like

    [{:count=>2, :user_id=>1}, {:count=>7, :user_id=>2}, {:count=>2, :user_id=>3}, ...]
    

    If you want to get the users with that, the easiest way with Sequel is probably to extract just the list of user_ids and feed that back into another query:

    DB[:users].where(:id => DB[:user_purchases].group_and_count(:user_id).
        having(:count > 1).all.map{|row| row[:user_id]}).all
    

    Edit:

    I felt like there should be a more succinct way and then I saw this answer (from Sequel author Jeremy Evans) to another question using select_group and select_more : https://stackoverflow.com/a/10886982/131226

    This should do it without the subselect:

    DB[:users].
      left_join(:user_purchases, :user_id=>:id).
      select_group(:id).
      select_more{count(:purchase_date).as(:purchase_count)}.
      having(:purchase_count > 1)
    

    It generates this SQL

    SELECT `id`, count(`purchase_date`) AS 'purchase_count' 
    FROM `users` LEFT JOIN `user_purchases` 
    ON (`user_purchases`.`user_id` = `users`.`id`) 
    GROUP BY `id` HAVING (`purchase_count` > 1)"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table users which has a primary key userid and a datetime
I have a User table which has a PrivilegeId foreign key points to a
I have an application which has a Projects table and a Users table. In
I have a table Product. Product is related to ProductDescription in a one-to-many relationship.
I have the following tables in my database that have a many-to-many relationship, which
I have a table User which has the fields (id, first_name, middle_name, last_name). I
I have a table in HTML which has three columns i.e Error - User
So I have a mySQL database with a 'users' table which includes a username,
I have a picture table which consists for users pictures Table Name:picture Columns: picture_id
I have a innoDB table which records online users. It gets updated on every

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.