I want the start and end dates of 4 quarters, so that I can use BETWEEN function to count how much data added in each quarter. How do I get the dates?
$this->db->select('SUM(CASE WHEN(data.added_on BETWEEN MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE())-2 QUARTER AND MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE())-1 QUARTER - INTERVAL 1 DAY ) THEN 1 ELSE 0 END) data_quarter3');
this will work for the previous quarter, but I don't want this. Any solution is welcome.
You don't need the dates, just specify the quarter and year you want in your WHERE statement
WHERE QUARTER(data.added_on) = 2 AND YEAR(data.added_on) = YEAR(NOW())
Related
I want to get data from training_course table where current date minus 5 days is equal to training_end_date. Training_end_date is my field in the table.
Thanks
You seem to want:
select *
from training_course
where training_end_date = current_date - interval 5 day
Or, if your dates have time components, you maybe want:
select *
from training_course
where training_end_date >= current_date - interval 5 day and training_date < current_date - interval 4 day
Hi I have a MySQL database that already has auto increment date time colomn (may information help).
How to filter last one week data as start from specific day until next 7 day.
example. Show data from Wednesday on this week until nest 7 day
$query="SELECT * FROM diagnosa WHERE schedule= '$a'";
$a is start day the data start to be shown (example Wednesday)
This may help you...
SELECT *
FROM table
WHERE date_colunm between 'start_date' AND DATE_ADD("start_date", INTERVAL 7 DAY);
UPDATE :
As you need from the Wednesday. Please us below query
SELECT *
FROM table
WHERE date_colunm
between (
CASE
WHEN DAYOFWEEK(curdate()) > 4
THEN DATE_SUB(NOW(), INTERVAL (7-DAYOFWEEK(curdate())) DAY)
ELSE (
CASE
WHEN DAYOFWEEK(curdate()) < 4
THEN DATE_ADD(NOW(), INTERVAL (4-DAYOFWEEK(curdate())) DAY)
ELSE curdate()
END
)
END
) as date
AND DATE_ADD (
(
CASE
WHEN DAYOFWEEK(curdate()) > 4
THEN DATE_SUB(NOW(), INTERVAL (7-DAYOFWEEK(curdate())) DAY)
ELSE (
CASE
WHEN DAYOFWEEK(curdate()) < 4
THEN DATE_ADD(NOW(), INTERVAL (4-DAYOFWEEK(curdate())) DAY)
ELSE curdate()
END
)
END
) as date, INTERVAL 6 DAY
)
NOTE :
There are some codes for the days like for Wednesday we have 4 (as mentioned in query),
so you can change the code according to your requirements. Like for Monday is : 2
I would like the database to count how many reports were generated for last month only and not from the current date backwards a month as it currently is e.g. 16/08/15 to 16/08/15. For example, I would like it to only count the total for August 2015, then once it is October, count the data for September 2015, so you can look back at the previous month how many reports were generated in the database.
I hope that makes sense? It will be used to count how many reports an employee has created to work out commission.
<?php
if ($result = $mysqli->query("SELECT count(inventory_id) cc FROM inventories WHERE inventory_date > CURRENT_DATE() - INTERVAL 1 MONTH")) {
$row = $result->fetch_assoc();
printf("<div class='col-6 statsMonth inventoryMonthStats'>Inventories in 30 days <span class='statCircle'>%d</span></div>", $row['cc']);
$result->close();
}
?>
Gets a bit ugly, but... assuming you always want "previous" month:
SELECT ...
...
WHERE YEAR(inventory_date) = YEAR(now() - interval 1 month)
AND MONTH(inventory_date) = MONTH(now() - interval 1 month)
If you want arbitrary previous months, then swap now() for a date in the month you want to calculate the "previous" of.
I want a mysql DATE_ADD(date_field_column, interval ## ####) method to jump on next working day.
Ex. Date1 = 10-07-2015 & Date2 = 20-07-2015
There are total 5 days working days (Mon-Fri) so I have a query that if date column has date 10-07-2015 then it should jump on 13-07-2015 as next working day.
Please use below code, this code will return you to date of next working day(exclude Saturday and Sunday).
I set example with SELECT statement so modify as per your requirement.
SELECT DATE_ADD(
'2015-07-10', INTERVAL
IF(DAYNAME('2015-07-10') = 'Saturday', 2,
IF(DAYNAME('2015-07-10') = 'Friday', 3, 1)
) DAY);
NOTE: Date format use for this example is YYYY-MM-DD.
Let me know if you face any query/concern regarding this.
Thanks!
use this
IF(
DAYOFWEEK(dat) = '6' OR DAYOFWEEK(dat) = '7' ,
IF(DAYOFWEEK(dat)='6', DATE_ADD(dat, INTERVAL + 3 DAY), DATE_ADD(dat, INTERVAL + 2 DAY)),
DATE_ADD(dat, INTERVAL + 1 DAY)
) AS newdat
I too had this requirement, however this won't work if your requirement would also include holidays. you then have to connect with your holiday master table and have to do it on application side.
I have a fun one for you. I have a database with the date columns free_from and free_until. What I need to find is the amount of days between now and 1 month today which are free. For example, if the current date was 2013/01/15 and the columns were as follows:
free_from | free_until
2013/01/12| 2013/01/17
2013/01/22| 2013/01/26
2013/01/29| 2013/02/04
2013/02/09| 2013/02/11
2013/02/14| 2013/02/17
2013/02/19| 2013/02/30
The answer would be 16
as 2 + 4 + 6 + 2 + 2 + 0 = 16
The first row only starts counting at the 15th rather than the 12th
since the 15th is the current date.
The last row is discounted because none of the dates are within a
month of the current date.
The dates must be counted as it the free_from date is inclusive and
the free_until date is exclusive.
I'm assuming DATEDIFF() will be used somewhere along the line, but I can't, for the life of me, work this one out.
Thanks for your time!
Edit: This is going into PHP mysql_query so that might restrict you a little concerning what you can do with MYSQL.
SET #today = "2013-01-15";
SET #nextm = DATE_ADD(#today, INTERVAL 1 month);
SET #lastd = DATE_ADD(#nextm, INTERVAL 1 day);
SELECT
DATEDIFF(
IF(#lastd> free_until, free_until, #lastd),
IF(#today > free_from, #today, free_from)
)
FROM `test`
WHERE free_until >= #today AND free_from < #nextm
That should work. At least for your test data. But what day is 2013/02/30? :-)
Dont forget to change #today = CURDATE();
The best I can think of is something like:
WHERE free_until > CURDATE()
AND free_from < CURDATE() + INTERVAL '1' MONTH
That will get rid of any unnecessary rows. Then on the first row do in PHP:
date_diff(date(), free_until)
On the last row, do:
date_diff(free_from, strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month"))
Then on intermediate dates do:
date_diff(free_from, free_until)
Something to that effect, but this seems extremely clunky and convoluted...
From the top of my mind... first do a:
SELECT a.free_from AS a_from, a.free_until AS a_until, b.free_from AS b_from
FROM availability a
INNER JOIN availability b ON b.free_from > a.free_until
ORDER BY a_from, b_from
This probably will return a set of rows where for each row interval you have next i.e. greater intervals. The results are ordered strategically. You can then wrap the results in a partial group by:
SELECT * FROM (
SELECT a.free_from AS a_from, a.free_until AS a_until, b.free_from AS b_from
FROM availability a
INNER JOIN availability b ON b.free_from > a.free_until
ORDER BY a_from, b_from
) AS NextInterval
GROUP BY a_from, b_until
In the above query, add a DATE_DIFF clause (wrap it in SUM() if necessary):
DATE_DIFF(b_until, a_from)