I have a table called eventdata like this
Timestamp GUId EventTypeId
2015-02-01 05:00:00 1100 E012-1
2015-02-01 05:30:00 1100 E012-2
2015-02-01 06:00:00 1100 E012-1
2015-02-01 06:20:00 1100 E012-2
2015-02-01 07:00:00 2200 E012-1
2015-02-01 07:15:00 2200 E012-2
Now corresponding to Guid, we have different EventTypeId. Now when I see E012-2 to corresponding E012-1 for a GUID (this could be different, for example sake I gave like this) I need a difference in Timestamp in minutes.
Example: for first two rows, for GUID=1100 it's 30. For the 3rd and 4th rows it's 20 minutes. for Guid=1100 for 5th and 6th rows it's 15 mints for Guid=2200.
I am using MySQL.
Related
I'm hoping the query will work both in Mysql and BigQuery
For each customer I need to find the first date they had a subscription and the first date that they stopped having any subscriptions (e.g. a break in subscription). A customer can have multiple overlapping subscriptions. Once a customer stops having access then future subscriptions are not considered.
This is a sample table with a few rows. The actual table will have millions of rows and thousands of customers.
using this sample data:
select * from test_sub order by customer_id, effect_date, expire_date;
subscription_id
customer_id
effect_date
expire_date
1
1
2022-01-01 00:00:00
2022-03-01 00:00:00
2
2
2021-01-01 00:00:00
2021-03-01 00:00:00
3
2
2021-02-01 00:00:00
2021-04-25 00:00:00
4
2
2021-05-01 00:00:00
2021-06-01 00:00:00
5
2
2021-08-01 00:00:00
2022-10-01 00:00:00
The answer should be:
customer_id
min(effect_date)
max(expire_date)
1
2022-01-01 00:00:00
2022-03-01 00:00:00
2
2021-01-01 00:00:00
2022-04-25 00:00:00
in my MySQL database I have the following view (originally combining 2 data tables) called MYDATA.
TABLE MYDATA
user
myDate
items
17
2020-01-01
1.0
22
2020-01-01
6.0
17
2020-01-02
3.2
17
2020-01-04
4.0
17
2020-01-08
1.0
17
2020-01-09
6.2
22
2020-01-09
4.0
17
2020-01-10
5.3
As you can see NOT all dates (column myDate) contain items. For a selected user (i.e. user 17) I need to calculate the moving average of sold items (column items) over past 14 days ("this" day included) for ALL DATES (i.e. including 2020-01-03 which is not included in the MYDATA table). So basically I want to obtain the following:
user
myDate
result
17
2020-01-01
(avg last 14 days)
17
2020-01-02
(avg last 14 days)
17
2020-01-03
(avg last 14 days)
...
...
...
17
2020-12-30
(avg last 14 days)
17
2020-12-31
(avg last 14 days)
Feel free to play with it in SQLFiddle: http://sqlfiddle.com/#!9/02cc94/1
If needed I have a table "calendar" containing all the year's dates as well.
TABLE CALENDAR
myDate
2020-01-01
2020-01-02
2020-01-03
2020-01-04
2020-01-05
2020-01-06
How can I proceed please? Thanks for any help. I've been stuck on this issue for months.
Look for
SELECT user, t1.myDate, SUM(t2.items) / 14 avg_items
FROM calendar t1
JOIN test t2 ON t2.myDate BETWEEN t1.myDate - INTERVAL 13 DAY and t1.myDate
GROUP BY t2.user, t1.myDate
ORDER BY 1,2
fiddle - MySQL8-specific construction (CTE) is used for calendar table generation only.
I have this table
FLIGHTS
FNO Departs Arrives Price
111 10:00:00 11:30:00 5000
222 13:30:00 18:00:00 6000
333 20:00:00 22:30:00 3000
444 22:45:00 23:30:00 1000
Requirement:
I want to calculate the TOTAL_TIME of Travel using only flights 111,222,333.
I tried TIMEDIFF and ADDTIME, but I am not able to add results of subquery and get in TIME format.
Try this:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(Arrives, Departs)))) Total_Duration
FROM FLIGHTS
WHERE FNO IN (111,222,333);
See DEMO on SQL Fiddle.
There's a DATETIME column called time. How could I select all rows that fall within the last existing 12 months (NOT within the last year from today)? Not every month might have a row, and months may have more than one row.
For example, out of this table (ORDER BY time DESC), rows with ids 2 to 17 would be selected.
id time
-- ----
17 2015-04-01
16 2015-04-01
15 2015-03-01
14 2015-02-01
13 2015-01-01
12 2014-12-01
11 2014-11-01
10 2014-10-01
9 2013-12-01
8 2013-11-01
7 2013-10-01
6 2013-09-01
5 2013-09-01
4 2013-09-01
3 2013-09-01
2 2013-08-01
1 2013-07-01
Another way to put this:
Take the table above and group by month/year, so we get:
2015-04
2015-03
2015-02
2015-01
2014-12
2014-11
2014-10
2013-12
2013-11
2013-10
2013-09
2013-08
2013-07
Now take the 12 most recent months from this list, which is everything except 2013-07.
2015-04
2015-03
2015-02
2015-01
2014-12
2014-11
2014-10
2013-12
2013-11
2013-10
2013-09
2013-08
And select everything from those months.
I guess I could do this with multiple queries or subqueries but is there another way to do this?
If your time field is only month-precision, you could do it with a pretty simple subselect:
SELECT * FROM Table t1
WHERE time IN (
SELECT DISTINCT time FROM Table t2 ORDER BY time DESC LIMIT 12
)
If your timestamps are full-precision, you could do the same thing, but you'd need to do some date manipulation to round the dates to the month for comparison.
How to order date, like this - 2012-02-01 00:00:00 by the hour,minutes,and the seconds, not by the year/moth/day.
If i have..
2012-02-01 02:00:00
2012-03-01 20:00:00
2012-04-01 12:00:00
2012-05-01 07:00:00
I wan't to get this output.
Column tipe is timestamp.
2012-02-01 02:00:00
2012-05-01 07:00:00
2012-04-01 12:00:00
2012-03-01 20:00:00
ORDER BY TIME(date_column)
This will, however, slow down your queries, as it isn't possible to index the on-the-fly calculation. If you have a lot of records, or if this query runs frequently, you should break the time portion of the date into its own column so you can index it for faster sorting.
Use the TIME() function to extract the time portion of the expression passed, e.g.
mysql> SELECT TIME('2012-02-01 02:00:00');
-> '02:00:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time