MySql get previous rows byTimeStamp - mysql

I have:
Table infoUpdate
+-------+-----------+-----------+-----------+-----------+
| idKey1| idKey2 | __date_Update_ | DayWeek |
+-------+-----------+-----------+-----------+-----------+
| 1___ | __3____ | 2013.01.01 15:00:00 | 2 |
| 1___ | __3____ | 2013.01.01 18:00:00 | 2 |
| 1___ | __3____ | 2013.01.02 15:00:00 | 3 |
| 1___ | __3____ | 2013.01.02 18:00:00 | 3 |
| 1___ | __3____ | 2013.01.03 15:00:00 | 4 |
| 1___ | __3____ | 2013.01.03 18:00:00 | 4 |
+-------+-----------+-----------+-----------+-----------+
How to obtain only previous rows by timeStamp
Select * FROM infoUpdate if where date_Update <='2013.01.03 18:00:00';
This is what I'm looking at obtaining:
+-------+-----------+-----------+-----------+-----------+
| idKey1| idKey2 | date_Update | DayWeek |
+-------+-----------+-----------+-----------+-----------+
| 1 | 3 | 2013.01.01 18:00:00 | 2 |
| 1 | 3 | 2013.01.02 18:00:00 | 3 |
| 1 | 3 | 2013.01.03 18:00:00 | 4 |
+-------+-----------+-----------+-----------+-----------+

will this work for you
Select * FROM infoUpdate where date_Update >= DATE_SUB( '2013.01.03 18:00:00' ,INTERVAL 90 DAY )
this will give you 90 days records previous to the date mentioned

Will it work for you:
Select idKey1, idKey2 , DayWeek, MAX(dateUpdate) as dateUpdate
FROM infoUpdate
where date_Update <='2013.01.03 18:00:00'
GROUP BY idKey1, idKey2 , DayWeek, DATE(dateUpdate);
If you are interested in last week data only, DATE(dateUpdate) can be removed from GROUP BY.

Related

Show counts of all date according to another table in sql?

I have two tables, One is calendar and second is final_registration as follow:
My Calendar table:
*--------------------*
| S.No. | datefield |
*--------------------*
| 1 | 2019-01-01 |
| 2 | 2019-01-02 |
| 3 | 2019-01-03 |
| 4 | 2019-01-04 |
| 5 | 2019-01-05 |
| 6 | 2019-01-06 |
| 7 | 2019-01-07 |
| 8 | 2019-01-08 |
| 9 | 2019-01-09 |
| 10 | 2019-01-10 |
| 11 | 2019-01-11 |
| 12 | 2019-01-12 |
| 13 | 2019-01-13 |
| 14 | 2019-01-14 |
| 15 | 2019-01-15 |
| 16 | 2019-01-16 |
| 17 | 2019-01-17 |
| 18 | 2019-01-18 |
| 19 | 2019-01-19 |
| 20 | 2019-01-20 |
| 21 | 2019-01-21 |
| 22 | 2019-01-22 |
| 23 | 2019-01-23 |
| 24 | 2019-01-24 |
| 25 | 2019-01-25 |
| 26 | 2019-01-26 |
| 27 | 2019-01-27 |
| 28 | 2019-01-28 |
| 29 | 2019-01-29 |
| 30 | 2019-01-30 |
| 31 | 2019-01-31 |
---------------------
My Second table:
*-----------------------------------------*
| id | event_id | name | booking_date |
*-----------------------------------------*
| 1 | 101 | Ritu | 2019-01-15 13:21 |
| 2 | 101 | Seeta | 2019-01-15 18:21 |
| 3 | 101 | Geeta | 2019-01-16 13:21 |
| 4 | 102 | Wasim | 2019-01-16 14:21 |
| 5 | 102 | Rahul | 2019-01-17 13:21 |
| 6 | 101 | Gagan | 2019-01-17 14:21 |
| 7 | 101 | Sunny | 2019-01-17 15:21 |
| 8 | 101 | Aman | 2019-01-17 16:21 |
-------------------------------------------
I am trying below output:
*--------------------*
| datefield | count |
*--------------------*
| 2019-01-01 | 0 |
| 2019-01-02 | 0 |
| 2019-01-03 | 0 |
| 2019-01-04 | 0 |
| 2019-01-05 | 0 |
| 2019-01-06 | 0 |
| 2019-01-07 | 0 |
| 2019-01-08 | 0 |
| 2019-01-09 | 0 |
| 2019-01-10 | 0 |
| 2019-01-11 | 0 |
| 2019-01-12 | 0 |
| 2019-01-13 | 0 |
| 2019-01-14 | 0 |
| 2019-01-15 | 2 |
| 2019-01-16 | 1 |
| 2019-01-17 | 3 |
---------------------
So as today 17-Jan so i need output till current date. I have tried query but it is not given me 01-Jan to 14-Jan
SELECT calendar.datefield, COUNT(calendar.datefield)
FROM calendar
LEFT JOIN final_registration
ON DATE_FORMAT(calendar.datefield, '%Y-%m-%d') = DATE_FORMAT(final_registration.booking_date, '%Y-%m-%d')
WHERE DATE_FORMAT(calendar.datefield, '%Y-%m-%d') <= DATE_FORMAT( CURDATE(), '%Y-%m-%d' )
AND DATE_FORMAT(calendar.datefield, '%Y-%m') = DATE_FORMAT( CURDATE(), '%Y-%m' )
AND final_registration.event_id = '101'
GROUP BY DATE_FORMAT(calendar.datefield, '%Y-%m-%d')
My query gave me below result:
*--------------------*
| datefield | count |
*--------------------*
| 2019-01-15 | 2 |
| 2019-01-16 | 1 |
| 2019-01-17 | 3 |
*--------------------*
I have tried lots but not able to get my result.
You are making your LEFT join into an INNER join by including criteria in the WHERE clause. Try this:
SELECT calendar.datefield, COUNT(calendar.datefield)
FROM calendar
LEFT JOIN final_registration
ON DATE_FORMAT(calendar.datefield, '%Y-%m-%d') = DATE_FORMAT
(final_registration.booking_date, '%Y-%m-%d')
AND final_registration.event_id = '101'
WHERE DATE_FORMAT(calendar.datefield, '%Y-%m-%d') <= DATE_FORMAT( CURDATE(), '%Y-%m-%d' )
AND DATE_FORMAT(calendar.datefield, '%Y-%m') = DATE_FORMAT( CURDATE(), '%Y-%m' )
GROUP BY DATE_FORMAT(calendar.datefield, '%Y-%m-%d')
You need to move some of the restrictions in the WHERE clause to the ON clause:
SELECT
c.datefield,
COUNT(f.booking_date) AS cnt
FROM calendar c
LEFT JOIN final_registration f
ON c.datefield = DATE(f.booking_date) AND
f.event_id = '101'
WHERE
c.datefield BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND CURDATE()
GROUP BY
c.datefield;
Demo
Note that you want to count a field from the final_registration table, which appears on the right side of the left join.
The delay in my answer was due to cleaning up your query to remove all the unneeded calls to DATE_FORMAT. You should be able to deal directly with the datefield. As for the WHERE clause, it seems that you just want dates which are in the current month, but no later than the current day in the month.

MySQL: Matching columns for year over year

This is a repost to help clarify my question. I am working with time series data and I need to reference a previous year's data. In excel I would just use a vlookup or index match.
Here is my table
+------------+--------------+--------------+------+-------+
| date | unique_id | prev_year_id | id | value |
+------------+--------------+--------------+------+-------+
| 2016-01-01 | 1-2016-01-01 | 1-2015-01-01 | 1 | 7 |
| 2016-01-01 | 2-2016-01-01 | 2-2015-01-01 | 2 | 19 |
| 2016-01-01 | 3-2016-01-01 | 3-2015-01-01 | 3 | 6 |
| 2016-01-01 | 4-2016-01-01 | 4-2015-01-01 | 4 | 13 |
| 2016-01-01 | 5-2016-01-01 | 5-2015-01-01 | 5 | 5 |
| 2017-01-01 | 1-2017-01-01 | 1-2016-01-01 | 1 | 17 |
| 2017-01-01 | 2-2017-01-01 | 2-2016-01-01 | 2 | 8 |
| 2017-01-01 | 3-2017-01-01 | 3-2016-01-01 | 3 | 20 |
| 2017-01-01 | 4-2017-01-01 | 4-2016-01-01 | 4 | 3 |
| 2017-01-01 | 5-2017-01-01 | 5-2016-01-01 | 5 | 0 |
| 2018-01-01 | 1-2018-01-01 | 1-2017-01-01 | 1 | 4 |
| 2018-01-01 | 2-2018-01-01 | 2-2017-01-01 | 2 | 21 |
| 2018-01-01 | 3-2018-01-01 | 3-2017-01-01 | 3 | 7 |
| 2018-01-01 | 4-2018-01-01 | 4-2017-01-01 | 4 | 3 |
| 2018-01-01 | 5-2018-01-01 | 5-2017-01-01 | 5 | 6 |
+------------+--------------+--------------+------+-------+
my columns are:
date,
unique_id (this is the concat of id and date together; primary key),
prev_year_id (concat of id and date - interval 1 year),
id,
value
I need help making a statement to match the prev_year_id to the unique_id and return the value of the rows.
There will be a gap of the first year since there will not be any data to reference, but beyond the first year each prev_year_value will have a match to it's previous year's unique_id
This is what I imagine the end result looking like:
+------------+--------------+--------------+------+-------+-----------------+
| date | unique_id | prev_year_id | id | value | prev_year_value |
+------------+--------------+--------------+------+-------+-----------------+
| 2016-01-01 | 1-2016-01-01 | 1-2015-01-01 | 1 | 7 | null |
| 2016-01-01 | 2-2016-01-01 | 2-2015-01-01 | 2 | 19 | null |
| 2016-01-01 | 3-2016-01-01 | 3-2015-01-01 | 3 | 6 | null |
| 2016-01-01 | 4-2016-01-01 | 4-2015-01-01 | 4 | 13 | null |
| 2016-01-01 | 5-2016-01-01 | 5-2015-01-01 | 5 | 5 | null |
| 2016-01-01 | 1-2017-01-01 | 1-2016-01-01 | 1 | 17 | 7 |
| 2016-01-01 | 2-2017-01-01 | 2-2016-01-01 | 2 | 8 | 19 |
| 2016-01-01 | 3-2017-01-01 | 3-2016-01-01 | 3 | 20 | 6 |
| 2016-01-01 | 4-2017-01-01 | 4-2016-01-01 | 4 | 3 | 13 |
| 2016-01-01 | 5-2017-01-01 | 5-2016-01-01 | 5 | 0 | 5 |
| 2016-01-01 | 1-2018-01-01 | 1-2017-01-01 | 1 | 4 | 17 |
| 2016-01-01 | 2-2018-01-01 | 2-2017-01-01 | 2 | 21 | 8 |
| 2016-01-01 | 3-2018-01-01 | 3-2017-01-01 | 3 | 7 | 20 |
| 2016-01-01 | 4-2018-01-01 | 4-2017-01-01 | 4 | 3 | 3 |
| 2016-01-01 | 5-2018-01-01 | 5-2017-01-01 | 5 | 6 | 0 |
+------------+--------------+--------------+------+-------+-----------------+
And this is the statement I have..
SELECT A.*, B.value as 'prev_year_value'
FROM table1 A
INNER JOIN table1 B ON A.current_month_code = B.`prev_year_code
The results are very slow to return and it returns the next year's value, not the previous year.
SELECT a.*, b.`value` as `prev_year_value`
FROM `table1` a
LEFT JOIN `table1` b
ON a.`prev_year_id` = b.`unique_id`
Since this is a left join, if there is no result for the "ON" statement, the results will still include the values from a, and the prev_year_value will end up being null, as you want.
In inner joins like the one you tried, it will only include results, where there is a matchup in both tables

MySQL: Get everyday incremental data

I want to fetch the data from Table based on date but in an incremental way.
Suppose I have data like this which is grouped by date
| DATE | Count |
| 2015-06-23 | 10 |
| 2015-06-24 | 8 |
| 2015-06-25 | 6 |
| 2015-06-26 | 3 |
| 2015-06-27 | 2 |
| 2015-06-29 | 2 |
| 2015-06-30 | 3 |
| 2015-07-01 | 1 |
| 2015-07-02 | 3 |
| 2015-07-03 | 4 |
So the result should come like this
| DATE | Count| Sum|
| 2015-06-23 | 10 | 10 |
| 2015-06-24 | 8 | 18 |
| 2015-06-25 | 6 | 24 |
| 2015-06-26 | 3 | 27 |
| 2015-06-27 | 2 | 29 |
| 2015-06-29 | 2 | 31 |
| 2015-06-30 | 3 | 34 |
| 2015-07-01 | 1 | 35 |
| 2015-07-02 | 3 | 38 |
| 2015-07-03 | 4 | 42 |
You would join every other previous date on that date, and then sum the count on that
If you give me your table structure, I can make it run.
id, name, date_joined
SELECT counts.theCount, sum(counts.theCount), table.date_joined
FROM yourTable
LEFT JOIN
(SELECT count(*) as theCount, table.date_joined
FROM yourTable
GROUP BY table.date_joined
) as counts
ON
yourTable.date_joined> counts.date_joined
GROUP BY yourTable.date_joined

Performing an operation within EACH GROUP of rows

I have a table like below.
+------------+------------------+-------------------------------+-------------+
| day | workflow_step_id | UNIX_TIMESTAMP(finished_time) | workflow_id |
+------------+------------------+-------------------------------+-------------+
| 2014-04-30 | 1 | 1398852780 | 1 |
| 2014-04-30 | 17 | 1398871213 | 2 |
| 2014-04-30 | 6 | 1398872807 | 1 |
| 2014-04-30 | 22 | 1398898676 | 2 |
| 2014-04-30 | 11 | 1398898234 | 1 |
| 2014-04-30 | 16 | 1398866813 | 2 |
| 2014-04-30 | 5 | 1398869940 | 1 |
| 2014-04-30 | 21 | 1398893419 | 2 |
| 2014-04-30 | 10 | 1398894136 | 1 |
| 2014-04-30 | 15 | 1398861385 | 2 |
| 2014-04-30 | 4 | 1398860271 | 1 |
| 2014-04-30 | 20 | 1398888252 | 2 |
| 2014-04-30 | 9 | 1398886916 | 1 |
| 2014-04-30 | 14 | 1398863922 | 2 |
| 2014-04-30 | 3 | 1398865682 | 1 |
| 2014-04-30 | 19 | 1398881994 | 2 |
| 2014-04-30 | 8 | 1398882497 | 1 |
| 2014-04-30 | 13 | 1398852785 | 2 |
| 2014-04-30 | 2 | 1398856674 | 1 |
| 2014-04-30 | 18 | 1398878836 | 2 |
| 2014-04-30 | 7 | 1398878949 | 1 |
| 2014-04-30 | 12 | 1398850920 | 2 |
| 2014-05-01 | 12 | 1398932040 | 2 |
| 2014-05-01 | 1 | 1398938880 | 1 |
| 2014-05-01 | 17 | 1398957830 | 2 |
| 2014-05-01 | 6 | 1398961385 | 1 |
| 2014-05-01 | 22 | 1398996388 | 2 |
| 2014-05-01 | 11 | 1398994543 | 1 |
| 2014-05-01 | 16 | 1398946714 | 2 |
| 2014-05-01 | 5 | 1398959182 | 1 |
| 2014-05-01 | 21 | 1398989773 | 2 |
| 2014-05-01 | 10 | 1398979568 | 1 |
| 2014-05-01 | 15 | 1398949939 | 2 |
| 2014-05-01 | 4 | 1398953732 | 1 |
| 2014-05-01 | 20 | 1398980246 | 2 |
| 2014-05-01 | 9 | 1398971281 | 1 |
| 2014-05-01 | 14 | 1398940775 | 2 |
| 2014-05-01 | 3 | 1398944207 | 1 |
| 2014-05-01 | 19 | 1398970295 | 2 |
| 2014-05-01 | 8 | 1398967778 | 1 |
| 2014-05-01 | 13 | 1398940935 | 2 |
| 2014-05-01 | 2 | 1398947433 | 1 |
| 2014-05-01 | 18 | 1398966959 | 2 |
| 2014-05-01 | 7 | 1398965931 | 1 |
-------------------------------------------------------------------------------
command:
SELECT day,
stats.workflow_step_id,
Max(Unix_timestamp(finished_time)) - Min(Unix_timestamp(finished_time)),
workflow_id
FROM modeling_dashboard_workflow_stats stats
INNER JOIN modeling_dashboard_workflow_step step
ON stats.workflow_step_id = step.workflow_step_id
ORDER BY day;
In this example only has two days and two workflow_ids, but it can have any number of days or workflow_ids.
I want to calculate the difference between maximum timestamp and minimum timestamp for each workflow_id for each day.
It should look something like this.
+------------+-------------------------------------------------------------+
| day | MAX(timestamp) - MIN(timestamp) |
+------------+-------------------------------------------------------------+
| 2014-04-30 | difference bw max&min timestamp with workflow_id=1 in 04-30 |
| 2014-04-30 | difference bw max&min timestamp with workflow_id=2 in 04-30 |
| 2014-05-01 | difference bw max&min timestamp with workflow_id=1 in 05-01 |
| 2014-05-01 | difference bw max&min timestamp with workflow_id=2 in 05-01 |
----------------------------------------------------------------------------
How can I do this?
Ideally it puts results for workflow_id = 1 and those for workflow_id = 2 in different columns, (day, MAX-MIN for id=1, MAX-MIN for id=2), but that's just my next step.
SELECT day, workflow_id
, MAX(UNIX_TIMESTAMP(finished_time)) - MAX(UNIX_TIMESTAMP(finished_time))
FROM modeling_dashboard_workflow_stats
GROUP BY day, workflow_id
try this:
select day
,workflow_step_id
,MAX(UNIX_TIMESTAMP(finished_time)) - MIN(UNIX_TIMESTAMP(finished_time))
from modeling_dashboard_workflow_stats
group by day
,workflow_step_id

MySQL query date ranges

I have googled but did not find anything related. I have a MySQL table like this:
+++++++++++++++++++++++++++++++
| roomID | date | price |
+++++++++++++++++++++++++++++++
| 1 | 2012-10-10 | 10 |
| 1 | 2012-10-11 | 10 |
| 1 | 2012-10-12 | 10 |
| 1 | 2012-10-13 | 12 |
| 2 | 2012-10-10 | 15 |
| 2 | 2012-10-11 | 15 |
| 2 | 2012-10-12 | 15 |
| 2 | 2012-10-13 | 16 |
| 2 | 2012-10-14 | 16 |
| 2 | 2012-10-15 | 16 |
+++++++++++++++++++++++++++++++
I need to get periods based on price and roomID:
++++++++++++++++++++++++++++++++++++++++++++
| roomID | from | till | price |
++++++++++++++++++++++++++++++++++++++++++++
| 1 | 2012-10-10 | 2012-10-12 | 10 |
| 1 | 2012-10-13 | 2012-10-13 | 12 |
| 2 | 2012-10-10 | 2012-10-12 | 15 |
| 2 | 2012-10-13 | 2012-10-15 | 16 |
++++++++++++++++++++++++++++++++++++++++++++
Thank you!
select roomid,
min(date) as from,
max(date) as till,
price
from periods
group by price
order by price
You can try using the following query:
SELECT roomid, MIN(date) AS `from`, MAX(date) AS `till`, price
FROM tableName
GROUP BY price
ORDER BY price