How to calculate base 100 on MYSQL select query - mysql

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!

Related

My SQL Query - Get items that had there prices reduced in the last 30 days

I have a table that keeps track of inventory price updates.
CREATE TABLE pricechangelog (
id int,
SKU varchar(50),
oldSelling DOUBLE(22,2),
newSelling DOUBLE(22,2),
date DATETIME
);
I would like to get the items that had their price reduced overrall in the last 30 days,
how would I go about doing that
This could be possible by find the difference of the current timestamp and the date. DATEDIFF() Function, and the newselling value shuould be less than the oldselling
SELECT *
FROM pricechangelog
WHERE DATEDIFF(CURRENT_TIMESTAMP, date)<= 30
AND oldSelling>newSelling;

Mysql grabbing correct price depending on date

I have a table that looks like the following:
The query below obviously returns all records in the table
SELECT * FROM pricing
My problem I have is that I want to display the correct price based on todays date. I know I can grab todays date using CURDATE() but how do I grab the row that shows the price of 70.00 as this is the correct price until today is equal to or greater than 2017-02-01?
Thanks in advance.
John
select price from table where product = YourProduct and date <= YourDate order by date desc limit 1

mysql query dynamic date range

I've created a website showing the prices of the fruits and vegetables of my province. These prices are updated every day (aprox. +250 rows each day).
For that reason I created a MySql table to speed up the access to this information.
On the table showed on my Web site I want to limit the date showed up to 1 week and not all the dates available on the database. Taking in consideration that the database is increased daily I need a query for this dynamic date request.
They query I'm using right now is:
SELECT base_beta.Tipo,
base_beta.fecha_numero,
base_beta.Variedad,
base_beta.Fecha,
base_beta.alhondiga,
base_beta.corte_uno,
base_beta.corte_dos,
base_beta.corte_tres,
base_beta.corte_cuatro,
base_beta.corte_cinco,
base_beta.corte_seis,
base_beta.corte_siete,
base_beta.corte_ocho,
base_beta.corte_nueve,
base_beta.corte_diez,
base_beta.corte_once,
base_beta.corte_doce,
base_beta.corte_trece,
base_beta.corte_catorce,
base_beta.corte_quince
FROM base_beta
WHERE 1=1
AND base_beta.Tipo = 'pi'
ORDER BY base_beta.fecha_numero DESC
There are serveral columns with corte (aka prices) because every item has a random number of prices, since the fruits and vegetables has a bid system where prices decrease on every round.
For example, a tomato may start at 0,80€ and then its prices decrease to 0.76, 0.74, 0.69 and so on.
What code do I have to add to show the data of the current day and the next 6 days to complete a week?
Here are a photo of the: database content and format
Thanks, Jose.
If this defines your table schema
CREATE TABLE prices (item INT UNSIGNED, price DECIMAL(5,2), date DATETIME);
the following query will retrieve prices up to 7 days back in time
SELECT * FROM prices WHERE date > SUBDATE(CURDATE(),7) AND date <= CURDATE();
See also https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Hope this helps.

How to get the average price for the X most recent rows based on date?

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.

Getting the average number of orders per day using mysql

I have the following table structure:
ID, User_ID, DateTime
Which stores a user id and datetime of an order purchased. How would I get the average number of orders a day, across every row?
In pseudo code I'm thinking:
Get total number of orders
Get number of days in range (from first row to last row).
Divide 1. by 2. to get average?
So it would return me a value of 50, or 100?
Thanks
Since you know the date range, and you are not guaranteed to have and order on these dates, you can't just subtract the max(date) from min(date), but you know the number of days before you run the query, therefore simply:
select count(*) / <days>
from mytable
where DateTime between <start> and <end>
Where you supply the indicated values because you know them.
select DATEDIFF(NOW(), date_time) as days, AVG(count(*))
from table
group by days
I have not tested the query, its just the idea, I guess it should work.