Mysql grabbing correct price depending on date - mysql

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

Related

How to calculate base 100 on MYSQL select query

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!

SQL: How do I search the data with the LATEST date AND the MOST value in quantity_used?

I know how to search the LATEST date and the MOST value specifically:
Most Quantity Used:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY quantity_used DESC
Latest Date:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY start_date DESC
But how can I do both, by able to search the LATEST date WITH the MOST value in quantity_used? Is this practice even possible?
EDITED: I think my question is not clear enough.
I intend to find the data with the LATEST date first, then from that result FIND the highest VALUE from quantity_used.
I think you just want two order by keys:
SELECT tp.*
FROM tour_packages tp
WHERE tp.active = 1
ORDER BY tp.start_date DESC, tp.quantity_used DESC;
This returns the rows ordered by date and within each date, the ones with the largest quantity go first.

How to sum order total amount by week

I am trying to retrieve last 3 months records. I need to sum order total amount by week. I have made following query.
select CONCAT("Week", WEEK(created_at)) as week_number, sum(total_cost)
from certified_diamond_orders
where created_at > 2016-11-22
and status = "delivered"
group by week("created_at")
But I am only getting one record with this. Infact my table has 2 years entries. Also I was trying to figure out how I can pull week start date and end date to diplay on my chart.
Any suggestions where I am making mistake?
week("created_at") looks like you're trying to determine the week of the string "created_at" rather than the column created_at. Which might explain why you're only getting one row in your result.
The date 2016-11-22 also looks suspiciously like a sum instead of a date (2016 - 11 - 22 = 1983 vs "2016-11-22"
Try this:
SELECT
CONCAT('Week', WEEK(created_at)) AS week_number,
SUM(total_cost)
FROM certified_diamond_orders
WHERE
created_at > '2016-11-22' AND
status = 'delivered'
GROUP BY WEEK(created_at)

MYSQL query and compare values if multiple rows found

Im not too mysql savvy and i'm having issues creating a select that fits with my needs.
I have a database table that looks similar to this:
registrar
id balance date
---------------------
1 500.00 2013-01-01
2 402.00 2013-01-01
3 396.00 2013-01-02
4 394.00 2013-01-02
I have a query that I use to pull the data into a script and display the data:
SELECT balance, date FROM $registrar WHERE date BETWEEN '$starting_date' AND '$ending_date'
However, it appears that when querying, mysql is only returning the newest entry for that date. Id like if possible to return the lower balance amount of that date if found multiple rows matching the date criteria.
try ORDER BY
SELECT balance, date FROM $registrar WHERE date BETWEEN '$starting_date' AND '$ending_date' ORDER BY balance DESC
You have to use order by class in the end of query
SELECT balance, date FROM $registrar WHERE date BETWEEN '$starting_date' AND '$ending_date' ORDER BY balance DESC
By default it id , so u have to put balance .

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.