Going to make a line graph. I want to query count with difference of 5 days.
Suppose if I have following rows:
booking_date
2015-02-1
2015-02-3
2015-02-5
2015-02-6
2015-02-6
2015-02-9
2015-02-10
2015-02-15
2015-02-17
2015-02-23
2015-02-28
In above table column it contains date. Now How can I do mysql query so that it can return with difference of 5 days like:
1 => 3 // count of date between 2015-02-1 & 2015-02-05 is 3
2 => 4 // count of date between 2015-02-06 & 2015-02-10 is 4
3 => 1 // count of date between 2015-02-11 & 2015-02-15 is 1
4 => 1 // count of date between 2015-02-16 & 2015-02-20 is 1
5 => 1 // count of date between 2015-02-21 & 2015-02-25 is 1
6 => 1 // count of date between 2015-02-26 & 2015-02-30 is 1
Is any direct way to query like above. I am not so good at mysql. But can do php nicely.
You can do the following to get everything in the same query.
First of all get the unix timestamp for the date you want to start grouping in 5 days. In your example that would be 2015-02-01 -> 1422748800
Then the query would be the following:
SELECT COUNT(*), FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5)) as FiveDayPackNumber from tbl GROUP BY FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5))
Haven't tested it so it may require some tweaking but you can get the idea: It will group them by the number of 5-days-packs that passed since your initial date, starting at 0.
Do you mean to do something like:
SELECT COUNT(1) FROM {table_name} WHERE booking_date > 2015-02-01 AND booking_date < 2015-02-05
?
(completely off memory so you might need to slightly alter it)
SELECT count(*)
FROM tbl
WHERE booking_date >= start_date
AND booking_date <= end_date
Related
User input = '2017-03-12'
Let say I have this tableRevenue
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-15 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-19 100
2017-02-26 100
2017-03-05 100
2017-03-12 100
And another tableHolidays which contains
date
----------
2017-01-15
2017-02-19
2017-03-05
I want to display it like this:
date revenue
---------- ---------
2017-01-01 100
2017-01-08 100
2017-01-22 100
2017-01-29 100
2017-01-05 100
2017-01-12 100
2017-02-26 100
2017-03-12 100
I want to display the revenue each of the last 8 weeks and I want to skip all the dates that are existing in tableHolidays using a loop. Is this possible in PHP?
mention: you didn't tag any specific database - my answer will refer to SQL-Server:
assuming #UserDate is a variable with the user input date
Use Date-Functions (specific to every DB system) to calculate the date range. in your case to subtract the 8 weeks.
Select all rows within this date range
exclude (NOT IN) all dates from your resultset which occur in your tableHolidays table
GROUP BY weeks (calculate weeknumber with WEEK) and SUM the revenue
Query:
SELECT WEEK(tR.date) as weeknr
,SUM(tR.revenue)
FROM tableRevenue tR
WHERE tR.date >= DATEADD(wk,-8,#UserDate)
AND tR.date <= #UserDate
AND tR.date NOT IN (SELECT date FROM tableHolidays)
GROUP BY WEEK(tR.date)
you can use the 'ANY' which is a mysql operator
for more information you can visit this link
https://www.w3schools.com/sql/sql_any_all.asp
$userInput = '2017-03-12';
$sql = "SELECT `date`, `revenue`
FROM `tableRevenue`
WHERE (
(`date` = ANY
(SELECT `date` FROM `tableHolidays` WHERE DATE(date)<='{$userInput}'))
AND (DATE(date) <='{$userInput}')
)";
I am working on Payroll System where an employee can be deployed on projects from any_date to any_date. Sample MySQL table below
id empid project_id start_date end_date
1 1 2 2016-11-05 15:10:22 2016-12-11 15:11:21
2 1 3 2016-12-13 15:26:10 2016-12-20 15:29:40
3 1 2 2016-12-23 15:31:46 2017-01-18 15:32:35
Now, if I want to calculate the number of days in given month worked on all projects, I am using Eloquent which translates to below query(I verified in DB::getQueryLog()):
SELECT
*
FROM
payroll_project_tracks
WHERE
empid = 1
AND '2016-12-01 00:00:00' BETWEEN start_date AND end_date
AND '2016-12-31 23:59:59' BETWEEN start_date AND end_date
After getting the rows that have deployments of given month (month = Dec 2016 here), I want to calculate Number of Days worked on all projects in a given month.
I am getting no results with the above query. Can anyone correct me or let me know if there is some better way of doing this.
I think that the query should be:
SELECT *
FROM payroll_project_tracks
WHERE
empid = 1 AND
start_date <= '2016-12-31 23:59:59' AND
end_date >= '2016-12-01 00:00:00'
This query will return the 3 rows you provided, but will cut out the following:
4 1 2 2017-01-01 15:31:46 2017-01-18 15:32:35
is this ok?
I a have table in mysql with some data like this:
id from to
---- -------------------- ---------------------------
1 2013-01-31 23:50:00 2013-02-02 09:00:00
2 2013-02-05 11:21:12 2013-02-08 01:01:01
3 2013-02-08 17:33:44 2013-02-08 18:22:55
4 2013-02-12 01:40:12 2013-02-12 02:00:59
5 2013-02-28 01:40:12 2013-03-02 02:00:59
I need a Mysql query or a php code for finding difference between 'from' column and 'to' column of each rows, and find how many seconds are there in each day between 'from' and 'to' date separately, for example for row 1 the needed output be something like this:
difference between 2013-01-31 23:50:00 - 2013-02-02 09:00:00 for row 1
2013-01-31 : 600 sec (24:00:00 - 23:50:00 => 600 sec)
2013-02-01 : 86400 sec (24:00:00 - 00:00:00 => 86400 sec)
2013-02-02 : 32400 sec (09:00:00 - 00:00:00 => 32400 sec)
and so on ... for each row
MySQL command is preferred. which code can create this output? is there any specific function in php or mysql for creating this output?
I read these answers:
mysql calculate seconds between two date/times for each day
MySQL: how to get the difference between two timestamps in seconds
but these are not my answer.
for information:
link 1 is a question like my question but the answer is not true because "The command needs grouped by day" but how?!
and the second is not my question but question one marked as duplicated with link 2 and it is not true.
Let me assume that you have a table of dates. If so, you can use a join or correlated subquery:
select d.date,
(select sum(timestampdiff(second,
greatest(d.date, t.from),
least(d.date, t.to)
))
from table t
where t.to >= d.date and
t.from < date_add(d.date, interval 1 day)
) as NumSeconds
from dates d;
If you don't have a dates table of some sort, you can create one on the fly:
from (select date('2013-01-31') as date union all
select date('2013-02-01') union all
. . .
) dates
id name quantity timestamp
1 item1 2 2015-06-01 20:00:00
2 item2 5 2015-06-01 22:30:00
3 item3 2 2015-06-02 20:00:00
4 item4 7 2015-06-02 20:30:00
5 item5 9 2015-06-02 21:30:00
This is an example database, 'timestamp' is in datetime format and contains varying values. Note that the table contains various date and time data. How do i get all the rows of one day and of certain minute(above eg: 30) only, eg how can i select all the rows with timestamp 2015-06-01 **:30:00, here ** = hour any help, thanks.
You can use DATE & MINUTE functions -
SELECT * from your_table WHERE DATE(`timestamp`) = '2015-06-02' AND MINUTE(`timestamp`) = 30
You need to write follow query:
SELECT * FROM Your_Table_Name WHERE DATE(timestamp) = '2015-06-01' AND TIME(timestamp) LIKE '%:30:00'
I have an example
id projectid date
1 2 2015-04-19
2 2 2015-04-19
3 2 2015-04-19
4 2 2015-04-19
5 2 2015-04-21
6 2 2015-04-21
7 2 2015-04-21
8 2 2015-04-22
9 2 2015-04-22
10 2 2015-04-22
I want to show next upcoming stored date using 2015-04-19 date, i.e next date is 2015-04-21, using mysql query
id projectid date
5 2 2015-04-21
6 2 2015-04-21
7 2 2015-04-21
I have written query for you. I hope it will resolve your problem.
QUERY
SELECT * FROM table
WHERE `date` = (SELECT `date` FROM table
WHERE `date`>'2015-04-19' ORDER BY `date` ASC LIMIT 1);
Use DateAdd Function in Select Statement like below
DATEADD(d,1,(CASE table.date as Date))
Basically to display next date older than 2015-04-19 from your database, this should be enough:
SELECT * FROM table WHERE date > '2015-04-19' ORDER BY date ASC LIMIT 1
SELECT * FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1
UPDATE
You wrote that you want to show next upcoming stored date, but looking at your example output and your comment it looks like you want to get all records with the next date.
SELECT * FROM table WHERE date=(
SELECT date FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1
)
SQL Fiffle example, use it as a copy paste solution.
Sub query will return the next date from which results will come according to that date only.
SELECT * FROM tableName WHERE date=("SELECT date FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1")