MS Access - Add most recent price for each new purchase - ms-access

I work with this data in table Purchases:
Mat_ID Date Price
11 5.1.2018 10
11 7.1.2018 12
11 9.1.2018 14
12 5.1.2018 10
12 7.1.2018 12
13 9.1.2018 14
13 5.1.2018 10
My desired output query is to have another column with last purchase Price:
Mat_ID Date Price PrevPrice
11 5.1.2018 10 Null
11 7.1.2018 12 10
11 9.1.2018 14 12
12 5.1.2018 10 Null
12 7.1.2018 12 10
13 9.1.2018 14 Null
13 5.1.2018 10 14
Can you recommend something, please?
Thank you!

Try this:
Select
*,
(Select Top 1 Price
From YourTable As T
Where T.Mat_ID = YourTable.Mat_ID And T.[Date] < YourTable.[Date]
Order By T.Mat_ID, T.[Date] Desc) As PrevPrice
From
YourTable

Related

Mysq/Mariadb get newest entry for some ids in a IN Select group by id

Hello i have a table with some ids and values
for example:
SELECT instrumentid, value from `mytable` where instrumentid in (12,11, 14,15);
id, instrumentid, recorddate, value
33 12 2022-10-05 55
34 11 2022-10-05 33
30 14 2022-10-05 13
29 12 2022-10-03 12
28 11 2022-10-03 53
40 14 2022-10-03 4
44 15 2022-10-03 4
as result i want or better explained only the last newst entry for instrumentid
instrumentid, value
12 55
11 33
14 13
15 4
thanks and regards
running latest mariadb 10.9.3
You can use ROW_NUMBER() to identify the last row for each instrument.
For example:
select *
from (
select t.*,
row_number() over(partition by instrumentit order by recorddate desc) as rn
from mytable t
where instrumentid in (12,11, 14,15)
) x
where rn = 1

GROUP BY with MAX(date), but the date is in two separate columns

I'm trying to get the list of IDs of all the rows with latest posting date for each author in a table, for example:
id author_id date
1 12 2020-12-23
2 12 2021-01-06
3 12 2021-04-12
4 12 2021-02-10
5 17 2021-09-16
6 17 2021-05-20
7 17 2021-02-23
8 17 2021-07-02
9 24 2021-03-24
10 24 2021-02-10
11 24 2020-08-18
12 24 2020-12-14
The desired result should be:
id
3
5
9
I used this query and it works perfect:
SELECT a.id
FROM (
SELECT author_id, MAX(`date`) as MaxDate
FROM `posts_log`
GROUP BY author_id
) b
INNER JOIN `posts_log` a
ON a.author_id = b.author_id AND a.date = b.MaxDate
But let's imagine that situation changed. Now author is allowed to post only once per month. So the table changed too and 'date' column became separated:
id author_id month year
1 12 12 2020
2 12 1 2021
3 12 4 2021
4 12 2 2021
5 17 9 2021
6 17 5 2021
7 17 2 2021
8 17 7 2021
9 24 3 2021
10 24 2 2021
11 24 8 2020
12 24 12 2020
Yeah, I know, looks a little bit stupid, but this wasn't my decision. Now I have such table and I'm not allowed to change the structure.
The question is: How to get the same result with this new table. Is it possible in MySQL?
You can use similar logic. Just instead of the date, use a calculated value from year and month:
SELECT a.id
FROM (
SELECT p.author_id, MAX(p.year*100+p.month) as MaxMonth
FROM posts_log p
GROUP BY p.author_id
) b
INNER JOIN posts_log a
ON a.author_id = b.author_id AND a.year*100+a.month = b.MaxMonth;

SQL Aggregate Function Min with Group

I have a table named Prices. Which contain ID, fkProductID (foregnkey of Products Table), PriceDate, Price, fkStore (the supplier ID who gives the prices)
Now I need to find the best price for each product for each month and the SUPPLIER name also.
Also some suppliers are giving duplicates prices. For example product id 8 got two times 1200 (ID# 8 and 3). If possible i only need to show the first supplier or a column with count values..
ID fkProductID PriceDate Price fkStore
-----------------------------------------------------
1 8 26-10-2014 1250 13
2 8 10-09-2014 1200 13
3 8 25-10-2014 1200 1
4 8 13-10-2014 1500 1
5 8 03-09-2014 1000 1
6 8 15-09-2014 1300 15
7 8 09-09-2014 950 21
8 8 10-10-2014 1200 23
9 8 09-09-2014 950 27
10 15 10-10-2014 3500 5
11 15 11-10-2014 3400 6
12 15 09-09-2014 3100 6
13 15 10-09-2014 3200 14
14 15 16-09-2014 3100 17
-----------------------------------------------------
my expected result.
-----------------------------------------------------
ID fkProductID Month Price Supplier
-----------------------------------------------------
7 8 September 950 21
2 8 October 1200 1
13 15 September 3100 13
11 15 October 3400 6
=================================================================
SCHEMA
SQL FIDDLE
You can do what you want with the substring_index()/group_concat() trick:
select substring_index(group_concat(idPrices order by Price, idPrices), ',', 1) as id,
fkProductId, monthname(PriceDate) as mon,
min(Price) as price,
substring_index(group_concat(fkStore order by Price, idPrices), ',', 1) as spec_id
from prices p
group by fkProductId, monthname(PriceDate);

MySQL Order By 2 colums (date/frequency)

I have a favorite table with 4 columns
employee_id
product_id
frequency
last_consumed_date
Now i'm getting the 6 rows with the highest frequency for the employee_id with a minimal frequency of 6.
Example with employee_id 1
SELECT * FROM `favorites`
WHERE `employee_id` = 1
AND `frequency` >= 6
ORDER BY `frequency` DESC LIMIT 0,6
So far so good!
But now i want to prefer the rows if the last_consumed_date is within a month (30 days), So i must do something with:
DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= `lastchanged`
Here is a table example to make it more clear
Table filled:
1 5 11 2012-10- 3
1 13 8 2012-11- 7
1 18 20 2012- 9-25
1 42 10 2012-11- 3
1 28 15 2012-10-17
1 9 7 2012-10- 8
1 64 9 2012-11- 1
2 24 8 2012- 8-28
2 12 5 2012-10-16
2 5 12 2012-11-11
Today is 2012-11- 8
30 days back is 2012-10- 9
Table returned after SQL:
1 28 15 2012-10-17 <Sorted by 30 days interval and frequency>
1 42 10 2012-11- 3
1 64 9 2012-11- 1
1 13 8 2012-11- 7
1 18 20 2012- 9-25 <Sorted by frequency>
1 5 11 2012-10- 3
Now the question is, How do i order those 2 things in 1 query?
First an order by the date (with 30 days interval)
and than an order by the frequency of seperated results (inside interval and all others)
Ohh i think i found my answer by trial-error ^v^
SELECT * FROM `favorites`
WHERE `employee_id` = 1
AND `frequency` >= 6
ORDER BY (DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= `lastchanged`) DESC,
`frequency` DESC
LIMIT 0,6
For those who tried helping! Thank you

SQL Count Average

I have table like
id userid semid courseid coursename total
1 36 17 13 CA 23
2 36 17 5 CB 46
3 36 17 8 CC 20
4 36 19 16 CD 34
5 36 19 13 CA 31
6 36 19 3 CA# 29
7 36 19 7 CE 60
8 36 10 9 CK 32
9 36 10 15 CH 56
I need average of semid for a userid i.e., SUM(courseid) /count (moduleid), It was showing 9 as module count, but I have only 3 modules.
This is my query
SELECT userid, SUM(total)/count(semid) FROM custom WHERE userid=36
just use the AVG( ) function
SELECT userid, semid, AVG(total)
FROM custom
WHERE userid = 36
GROUP BY userid, semid
SQLFiddle Demo
SELECT userid, SUM(total)/count(distinct semid) FROM custom WHERE userid=36
Try this query
There is MYSQL aggregate function AVG() for finding Average . #John Totet Woo has posted the answer.