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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:22:14+00:00 2026-05-14T02:22:14+00:00

I kind of stuck on how to implement this query – this is pretty

  • 0

I kind of stuck on how to implement this query – this is pretty similar to the query I posted earlier but I’m not able to crack it.

I have a shopping table where everytime a user buys anything, a record is inserted.

Some of the fields are

* shopping_id (primary key)
* store_id
* user_id

Now what I need is to pull only the list of those stores where he’s among the top 5 visitors:

When I break it down – this is what I want to accomplish:

* Find all stores where this UserA has visited
* For each of these stores - see who the top 5 visitors are.
* Select the store only if UserA is among the top 5 visitors.

The corresponding queries would be:

select store_id from shopping where user_id = xxx
select user_id,count(*) as 'visits' from shopping 
where store_id in (select store_id from shopping where user_id = xxx)
group by user_id
order by visits desc
limit 5

Now I need to check in this resultset if UserA is present and select that store only if he’s present.
For example if he has visited a store 5 times – but if there are 5 or more people who have visited that store more than 5 times – then that store should not be selected.

So I’m kind of lost here.

Thanks for your help

  • 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-14T02:22:15+00:00Added an answer on May 14, 2026 at 2:22 am

    This should do it. It uses an intermediate VIEW to figure out how many times each user has shopped at each store. Also, it assumes you have a stores table somewhere with each store_id listed once. If that’s not true, you can change SELECT store_id FROM stores to SELECT DISTINCT store_id FROM shopping for the same effect but slower results.

     CREATE VIEW shop_results (store_id, user_id, purchase_count) AS
         SELECT store_id, user_id, COUNT(*)
         FROM shopping GROUP BY store_id, user_id
    
     SELECT store_id FROM stores 
        WHERE 'UserA' IN 
          (SELECT user_id FROM shop_results 
           WHERE shop_results.store_id = stores.store_id 
           ORDER BY purchase_count DESC LIMIT 5)
    

    You can combine these into a single query by placing the SELECT from the VIEW inside the sub-query, but I think it’s easier to read this way and it may well be true that you want that aggregated information elsewhere in the system — more consistent to define it once in a view than repeat it in multiple queries.

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

Sidebar

Related Questions

I'm kind of like stuck trying to implement YUI autocomplete textbox. here's the code:
Kind of a weird question, but. I need to have a list of strings
this kind of follows on from another question of mine. Basically, once I have
Kind of stuck with this thing, that's why need help please. I am using
Kind of a basic question but I'm having troubles thinking of a solution so
What kind of programming problems are state machines most suited for? I have read
As kind of a follow up to this question about prefixes , I agree
The Getting Started Rails Guide kind of glosses over this part since it doesn't
I'm kind of stuck on a problem of how to stop my menu from
I'm kind of stuck working out where I'm going wrong with the below entity

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.