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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:45:51+00:00 2026-05-16T20:45:51+00:00

I can certainly do this by iterating through results with PHP, but just wanted

  • 0

I can certainly do this by iterating through results with PHP, but just wanted to know if someone had a second to post a better solution.

The scenario is that I have a list of transactions. I select two dates and run a report to get the transactions between those two dates…easy. For one of the reporting sections though, I need to only return the transaction if it was their first transaction.

Here was where I got with the query:

 SELECT *, MIN(bb_transactions.trans_tran_date) AS temp_first_time 
 FROM 
   bb_business      
   RIGHT JOIN bb_transactions ON bb_transactions.trans_store_id = bb_business.store_id 
   LEFT JOIN bb_member ON bb_member.member_id = bb_transactions.trans_member_id 
 WHERE 
   bb_transactions.trans_tran_date BETWEEN '2010-08-01' AND '2010-09-13' 
   AND bb_business.id = '5651' 
 GROUP BY bb_member.member_id 
 ORDER BY bb_member.member_id DESC

This gives me the MIN of the transactions between the selected dates. What I really need is the overall MIN if it falls between the two dates. Does that make sense?

I basically need to know if a customers purchased for the first time in the reporting period.

Anyways, no huge rush as I can solve with PHP. Mostly for my own curiosity and learning.

Thanks for spreading the knowledge!

EDIT:
I had to edit the query because I had left one of my trial-errors in there. I had also tried to use the temporary column created from MIN as the selector between the two dates. That returned an error.

SOLUTION:
Here is the revised query after help from you guys:

SELECT * FROM (
  SELECT 
    bb_member.member_id, 
    MIN(bb_transactions.trans_tran_date) AS first_time
  FROM 
    bb_business 
    RIGHT JOIN bb_transactions ON bb_transactions.trans_store_id = bb_business.store_id 
    LEFT JOIN bb_member ON bb_member.member_id = bb_transactions.trans_member_id 
  WHERE bb_business.id = '5651' 
  GROUP BY bb_member.member_id
) AS T 
WHERE T.first_time BETWEEN '2010-08-01' AND '2010-09-13'
  • 1 1 Answer
  • 1 View
  • 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-16T20:45:52+00:00Added an answer on May 16, 2026 at 8:45 pm

    If we do a minimum of all transactions by customer, then check to see if that is in the correct period we get something along the lines of…

    This will simply give you a yes/no flag as to whether the customer’s first purchase was within the period…

    SELECT CASE COUNT(*) WHEN 0 THEN 'Yes' ELSE 'No' END As [WasFirstTransInThisPeriod?]
    FROM (  
            SELECT bb_member.member_id As [member_id], MIN(bb_transactions.trans_tran_date) AS temp_first_time 
            FROM bb_business      
            RIGHT JOIN bb_transactions ON bb_transactions.trans_store_id = bb_business.store_id 
            LEFT JOIN bb_member ON bb_member.member_id = bb_transactions.trans_member_id 
            WHERE bb_business.id = '5651' 
            GROUP BY bb_member.member_id
        ) T
    WHERE T.temp_first_time BETWEEN '2010-08-01' AND '2010-09-13'
    ORDER BY T.member_id DESC
    

    (this is in T-SQL, but should hopefully give an idea of how this can be achieved similarly in mySQL)

    Simon

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

Sidebar

Related Questions

This can certainly be done using a simple script and reading the database. I
My problem is certainly right on my face but I can't see it... I
This may be a case of me just misunderstanding what I've read, but all
I recently had my first encounter with PHP (5) through a Drupal application for
in websql we can request a certain row like this: tx.executeSql('SELECT * FROM tblSettings
How can I escape certain characters in a string with a C# Regex? This
There is something wrong in my jQuery plugin. I can't post the whole script
This is a pretty basic scenario but I'm not finding too many helpful resources.
I've been trying to work out this particular problem and though I can easily
I can't even begin to think about how this would be done. Basically, imagine

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.