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

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;

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!

How to create calculation in MySQL between date and price

I'm want to calculate the date to count days and calculate with price or custom price in MySQL.
This is booking table
CREATE TABLE tbl_booking(
booking_id int(11) NOT NULL AUTO_INCREMENT,
user_id int(11),
comp_id int(11),
register_number varchar(100),
booking_status int(11),
pick_date date,
drop_date date,)
This is vehicle table
CREATE TABLE tbl_vehicle(
register_number varchar(100) NOT null,
vehicle_model varchar(255),
vehicle_type varchar(255),
vehicle_price varchar(10));
So, like this:
totalprice = (pick_date - drop_date) * vehicle_price
Join two tables on common column which seems to be register_number then use datediff to calculate the difference in days between two dates and multiply it by price to get total price for each booking:
select
b.*,
datediff(b.drop_date, b.pick_date) * v.price as total_price
from tbl_booking b
inner join tbl_vehicle v on
b.register_number = v.register_number
Remember that you should substract later date from an earlier one (drop - pick) instead of (pick - drop) since this will give you a negative number.
Use below :
DATEDIFF(pick_date , drop_date) * price
DATEDIFF will give you day different between two dates.
SELECT DATEDIFF("2017-06-25", "2017-06-15");
Output will be : 10
As you price is in another table hope you will be able to join and calculate.
You can convert the date field data into days, then subtract them, and multiply with price. Please note that the syntax can vary among the different implementations of SQL. With the correct MySQL syntax this would be:
SELECT (TO_DAYS(drop_date) - TO_DAYS(pick_date)) * price FROM tbl_booking;
Also note that according to your code, tbl_booking does not contain the price field, so you'll probably need to join with another table as well.

Retrieve one out of every n records

I have a table containing thousands of records representing the temperature of a room in a certain moment. Up to now I have been rendering a client side graph of the temperature with JQuery. However, as the amount of records increases, I think it makes no sense to provide so much data to the view, if it is not going to be able to represent them all in a single graph.
I would like to know if there exists a single MySQL query that returns one out of every n records in the table. If so, I think I could get a representative sample of the temperatures measured during a certain lapse of time.
Any ideas? Thanks in advance.
Edit: add table structure.
CREATE TABLE IF NOT EXISTS `temperature` (
`nid` int(10) unsigned NOT NULL COMMENT 'Node identifier',
`temperature` float unsigned NOT NULL COMMENT 'Temperature in Celsius degrees',
`timestamp` int(10) unsigned NOT NULL COMMENT 'Unix timestamp of the temperature record',
PRIMARY KEY (`nid`,`timestamp`)
)
You could do this, where the subquery is your query, and you add a row number to it:
SET #rows=0;
SELECT * from(
SELECT #rows:=#rows+1 AS rowNumber,nid,temperature,`timestamp`
FROM temperature
) yourQuery
WHERE MOD(rowNumber, 5)=0
The mod would choose every 5th row: The 5 here is your n. so 5th row, then 10th, 15th etc.
Not really sure what your asking but you have multiple options
You can limit your results to n (n representing the amount of temperatures you want to display)
just a simple query with the limit in the end:
select * from tablename limit 1000
You could use a time/date restraint so you display only the results of the last n days.
Here is an example that uses date functions. The following query selects all rows with a date_col value from within the last 30 days:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
You could select an average temperature of a certain period, the shorter the period the more results you'll get. You can group by date, yearweek, month etc. to "create the periods"

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.