Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding date where conditions within 30 days has elapsed

Tags:

date

sql

mysql

For my website, I have a loyalty program where a customer gets some goodies if they've spent $100 within the last 30 days. A query like below:

SELECT u.username, SUM(total-shipcost) as tot
    FROM orders o
        LEFT JOIN users u
        ON u.userident = o.user
    WHERE shipped = 1
    AND user = :user
    AND date >= DATE(NOW() - INTERVAL 30 DAY)

:user being their user ID. Column 2 of this result gives how much a customer has spent in the last 30 days, if it's over 100, then they get the bonus.

I want to display to the user which day they'll leave the loyalty program. Something like "x days until bonus expires", but how do I do this?

Take today's date, March 16th, and a user's order history:

id | tot  |    date
-----------------------
84    38     2016-03-05
76    21     2016-02-29
74    49     2016-02-20
61    42     2015-12-28

This user is part of the loyalty program now but leaves it on March 20th. What SQL could I do which returns how many days (4) a user has left on the loyalty program?

If the user then placed another order:

id | tot  |    date
-----------------------
87    12     2016-03-09

They're still in the loyalty program until the 20th, so the days remaining doesn't change in this instance, but if the total were 50 instead, then they instead leave the program on the 29th (so instead of 4 days it's 13 days remaining). For what it's worth, I care only about 30 days prior to the current date. No consideration for months with 28, 29, 31 days is needed.

Some create table code:

create table users (
    userident int,
    username varchar(100)
);

insert into users values 
    (1, 'Bob');

create table orders (
    id int,
    user int,
    shipped int,
    date date,
    total decimal(6,2),
    shipcost decimal(3,2)
);

insert into orders values
    (84, 1, 1, '2016-03-05', 40.50, 2.50),
    (76, 1, 1, '2016-02-29', 22.00, 1.00),
    (74, 1, 1, '2016-02-20', 56.31, 7.31),
    (61, 1, 1, '2015-12-28', 43.10, 1.10);

An example output of what I'm looking for is:

userident | username | days_left
--------------------------------
    1          Bob        4

This is using March 16th as today for use with DATE(NOW()) to remain consistent with the previous bits of the question.

like image 832
gator Avatar asked Mar 16 '16 04:03

gator


People also ask

WHERE date is greater than current date in SQL?

In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a 'YYYY-MM-DD hh:mm: ss. mmm' pattern.

How can I get date between two dates in MySQL?

Use the DATEDIFF() function to retrieve the number of days between two dates in a MySQL database. This function takes two arguments: The end date. (In our example, it's the expiration_date column.)

How do I get 30 days old data in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How can I calculate days between two dates in SQL?

The DATEDIFF() function returns the difference between two dates.

How to calculate elapsed time between two dates in Excel?

As we know, Excel stores date as a serial number that starts from 1 at the date 1/1/1900. So, this output number means there was a total of 2077.43 days elapsed in WW2. Seconds by multiplying the number of days ( output) with 24*60*60. Read More: Calculate Elapsed Time Between Two Dates in Excel (5 Methods) 2.

What does “within X number of days” mean?

Various U.S. government agencies use the phrase “within x number of days” specifically to mean after the date. These are instances when doing that thing before the date would be difficult or near-impossible.

How to format dates within 30 days in Excel?

In our very first method, I’ll show how to use the Between option of Conditional Formatting to format dates within 30 days. You can set here any date range. ➥ Click as follows: Home > Conditional Formatting > Highlight Cells Rules > Between A dialog box will open up. ➥ Now set a date range between 30 days. I have set 30-11-21 to 31-12-21.

When should the form be submitted?

However, a literal reading of the statement would suggest that the form must be submitted "sometime during the 30 days before or after [a certain date]". In context, is that a correct (or at least reasonable) interpretation of the statement?


1 Answers

The following is basically how to do what you want. Note that references to "30 days" are rough estimates and what you may be looking for is "29 days" or "31 days" as works to get the exact date that you want.

  1. Retrieve the list of dates and amounts that are still active, i.e., within the last 30 days (as you did in your example), as a table (I'll call it Active) like the one you showed.

  2. Join that new table (Active) with the original table where a row from Active is joined to all of the rows of the original table using the date fields. Compute a total of the amounts from the original table. The new table would have a Date field from Active and a Totol field that is the sum of all the amounts in the joined records from the original table.

  3. Select from the resulting table all records where the Amount is greater than 100.00 and create a new table with Date and the minimum Amount of those records.

  4. Compute 30 days ahead from those dates to find the ending date of their loyalty program.

like image 95
Zhora Avatar answered Oct 19 '22 23:10

Zhora