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.
Related
I have a screenshot table and I want to get the user screenshot time starts and screenshot time ends. I want to create a query to be able to export the data to provide to my users.
Let's say this is my table data.
scs_id
scs_tracker_id
created_at
1
1000
2022-02-22 00:00:00
2
1001
2022-02-22 04:00:00
3
1000
2022-02-22 01:00:00
4
1002
2022-02-22 12:00:00
5
1001
2022-02-22 08:00:00
3
1000
2022-02-22 02:00:00
My expected output should be:
scs_tracker_id
screenshot_starts
screenshot_ends
1000
2022-02-22 00:00:00
2022-02-22 02:00:00
1001
2022-02-22 04:00:00
2022-02-22 08:00:00
1002
2022-02-22 12:00:00
2022-02-22 12:00:00
Code that I'm playing as of the moment:
SELECT
(SELECT MIN(created_at) FROM screen_shots GROUP BY scs_tracker_id ORDER BY scs_id ASC LIMIT 1) AS screenshot_starts,
(SELECT MAX(created_at) FROM screen_shots GROUP BY scs_tracker_id ORDER BY scs_id DESC LIMIT 1) AS screenshot_ends
FROM screen_shots
Aggregate by tracker ID and then take the min/max timestamp:
SELECT
scs_tracker_id,
MIN(created_at) AS screenshot_starts,
MAX(created_at) AS screenshot_ends
FROM screen_shots
GROUP BY scs_tracker_id;
Let's say that I have a database, that looks like that:
price date hour
12.00 2018-12-11 5
13.00 2018-12-04 2
14.00 2018-12-06 1
15.00 2018-12-11 1
16.00 2018-12-04 6
17.00 2018-12-06 10
I need to order by date and if days are the same after hour, so results should be:
price date hour
13.00 2018-12-04 2
16.00 2018-12-04 6
14.00 2018-12-06 1
17.00 2018-12-06 10
15.00 2018-12-11 1
12.00 2018-12-11 5
I tried to write a simple query, but it couldn't take into account 2 conditions, one after another:
SELECT price, date, hour
FROM table
WHERE date BETWEEN '2018-12-04' AND '2018-12-11'
ORDER BY date, hour
Could anyone help with this issue ?
Thanks All !
The only real issue I can think of would be if hour were stored as a string. If so, use implicit conversion:
SELECT price, date, hour
FROM table
WHERE date BETWEEN '2018-12-04' AND '2018-12-11'
ORDER BY date, hour + 0;
A simple ORDER BY will suffice:
select * from my_table order by `date`, hour;
Please note the date is usually a reserved word, so its usage is discouraged, and needs to be quoted.
I am using Mysqli
How can I do a single query that filters a subquery result? Is it possible?
I need to find users from a subquery result in a particular timestamp period.
SELECT * FROM Table WHERE (using subquery results i.e SELECT ...) BETWEEN 2017-01-08 09:00:00 AND 2017-01-08 12:00:00.
I should get back Mr A and Mr B.
Subquery results:
TimeIn User TimeOut
2017-01-06 10:00:00 Mr A 2017-01-08 11:00:00
2017-01-07 10:00:00 Mr A 2017-01-08 12:00:00
2017-01-08 10:00:00 Mr A 2017-01-08 13:00:00
2017-01-08 09:00:00 Mr B 2017-01-08 11:00:00
Thank you for your kind help as I am quite lost on how to do this.
Like this
SELECT * FROM
(
select ...
) subquery_alias_name
where timeIn BETWEEN '2017-01-08 09:00:00' AND '2017-01-08 12:00:00'
Don't forget to give the subquery an alias name
I have some customer financial data as shown below and can't figure out how to display the start and end contribution dates for each regular amount that has been paid. I'm using SQL Server 2008. My source data appears below.
Cust Contrib_Date Amount
---- ------------ ------
100 2013-01-01 500.00
100 2013-02-01 500.00
100 2013-03-02 500.00
100 2013-04-01 500.00
100 2013-05-05 600.00
100 2013-06-06 600.00
100 2013-07-05 600.00
100 2013-09-10 500.00
100 2013-10-10 500.00
100 2013-11-10 500.00
100 2013-12-11 500.00
200 2010-01-01 100.00
200 2010-02-02 100.00
200 2010-03-09 300.00
Here is what I'm trying to get from the output.
Cust Start_Date End_Date Amount
---- ---------- ---------- ------
100 2013-01-01 2013-04-01 500.00
100 2013-05-05 2013-07-05 600.00
100 2013-09-10 2013-12-11 500.00
200 2010-01-01 2010-02-02 100.00
200 2010-03-09 2010-03-09 300.00
The customer is making regular monthly contributions into an account. The amounts stay the same over each period (we have 3 for Cust = 100, i.e. 1st period 500.00 was paid, 2nd period 600.00 and 3rd period was back to 500.00) but the contribution dates may vary slightly by a couple of days due to weekends, public holidays, etc. as demonstrated above. So, I need to loop through the data, find the min and max contribution dates in chronological order for each amount for every customer and when I come across a different amount start the process again. There will be many customers that this would need to be done for.
I'm relatively OK with SQL but can't figure a way how to do this. Does anyone have any ideas? Can anyone help? I've used cursors before but not that often.
Try this:
SELECT y.Cust,
MIN(y.Contrib_Date) AS FromDate,
MAX(y.Contrib_Date) AS ToDate,
y.Amount
FROM (
SELECT x.Cust, x.Contrib_Date, x.Amount,
ROW_NUMBER() OVER(PARTITION BY Cust ORDER BY x.Contrib_Date ASC)
-
ROW_NUMBER() OVER(PARTITION BY Cust ORDER BY x.Amount ASC) AS GroupID
FROM #Contrib x
) y
GROUP BY y.Cust, y.GroupID, y.Amount
ORDER BY y.Cust, FromDate;
Ouput:
Cust FromDate ToDate Amount
---- ---------- ---------- ------
100 2013-01-01 2013-04-01 500.00
100 2013-05-05 2013-07-05 600.00
100 2013-09-10 2013-12-11 500.00
200 2010-01-01 2010-02-02 100.00
200 2010-03-09 2010-03-09 300.00
SQL Fiddle demo
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