Jump to content

Need help with Query


ScubaAddict

Recommended Posts

I have a membership database.  Some people have signed up twice (by accident).  They get billed every 30 days.  If they signed up twice, they are charged twice.  If they signed up twice, they will have done it in the same 24 hour period.

 

The SQL I have figured out so far is this (payments.total is not important):

 

SELECT payments.date, payments.total, users.username from payments INNER JOIN users ON payments.user_id = users.id WHERE (payments.membership_id = 7 or payments.membership_id = 6) ORDER BY payments.user_id, payments.date

 

Gives me a table like below:

 

2016-12-20 05:29:13   4.99   user1

2017-01-19 13:27:12   4.99   user1

2016-12-20 14:34:49   4.99   user2

2017-01-19 13:28:01   4.99   user2

2017-01-19 13:59:40   4.99   user2

2016-12-21 16:28:24   4.99   user3

2017-01-20 13:43:15   4.99   user3

2017-02-19 15:25:46   4.99   user3

2016-12-21 16:38:12   4.99   user4

2017-01-20 13:49:59   4.99   user4

2017-02-20 15:38:52   4.99   user4

2017-02-20 13:49:59   4.99   user4      

 

Notice most dates for each user are 30 days(ish) apart, except for user2 on 1-19 and user2 on 2-20 - they have both been charged twice on the same day.  I need to alter the SQL to only select records where a single user has 2 dates (payments.date) within 24 hours of each other.  

 

So I am looking for it to show:

 

2017-01-19 13:28:01   4.99   user2

2017-01-19 13:59:40   4.99   user2

2017-02-20 15:38:52   4.99   user4

2017-02-20 13:49:59   4.99   user4

 

I am killing myself here - can someone help me?

 

THANK YOU!!

Edited by ScubaAddict
Link to comment
Share on other sites

MY FIRST POST may not have been clear, but I can't edit it.  I am looking to identify users already in my database that have more then one payment record within a 24 hour time period.

 

 

Hi Barand - thank you for the reply!  I am a newbie - but I think I am not understanding your reply.  I have altered my SQL (incorporating what I think you were implying) - as your exact SQL produced no results: 

 

SELECT DATE(payments.date) as date, users.username, COUNT(*) as tot from payments INNER JOIN users ON payments.user_id = users.id WHERE (payments.membership_id = 7 or payments.membership_id = 6) GROUP BY payments.user_id, date HAVING tot > 1

 

Produced no results.  If I remove the HAVING clause, I get exactly what I had with a tot column that has '1' in every cell.

 

I didn't see anything in your SQL that deciphered records within 24 hours from those outside 24 hours apart for the same user.  I am a newbie, so I assume I am missing something you were trying to get me to do.

 

Benanamen - I am looking for a way to identify those who have already signed up, not preventing future ones.  But thank you for the reply.

Edited by ScubaAddict
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.