MySQL Rolling Value - subquery with joined value - mysql

I am trying to do a single query over a period of a month. This is a working query:
SELECT AVG(days)
FROM (SELECT datediff(IF(MIN(date_end) = '0000-00-00', DATE(NOW()), MAX(date_end)), MIN(date_start)) AS days
FROM tenancies
WHERE deleted_at IS NULL AND date_start < DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY tenancies.tenant_id)
I want to replace NOW() with a date.
I have another query:
SELECT calendar_date
FROM calendar_dates
WHERE calendar_date BETWEEN NOW() - INTERVAL 1 MONTH AND NOW()
This gets me all the dates I want. If I try to do a double-subquery it doesn't recognize the calendar_date:
SELECT calendar_date, (SELECT AVG(days)
FROM (SELECT datediff(IF(MIN(date_end) = '0000-00-00', DATE(calendar_date), MAX(date_end)), MIN(date_start)) AS days
FROM tenancies
WHERE deleted_at IS NULL AND date_start < DATE_SUB(calendar_date, INTERVAL 1 MONTH)
GROUP BY tenancies.tenant_id) d) AS days
FROM calendar_dates
WHERE calendar_date BETWEEN NOW() - INTERVAL 1 MONTH AND NOW()
Anyone have any suggestions?

I assume it does not recognize DATE(calendar_date) and DATE_SUB(calendar_date, INTERVAL 1 MONTH), If right, Your tenancies table should have a calendar_date field in itself, otherwise you need to join tenancies with calendar_dates table in the middle query too. Because thats a separate query.

Related

Counting all rows in column with two different date conditions

I'm trying to turn two count queries with date conditions (the ones below) into one query.
SELECT COUNT(*) as yesterday FROM orders WHERE DATE(timedate) = DATE(NOW() - INTERVAL 1 DAY)
SELECT COUNT(*) as yesterday FROM orders WHERE DATE(timedate) = DATE(NOW() - INTERVAL 2 DAY)
Following the advice of another answer I created the following, but that doesn't seem to work syntax-wise, and I'm not quite sure why. Is there another way to do this? I can't find a similar question on this
SELECT
SUM(IF(DATE(timedate) = DATE(NOW() - INTERVAL 1 DAY))) AS testcount1,
SUM(IF(DATE(timedate) = DATE(NOW() - INTERVAL 2 DAY))) AS testcount2
FROM
orders
You're missing the output values for the IF expression. Also you should use CURRENT_DATE() so you don't need to convert to a DATE:
SELECT
SUM(IF(DATE(timedate) = CURRENT_DATE() - INTERVAL 1 DAY, 1, 0)) AS testcount1,
SUM(IF(DATE(timedate) = CURRENT_DATE() - INTERVAL 2 DAY, 1, 0)) AS testcount2
FROM
orders
Note that MySQL treats boolean expressions as 1 (true) or 0 (false) in a numeric context, so you can actually SUM the expression without needing the IF:
SELECT
SUM(DATE(timedate) = CURRENT_DATE() - INTERVAL 1 DAY) AS testcount1,
SUM(DATE(timedate) = CURRENT_DATE() - INTERVAL 2 DAY) AS testcount2
FROM
orders
You want conditional aggregation. I would phrase the query as follows:
SELECT
SUM(
timedate >= CURRENT_DATE - INTERVAL 1 DAY
and timedate < CURRENT_DATE
) AS testcount1,
SUM(
timedate >= CURRENT_DATE - INTERVAL 2 DAY
and timedate < CURRENT_DATE- INTERVAL 1 DAT
) AS testcount2
FROM orders
Details:
this uses a nice feature of MySQL, that evaluates false/true conditions as 0/1 in numeric context
no date functions are applied on the timedate column : instead, we do litteral date comparisons. This is much more efficient, since the database can possibly take advantage of an index on the datetime column
You might also want to add a WHERE clause to the query:
WHERE
timedate >= CURRENT_DATE - INTERVAL 2 day
AND timedate< CURRENT_DATE

sql query to show records created this month

I want to write an sql query that must show me records that are only created last month..for example, if this month is June, I want the query to show me records of May.
This is my query but it is not working.
SELECT COUNT(*) AS stdtotal_today FROM `login`
WHERE `login_account_type` = 'STUDENT'
AND `account_created_date` = CURDATE() - 30
Note that login is the table name and account_created_date is the column name of type date.
Try This :
SELECT * FROM `login`
WHERE `login_account_type` = 'STUDENT' AND YEAR(`account_created_date`) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(`account_created_date`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Try this...
SELECT COUNT(*) AS stdtotal_today FROM `login` WHERE account_created_date
BETWEEN DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01 00:00:00') AND DATE_FORMAT(LAST_DAY(NOW() - INTERVAL 1 MONTH), '%Y-%m-%d 23:59:59')

Count of records for the last and current month unix timestamp

I need to make statistics page for billing with auto query. For example now is july and query must show count of record for the june and current count of records on current day(only july). Sort of:
"records for the last month - 85, for the current day - 32"
I have table customer and row create_time but it is in unix timestamp. Tried
SELECT COUNT(*) FROM customer WHERE create_time >=
UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 MONTH))
but its absolutely not what i want.
I would appreciate for any help.
Try this:
SELECT *
FROM ( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01')
AND create_time < LAST_DAY(now() - interval 1 month )
) AS s1,
( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(extract(year from now()),'-',extract(month from now()),'-01')
AND create_time <= now()
) AS s2
You can do this by adding condition to the WHERE:
create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 month))
in PHP you can calculate the one month back date
$newDate = strtotime('-1 month');
and use this in query

How to get average sales of an employee before certain sale in MySQL?

Suppose I have a table with 3 columns: EMPLOYEE_ID, NUM_SALES, DATE. Simply this is the table of Employees indicating daily sales. For each row in the table, I try to compute this; average number of sales of that EMPLOYEE_ID in the last K days excluding this day.
How can I query this in MySQL? I try to group by with EMPLOYEE_ID and DATE but I cannot figure out how to find last K sales for each row.
To select an interval of days, you can use MySQL's DATE_SUB() function:
WHERE `date` >= DATE_SUB(NOW(), INTERVAL 3 DAY)
This will select all records that are from the past 3 days. However, to exclude "today" from that:
WHERE `date` BETWEEN
DATE_SUB(NOW(), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
After that you should be able to GROUP BY the employee_id to get what you're after:
SELECT
employee_id, avg(num_sales) AS avg_num_sales
FROM
employee_table
WHERE `date` BETWEEN
DATE_SUB(NOW(), INTERVAL 3 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY
employee_id
You need to be able to select items from your table, let's call it dailysale, by date.
Here's what you do.
SELECT employee_id, AVG(num_sales) AS avg_sales
FROM dailysale
WHERE date >= CURDATE() - INTERVAL 3 DAY
AND date < CURDATE()
GROUP BY employee_id
This uses two WHERE clauses to winnow down the date range you're using. date >= CURDATE() - INTERVAL 3 DAY excludes all records before midnight three days ago, and date < CURDATE() excludes all records on or after midnight today.
You need to use CURDATE() rather than NOW() because, well, NOW() includes the date and the present time of day. date < NOW() will include today's sales, because your date column only records dates and not times.
If you want to list the employees in order of sales, you could add
ORDER BY AVG(num_sales) DESC, employee_id
to the query.

Query to get all rows from previous month

I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)