Assuming I want to get 'weekly' results from a date range, and the date range contains four separate weeks, but my database only has a record for one week, how can I get filler values for the remaining three weeks?
For example, given this date range: 6/2018 - 7/2018
I run this query:
SELECT
DATE_FORMAT(period, '%m %d') || ' - ' || DATE_FORMAT(period, '%m %d') AS period,
SUM(clicks) AS clicks
FROM tablename
WHERE period >= ? AND period <= ?
GROUP BY YEAR(period), WEEK(period)
With these table rows in the database:
| period | clicks |
| 07/01/2018T:00:00:00Z | 1000 |
And I get these query results:
| period | clicks |
| Jul 1 - Jul 5 | 1000 |
But I want to get these query results to cover default values for the empty weeks so I can use them to populate a d3 chart:
| period | clicks |
| Jul 1 - Jul 5 | 1000 |
| Jun 25 - Jun 23 | 0 |
| Jun 18 - Jun 24 | 0 |
| Jun 12 - Jun 18 | 0 |
Any ideas? This sort of "filler" phenomena should be generic enough to work with other intervals, such as daily, monthly, yearly, hourly.
rewrite your query as
ifnull( SUM(clicks),0) AS clicks
You can use this query to generate the weeks and then left join with your current query to get what you want.
DECLARE #StartingFromDate DATETIME = '2018-12-01';
DECLARE #EndingAtDate DATETIME = '2018-12-31';
WITH CTE_DateRange (DateRange)
AS (SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, #EndingAtDate) - DATEDIFF(WEEK, #StartingFromDate, #EndingAtDate), 0)
UNION ALL
SELECT DATEADD(WEEK, 1, DateRange)
FROM CTE_DateRange
WHERE DATEADD(WEEK, 1, DateRange) < #EndingAtDate)
SELECT CTE_DateRange.DateRange
FROM CTE_DateRange
WHERE CTE_DateRange.DateRange BETWEEN #StartingFromDate AND #EndingAtDate
Working with the following sql tables:
table: fiscal
DateID | date | fiscal_year | fiscal_week
20170101 2017-01-01 00:00:00.0 2017 2017 WK 01
20170102 2017-01-02 00:00:00.0 2017 2017 WK 01
table: email_info
email_id | email_name | email_subjectline
123 New_Year_2017 Welcome the new year!
345 Reminder Don't forget
table: sent_info
email_id | sent_date
123 | 1/1/2017 8:58:39 PM
345 | 1/2/2017 6:33:39 AM
table: click_info
recipient | email_id | click_date
XYZ 123 1/7/2017 4:25:27 PM
ABC 123 1/5/2017 3:13:56 AM
CDF 345 1/6/2017 2:20:16 AM
ABC 345 1/14/2017 3:33:25 AM
Obviously there are many rows in each table.
The joining between the email tables is straightforward.
SELECT *
FROM email_info
JOIN sent_info
ON sent_info.email_id = email_info.email_id
JOIN click_info
ON click_info.email_id = email_info.email_id
I am struggling with the following:
how to get all dates into the same format? ( I don't need the times,
only the day)
how to join the fiscal table so I can filter by fiscal week for example
how to count all clicks for an email for 7 days after the sent date (this cannot be hard-coded by dates, but must be dynamic)
This is the output I am looking for (filtered by fiscal week = 2017 WK 01):
email_id | email_name | sent_date | fiscal_week | Clicks
123 New_year_2017 1/1/2017 2017 WK 01 2
345 Reminder 1/2/2017 2017 WK 01 1
*Please note that the last click in the click_info table example was not counted, because it was beyond the 7 days after sent date.
** DateID is an integer and sent_date and click_date are strings/varchar
assuming that dateId is varchar and the others are datetime should be
select a.email_id, a.email_name, date(b.sent_date), c.fiscal_week, count(d.click_date)
from email_info a
inner join fiscal c on str_to_date(c.dateID, '%Y%m%d') = date(b.sent_date)
inner join sent_info b on b.email_id = c.email_id
inner join click_info d on d.email_id = b.email_id
and date(d.click_date) between date(b.sent_date) and DATEADD(week,1,date(b.sent_date))
group by a.email_id, a.email_name, date(b.sent_date), c.fiscal_week
PS do the fact the query struct is defined if you have other format you can convert properly and change the single piece
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.
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.
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.
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.
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.
Compute 30 days ahead from those dates to find the ending date of their loyalty program.
You would need to take the following steps (per user):
join the orders table with itself to calculate sums for different (bonus) starting dates, for any of the starting dates that are in the last 30 days
select from those records only those starting dates which yield a sum of 100 or more
select from those records only the one with the most recent starting date: this is the start of the bonus period for the selected user.
Here is a query to do that:
SELECT u.userident,
u.username,
MAX(base.date) AS bonus_start,
DATE(MAX(base.date) + INTERVAL 30 DAY) AS bonus_expiry,
30-DATEDIFF(NOW(), MAX(base.date)) AS bonus_days_left
FROM users u
LEFT JOIN (
SELECT o.user,
first.date AS date,
SUM(o.total-o.shipcost) as tot
FROM orders first
INNER JOIN orders o
ON o.user = first.user
AND o.shipped = 1
AND o.date >= first.date
WHERE first.shipped = 1
AND first.date >= DATE(NOW() - INTERVAL 30 DAY)
GROUP BY o.user,
first.date
HAVING SUM(o.total-o.shipcost) >= 100
) AS base
ON base.user = u.userident
GROUP BY u.username,
u.userident
Here is a fiddle.
With this input as orders:
+----+------+---------+------------+-------+----------+
| id | user | shipped | date | total | shipcost |
+----+------+---------+------------+-------+----------+
| 61 | 1 | 1 | 2015-12-28 | 42 | 0 |
| 74 | 1 | 1 | 2016-02-20 | 49 | 0 |
| 76 | 1 | 1 | 2016-02-29 | 21 | 0 |
| 84 | 1 | 1 | 2016-03-05 | 38 | 0 |
| 87 | 1 | 1 | 2016-03-09 | 50 | 0 |
+----+------+---------+------------+-------+----------+
The above query will return this output (when executed on 2016-03-20):
+-----------+----------+-------------+--------------+-----------------+
| userident | username | bonus_start | bonus_expiry | bonus_days_left |
+-----------+----------+-------------+--------------+-----------------+
| 1 | John | 2016-02-29 | 2016-03-30 | 10 |
+-----------+----------+-------------+--------------+-----------------+
Simple solution
Seeing how you do your first query, I guessed that when you are at the point where you look for the "expiration date", you already know that the user meets the 100 points over last 30 days. Then you can do this :
SELECT DATE_ADD(MIN(date),INTERVAL 30 DAY)
FROM orders o
WHERE shipped = 1
AND user = :user
AND date >= (DATE(NOW() - INTERVAL 30 DAY))
It takes the minimum order date of a user over the last 30 days, and add 30 days to the result.
But that really is a poor design to achieve what you want.
You would better to think further and implement what's next.
Advanced solution
In order to reproduce all the following solution, I have used the Fiddle that Trincot kindly built, and expanded it to test on more data : 4 users having 4 orders.
SQL FIddle http://sqlfiddle.com/#!9/668939/1
Step 1 : Design
The following query will return all the users meeting the loyalty program criteria, along with their earlier order date within 30 days and the loyalty program expiration date calculated from the earlier date, and the number of days before it expires.
SELECT O.user, u.username, SUM(total-shipcost) as tot, MIN(date) AS mindate,
DATE_ADD(MIN(date),INTERVAL 30 DAY) AS expirationdate,
DATEDIFF(DATE_ADD(MIN(date),INTERVAL 30 DAY), DATE(NOW())) AS daysleft
FROM orders o
LEFT JOIN users u
ON u.userident = o.user
WHERE shipped = 1
AND date >= DATE(NOW() - INTERVAL 30 DAY)
GROUP BY user
HAVING tot >= 100;
Now, create a VIEW with the above query
CREATE VIEW loyalty_program AS
SELECT O.user, u.username, SUM(total-shipcost) as tot, MIN(date) AS mindate,
DATE_ADD(MIN(date),INTERVAL 30 DAY) AS expirationdate,
DATEDIFF(DATE_ADD(MIN(date),INTERVAL 30 DAY), DATE(NOW())) AS daysleft
FROM orders o
LEFT JOIN users u
ON u.userident = o.user
WHERE shipped = 1
AND date >= DATE(NOW() - INTERVAL 30 DAY)
GROUP BY user
HAVING tot >= 100;
It is important to understand that this is only a one-shot action on your database.
Step 2 : Use your new VIEW
Once you have the view, you can get easily, for all users, the "state" of the loyalty program:
SELECT * FROM loyalty_program
user username tot mindate expirationdate daysleft
1 John 153 February, 28 2016 March, 29 2016 9
2 Joe 112 February, 24 2016 March, 25 2016 5
3 Jack 474 February, 23 2016 March, 24 2016 4
4 Averel 115 February, 22 2016 March, 23 2016 3
For a specific user, you can get the date you are looking for like this:
SELECT expirationdate FROM loyalty_program WHERE username='Joe'
You can also request all the users for which the expiration date is today
SELECT user FROM loyalty_program WHERE expirationdate=DATE(NOW))
But there are other easy possibilities that you'll discover after having played with your VIEW.
Conclusion
Make your life easier: learn to use VIEWS !
I am assuming your table looks like this:
user | id | total | date
-------------------------------
12 84 38 2016-03-05
12 76 21 2016-02-29
23 74 49 2016-02-20
23 61 42 2015-12-28
then try this:
SELECT x.user, x.date, x.id, x.cum_sum, d,date, DATEDIFF(NOW(), x.date) from (SELECT a.user, a.id, a.date, a.total,
(SELECT SUM(b.total) FROM order_table b WHERE b.date <= a.date and a.user=b.user ORDER BY b.user, b.id DESC) AS cum_sum FROM order_table a where a.date>=DATE(NOW() - INTERVAL 30 DAY) ORDER BY a.user, a.id DESC) as x
left join
(SELECT c.user, c.date as start_date, c.id from (SELECT a.user, a.id, a.date, a.total,
(SELECT SUM(b.total) FROM order_table b WHERE b.date <= a.date and a.user=b.user ORDER BY b.user, b.id DESC) AS cum_sum FROM order_table a where a.date>=DATE(NOW() - INTERVAL 30 DAY) ORDER BY a.user, a.id DESC) as c WHERE FLOOR(c.cum_sum/100)=MIN(FLOOR(c.cum_sum/100)) and MOD(c.cum_sum,100)=MAX(MOD(c.cum_sum,100)) group by concat(c.user, "_", c.id)) as d on concat(x.user, "_", x.id)=concat(d.user, "_", d.id) where x.date=d.date;
You will get a table something like this:
user | Date | cum_sum | start_date | Time_left
----------------------------------------------------
12 2016-03-05 423 2016-03-05 24
13 2016-02-29 525 2016-02-29 12
23 2016-02-20 944 2016-02-20 3
29 2015-12-28 154 2015-12-28 4
i have not tested this. But what i am trying to do is to create a table in descending order of id and user, and get a cumulative total column along with it. I have created another table by using this table with cumulative total, with relevant date (i.e. date from which date difference is to be calculated) for each user. I have left joined these two tables, and put in the condition x.date=d.date. I have put start_date and date in the table to check if the query is working.
Also, this is not the most optimum way of writing this code, but i have tried to stay as safe as possible by using sub queries, since i did not have the data to test this. Let me know if you face any error.
I have a reoccurring payment day of 14th of each month and want to group a subset of data by month/year and sum the sent column. For example for the given data:-
Table `Counter`
Id Date Sent
1 10/04/2013 2
2 11/04/2013 4
3 15/04/2013 7
4 10/05/2013 3
5 14/05/2013 5
6 15/05/2013 3
7 16/05/2013 4
The output I want is something like:
From Count
14/03/2013 6
14/04/2013 10
14/05/2013 12
I am not worried how the from column is formatted or if its easier to split into month/year as I can recreated a date from multiple columns in the GUI. So the output could easily just be:
FromMth FromYr Count
03 2013 6
04 2013 10
05 2013 12
or even
toMth toYr Count
04 2013 6
05 2013 10
06 2013 12
If the payment date is for example the 31st then the date comparison would need to be the last date of each month. I am also not worried about missing months in the result-set.
I will also turn this into a Stored procedure so that I can push in the the payment date and other filtered criteria. It is also worth mentioning that we can go across years.
Try this query
select
if(day(STR_TO_DATE(date, "%Y-%d-%m")) >= 14,
concat('14/', month(STR_TO_DATE(date, "%Y-%d-%m")), '/', year(STR_TO_DATE(date, "%Y-%d-%m"))) ,
concat('14/', if ((month(STR_TO_DATE(date, "%Y-%d-%m")) - 1) = 0,
concat('12/', year(STR_TO_DATE(date, "%Y-%d-%m")) - 1),
concat(month(STR_TO_DATE(date, "%Y-%d-%m"))-1,'/',year(STR_TO_DATE(date, "%Y-%d-%m")))
)
)
) as fromDate,
sum(sent)
from tbl
group by fromDate
FIDDLE
| FROMDATE | SUM(SENT) |
--------------------------
| 14/10/2013 | 3 |
| 14/12/2012 | 1 |
| 14/3/2013 | 6 |
| 14/4/2013 | 10 |
| 14/5/2013 | 12 |
| 14/9/2013 | 1 |
Pay date could be grouped by months and year separatedly
select Sum(Sent) as "Count",
Extract(Month from Date - 13) as FromMth,
Extract(Year from Date - 13) as FromYr
from Counter
group by Extract(Year from Date - 13),
Extract(Month from Date - 13)
Be careful, since field's name "Date" coninsides with the keyword "date" in ANSISQL
I think the simplest way to do what you want is to just subtract 14 days rom the date and group by that month:
select date_format(date - 14, '%Y-%m'), sum(sent)
from counter
group by date_format(date - 14, '%Y-%m')
I'm having a table like this one:
-------------------------------
| NrOfVisitors | Year | Month |
-------------------------------
| 320 | 2009 | 1 |
-------------------------------
| 300 | 2009 | 2 |
-------------------------------
| 150 | 2010 | 1 |
-------------------------------
| 100 | 2010 | 2 |
-------------------------------
Now I want to count the visitors untill the 1st month of 2010.
When I say:
SELECT SUM(NrOfVisitors) As TotalVisitors FROM VisitorTable WHERE YEAR <= 2010 AND Month <= 1
Then I don't get the amount of visitors because then it doesn't count the visitors of 2009 month 2. So I'm missing 300 visitors with that query. So what I need is a query to count the NrOfVisitors untill 2010 month 1
Who can help me with this/
Thanks in advance!
Try this:
SELECT SUM(NrOfVisitors) As TotalVisitors
FROM VisitorTable
WHERE Year * 12 + Month <= 2010 * 12 + 1
In this case you can simply use OR
WHERE YEAR <= 2010 OR MONTH <= 1
To be more general something like
WHERE YEAR < 2010 OR (YEAR = 2010 AND MONTH <=1)
Just remove the:
AND Month <=1 ?
Edit: also make the:
WHERE YEAR <= 2010
to:
WhERE YEAR < 2010
That should work?
Shouldn't it be
SELECT SUM(NrOfVisitors) As TotalVisitors FROM VisitorTable WHERE YEAR <= 2010 AND Month >= 1
?
I just changed Month <= 1 to Month >= 1 because, well, 2 > 1.
That way, if you have more months, and if you don't want them counted, you can just change that constraint to reflect which months you want to count.
SELECT SUM(NrOfVisitors) As TotalVisitors
FROM VisitorTable
WHERE YEAR <= 2010
OR (YEAR = 2010 AND MONTH = 1)