MYSQL WPDataCharts Wordpress - mysql

I have very little knowledge of MYSQL so please forgive me. I have a wordpress form using Formidable Forms. I am querying the database to find forms that were logged from the beginning of this month to the current date which is....
SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < now() and date(created_at) > last_day(curdate() - interval 1 month) and field_id=30
field_id=30 is the date field. This procedure works.
I also have a similar procedure for last month which gives me the forms created from the beginning of last month to the same date of last month so if today is 14/08. The procedure would display a count of all forms from 01/07-14/07 which it does...
SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < NOW () - interval 1 month and date(created_at) > last_day(curdate() - interval 2 month) and field_id=30;
I need to try to join these so they output the data in the same query. Is this possible? I've tried union and join and everything tells me I have an error in my code. The error that comes up is cannot calculate position of the second select.
I would also like it to calculate the number of days left in the current month and I will be adding another procedure to give me the total forms created last month so I can run formulas on them.
So the table will look like...
Month Tickets
08 60
07 89
Total LM 194
Diff 134
Days Left 17
Target 7.8
Apologies for the long question and I appreciate any help.
Thanks
So I have the right data now but I need to change the order it appears. My query is...
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < now() and date(created_at) > last_day(curdate() - interval 1 month) and field_id=30)
UNION
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < NOW () - interval 1 month and date(created_at) > last_day(curdate() - interval 2 month) and field_id=30)
UNION
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) <= last_day(now() - interval 1 month) and date(created_at) > last_day(now() - interval 2 month) and field_id=30)
UNION
(SELECT DAYOFMONTH(now()) as D1, DAY(LAST_DAY(now())) as DaysInMonth);
This gives me the table...
Month.....Tickets
8.....62
7.....84
7.....194
14....31
The first row is the forms created this month. 84 is the forms created to the same date last month. 194 is the total for last month and the last row is the day of the month and the total days in the month.
I need to change the format slighly so I can do calculations on the results.
I need...
ThisMth.....LastMth.....LastMthTotal
62..........84..........194
14..........31
Thanks

UNION is the correct answer, if you want both as rows
But you need to enter around both SELECTS parenthesis
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < now() and date(created_at) > last_day(curdate() - interval 1 month) and field_id=30)
UNION
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < NOW () - interval 1 month and date(created_at) > last_day(curdate() - interval 2 month) and field_id=30);
for that you can use
(SELECT (SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < now() and date(created_at) > last_day(curdate() - interval 1 month) and field_id=30) ThisMth
,
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) < NOW () - interval 1 month and date(created_at) > last_day(curdate() - interval 2 month) and field_id=30) LastMth
,
(SELECT month(created_at) as Month, count(id) as Tickets
FROM wp_frm_item_metas
WHERE date(created_at) <= last_day(now() - interval 1 month) and date(created_at) > last_day(now() - interval 2 month) and field_id=30) LastMthTotal)
UNION
(SELECT DAYOFMONTH(now()) as D1, DAY(LAST_DAY(now())) as DaysInMonth,'');

Related

SQL comparing date

I want to compare the current date with a date from a database table.
This code:
DATE_FORMAT(dt,'%y-%m')
gives me a date like this 2020-10
and i want that this code:
DATE_ADD(CURRENT_DATE, INTERVAL - 1 month)
gets date like 2020-10 not 2020-10-12(some day)
Here is the whole sql query:
select count(*)
from app_tickets t
where t.status= 3
and DATE_FORMAT(dt,'%y-%m')=DATE_ADD(CURRENT_DATE, INTERVAL - 1 month)
Thanks for help:)
This can make use of indexes
select count(*)
from app_tickets t
where t.status = 3
and t.dt >= DATE_FORMAT(CURRENT_DATE - interval 1 month ,'%Y-%m-01')
and t.dt < DATE_FORMAT(CURRENT_DATE ,'%Y-%m-01')

How to get last 12 months data and insert 0 if no data found for the month

I'm trying to get last 12 months data in Mysql using below query but i'm getting all the data not last 12 months data i gone through few of the post but helpful
My CreatedDate format is like '2020-04-17 12:03:59'
SELECT date_format(createdDate,'%m') as Month,
count(*) as count,
date_format(createdDate,'%Y') as Year
FROM tablename
WHERE DATE_ADD(Now(),INTERVAL- 12 MONTH)
GROUP BY Year,
Month
ORDER by Year desc,
Month desc;
and also need help you Insert 0 if no data found in that month.
You may use a calendar table here:
SELECT
cal.dt,
COUNT(t.createdDate) AS count
FROM
(
SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS dt UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 2 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 4 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 5 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 6 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 7 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 8 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 9 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 10 MONTH), '%Y-%m') UNION ALL
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 11 MONTH), '%Y-%m')
) cal
LEFT JOIN tablename t
ON cal.dt = DATE_FORMAT(createdDate, '%Y-%m')
WHERE
t.createdDate >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY
cal.dt
ORDER BY
cal.dt DESC;
In MySQL 8+, you can use a recursive CTE to generate the dates:
with recursive dates as (
select curdate() - interval (1 - day(curdate())) day as yyyymm, 1 as lev
union all
select yyyymm - interval 1 month, lev + 1
from dates
where lev < 12
)
select d.yyyymm, count(t.createdDate) as cnt
from dates d left join
tablename t
on t.createdDate >= d.yyyymm and
t.createdDate < d.yyyymm + interval 1 month
group by d.yyyymm;
Note that this doesn't split the year and month into separate columns, but you can do that if you really want.
Also, this provide the current month with partial data. You can adjust the logic if you only want complete months.

MYSQL SELECT QUERY show all dates between, even if data not present with count=0

Below is the query i have written to fetch the order count with status=1 for last week.
SELECT count(*) as order_count,
DATE_FORMAT(order_date,'%d-%b-%Y') as order_date,
status
FROM customer_order
WHERE date(order_date) >= curdate()
- INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date(order_date) < curdate()
- INTERVAL DAYOFWEEK(curdate())-1 DAY
AND status=1
GROUP BY DATE_FORMAT(order_date, '%Y%m%d'),
status
I am getting result only for the dates present in the table. I need all the dates with count = 0 if data is not present for particular date.
You could try:
SELECT DATE_FORMAT(c1.order_date,'%d-%b-%Y') AS order_date
FROM customer_order AS c1
WHERE DATE_FORMAT(c1.order_date,'%d-%b-%Y')
IN ( SELECT DATE_FORMAT(c2.order_date,'%d-%b-%Y') as order_date2
FROM customer_order AS c2
WHERE date(DATE_FORMAT(c2.order_date,'%d-%b-%Y')) >= curdate()
- INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date(DATE_FORMAT(c2.order_date,'%d-%b-%Y')) < curdate()
- INTERVAL DAYOFWEEK(curdate())-1 DAY
AND c2.status=1
GROUP BY DATE_FORMAT(DATE_FORMAT(c2.order_date,'%d-%b-%Y'), '%Y%m%d'),
c2.status
HAVING count(*)=0
)

HOW To get monthwise report of downloaded songs of last 1 year from current date in mysql

I want to get no of downloads per month from current date since last 1 year
This is my query
SELECT a.imei,
a.numberofdownloads,
a.numberofdownloads AS aug,
a.numberofstreams AS jul,
a.numberofdownloads AS jun,
a.numberofstreams,
b.datetime
FROM activeusers a,
imei_downloadstream b
WHERE a.imei = b.imei
AND b.datetime BETWEEN Date_format(Curdate() - INTERVAL 1 month,
'%Y-%m-01 00:00:00'
) AND
Date_format(Last_day(Curdate() -
INTERVAL 1 month),
'%Y-%m-%d 23:59:59')
AND Date_format(Last_day(Curdate() - INTERVAL 2 month),
'%Y-%m-%d 23:59:59')
AND Date_format(Last_day(Curdate() - INTERVAL 3 month),
'%Y-%m-%d 23:59:59')
LIMIT 10;
You are very close. You need a way to determine the first day of the month from the current date.
You have almost figured that out. It is this:
DATE(DATE_FORMAT(CURDATE(), '%Y-%m-01'))
Then you need to use a GROUP BY clause featuring that expression. Something like this will do it.
SELECT a.imei,
SUM(a.numberofdownloads) AS numberofdownloads,
DATE(DATE_FORMAT(b.datetime, '%Y-%m-01')) AS month_beginning
FROM activeusers a,
JOIN imei_downloadstream b ON a.imei = b.imei
WHERE b.datetime >= DATE(DATE_FORMAT(CURDATE(), '%Y-%m-01')) - INTERVAL 12 MONTH
AND b.datetime < DATE(DATE_FORMAT(CURDATE(), '%Y-%m-01'))
GROUP BY DATE(DATE_FORMAT(b.datetime, '%Y-%m-01')), a.imei
See what's going on here? You're choosing all the records having dates between the first of the month twelve months ago and the first of the present month. Then you're computing the beginning date of the month for each record, then you're grouping by that value.
This works flawlessly if you get your JOIN right. Here is a more detailed writelup.
http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

count amount of rows in per half year with different years

I want to count amount of ID's for each month for the last half year, here is my query:
SELECT count(`Id`) FROM `items` WHERE MONTH(Created) = MONTH(CURRENT_DATE) UNION SELECT `count(`Id`) FROM `items` WHERE MONTH(Created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) UNION SELECT count(`Id`) FROM `items` WHERE MONTH(Created) = MONTH(CURRENT_DATE - INTERVAL 2 MONTH) UNION SELECT count(`Id`) FROM `items` WHERE MONTH(Created) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH) UNION SELECT count(`Id`) FROM `items` WHERE MONTH(Created) = MONTH(CURRENT_DATE - INTERVAL 4 MONTH) UNION SELECT count(`Id`) FROM `items` WHERE MONTH(Created) = MONTH(CURRENT_DATE - INTERVAL 5 MONTH);`
Created is the time stamp of the created row.
My problem is getting the fourth, fifth and sixth months. Because they were created in 2013 (different year), my query can't get their result.
I would start with something like this:
SELECT MONTH(created), count(*)
FROM `items`
WHERE Created >= LAST_DAY(current_date() - INTERVAL 6 MONTH) + INTERVAL 1 DAY
GROUP BY MONTH(created)
Where LAST_DAY(current_date() - INTERVAL 6 MONTH) + INTERVAL 1 DAY will return the first day of the month where to start the count, you might also want to group by month and year:
SELECT DATE_FORMAT(created, '%Y-%m'), count(*)
FROM `items`
WHERE Created >= LAST_DAY(current_date() - INTERVAL 6 MONTH) + INTERVAL 1 DAY
GROUP BY DATE_FORMAT(created, '%Y-%m')