I have a table Aud_Usd with a Time with minute increments (08-09-2017 10:00:00) and a Price (FX conversion rate eg. 0.797). I would like to show in a single table:
Time | Price | Time + 5 minutes | Corresponding price to Time + 5 minutes
Essentially I would just like to compare how the price has moved in that period. I have been stuck on showing the corresponding price. Currently I have
SELECT Time, Price, ADDTIME(Time, '00:01:00') As tplus5, ??
Thanks!
Try this:
SELECT au.Time, au.Price, ADDTIME(au.Time, '00:05:00') As tplus5,
(select price from Aud_Usd where Time = ADDTIME(au.Time, '00:05:00')) as
PricePlus5
from Aud_Usd au
You can use SEC_TO_TIME to achieve this. So your query will be as follows:
SELECT Time, Price, ADDTIME(startTime, SEC_TO_TIME(300)) As tplus5, ....
Related
I have a table with stock prices. I want to query (with a specific timeframe) to analyze and get the prices back as a base 100 from the first value for that particular timeframe. Currently I have:
SELECT price.`date`,
FIRST_VALUE(`NASDAQ:AAPL`) OVER (ORDER BY price.`date`) AS `Base`,
price.`NASDAQ:AAPL`
FROM price
WHERE DATE date >= "XXX"
This returnes me for every row:
Date
First price of the period
Price for the day
Which correclty shows me the date, the price for each date and the first price for that period (Base).
How can I get a field that is the result of dividing each price / first price for the period?
I've tried
price.`NASDAQ:AAPL` / `Base` AS `Ratio`
But that doesn't seem to work.
Thanks!
Thanks to #forpas, I found a solution:
price.`NASDAQ:AAPL` / FIRST_VALUE(`NASDAQ:AAPL`) OVER (ORDER BY price.`date`) AS `Ratio`
Thanks!
I have a table in my database, my program will insert data to that table in every 10 mins.
The table has a field recording the insert date and time.
Now I want to retrieve those data, but I don't want hundreds of data comes out.
I want to get 1 records from every half hour based on insert time stamp (so less than 50 in total of a day).
For that 1 record, it can be either random pick or average from each interval.
Sorry for the ambiguit, cuz I just wanna figure out the way to select from intervals
Let say,
Table name: network_speed
----------------------------------
ID. ....... Speed ......... Insert_time
1 ....... 10 ......... 10:02am......
2 ....... 12 ......... 10:12am......
...
...
...
123 ....... 17 ........ 9:23am........
To get them all but out put must be average of each half hour record
How can I write a query to achieve this?
Here is a query that calculates half hour intervals on a specific day ( 2013-09-04).
SELECT ID, Speed, Insert_time,
ROUND(TIMESTAMPDIFF(MINUTE, '2013-09-04', Insert_time)/48) AS 'interval'
FROM network_speed
WHERE DATE(Insert_time) = '2013-09-04';
Use that in a nested query to get stats on the records in the intervals.
SELECT IT.interval, COUNT(ID), MIN(Insert_time), MAX(Insert_time), AVG(Speed)
FROM
(SELECT ID, Speed, Insert_time,
ROUND(TIMESTAMPDIFF(MINUTE, '2013-09-04', Insert_time)/48) AS 'interval'
FROM network_speed
WHERE DATE(Insert_time) = '2013-09-04') AS IT
GROUP BY IT.interval;
Here it is used to get the first record in each interval.
SELECT NS.*
FROM
(SELECT IT.interval, MIN(ID) AS 'first_id'
FROM
(SELECT ID, Speed, Insert_time,
ROUND(TIMESTAMPDIFF(MINUTE, '2013-09-04', Insert_time)/48) AS 'interval'
FROM network_speed
WHERE DATE(Insert_time) = '2013-09-04') AS IT
GROUP BY IT.interval) AS MI,
network_speed AS NS
WHERE MI.first_id = NS.ID;
Hope that helps.
Is this what you need?
SELECT HOUR(ts) as hr, fld1, fld2 from tbl group by hr
This query selects only hour from the timestamp and then groups the result based on the hour field so you get 1 row for each hour
I am looking to calculate moving averages over variable dates.
My database is structured:
id int
date date
price decimal
For example, I'd like to find out if the average price going back 19 days ever gets greater than the average price going back 40 days within the past 5 days. Each of those time periods is variable.
What I am getting stuck on is selecting a specific number of rows for subquery.
Select * from table
order by date
LIMIT 0 , 19
Knowing that there will only be 1 input per day, can I use the above as a subquery? After that the problem seems trivial....
if you only have one input per day you don't need id, date can be your primary id? Am i missing something? Then use select sum
SELECT SUM(price) AS totalPrice FROM table Order by date desc Limit (most recent date),(furthest back date)
totalPrice/(total days)
I may not understand your question
Yes you can use that as a sub-query like this:
SELECT
AVG(price)
FROM
(SELECT * FROM t ORDER BY date DESC LIMIT 10) AS t1;
This calculates the average price for the latest 10 rows.
see fiddle.
I'm in need of a method to store a time duration in a db field. I'm building a website where customers should be able to choose how long they would like an advert to display from a particular start date.
I had thought about using TIME but that has a max of '838:59:59' which works out at about 34 days. Its possible that a client would want an advert to exist for longer than that.
So what would be the best way to deal with this? Just a really large INT?
If you intend to have a column for start time and one for duration, I think you can store it in seconds. So, I assume you will have something like this;
+-----------+--------------------------+------------------+
| advert_id | start_time | duration_seconds |
+-----------+--------------------------+------------------+
| 2342342 |'2012-11-12 10:23:03' | 86400 |
+-----------+--------------------------+------------------+
(For the sake of the example, we will call this table adverts)
advert_id - a key pointing to your advert
start_time - the time the advert should start (data type - TIMESTAMP)
duration_seconds - Time in seconds that the advert is supposed to "live" (INTEGER(11)
SELECT TIME_TO_SEC(timediff(now(),start_time)) as 'time_difference_in_seconds_since_advert_started' FROM adverts;
If you want to get only adverts that have not expired, you will run a query like this;
SELECT * FROM `adverts` WHERE TIME_TO_SEC(timediff(now(),start_time))<=`duration_seconds`;
That's one way I would do it if I were to go with the "duration" field.
Yes, you can store time as INT data type (or another big integer: MEDIUMINT, LONGINT). Then use you can easily get days and time part from this, e.g. -
SELECT time DIV 86400 AS days, SEC_TO_TIME(column1 MOD 86400) AS time FROM table
Where 86400 is a number of seconds in 24h (60 * 60 * 24 = 86400).
not the best solution but you can add one column in your db, and check when time is more than 24 hours, calculate it as 1 day and write in that column, and all the rest time write in time column. But selecting from db you should calculate also that column of days
code id date time difference
AiK4JJ kcy2000ok 2012-01-31 17:25:41 13
unBG1D gktoql 2012-01-31 17:25:35 3
vzqeWU gktoql 2012-01-31 17:25:32 4
vvkOSd judyssi 2012-01-31 17:25:32 8
uwhbGt kcy2000ok 2012-01-31 17:25:28 ?
unBG1D gktoql 2012-01-31 17:25:27 ?
vvkOSd judyssi 2012-01-31 17:25:24 ?
I want to calculate the time difference in seconds between recent date and previous date based on id.
If you look at kcy2000ok, time difference is 13 seconds for last row.
time difference for gktoql is 3 seconds for last row.
I need to make a query that calculate time difference based on id.
Can you help me to build MySql query?
SELECT id, UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time) as difference FROM <your-table-name>
Addendum:
Your question was a little bit hard to understand until you editied it. So to make this clear: If you want to calculate the minimum difference grouped by id, you can do:
SELECT
id,
MIN(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time)) as difference
FROM your_table_name
GROUP BY id
If you mean find the number of seconds since the most recent record for each user:
SELECT id, min(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time))
GROUP BY id
Or to get the most recent record for each user:
SELECT * FROM (
SELECT *
FROM mytable
ORDER BY `date` desc) x
GROUP BY id