I edited my question to make it more understanding. I have 3 tables
1. st_kalk
id_artikli|cena_neto|iddg
1066 |25000 |34323
808 |231933 |25234
718 |22999 |34244
718 |22965 |23212
2._artikli
id_artikli
1066
808
718
718
3.dok
iddg |datum
34323 |4/22/2022
25234 |2/16/2021
23212 |1/29/2022
34244 |2/2/2022
In st_kalk I have column id_artikli as well in the _artikli table, also st_kalk is related to dok by iddg as you can see in my query. I have to join these 3 tables in order to get the correct price ( because some of my products have multiple prices (cena_neto)) based on the latest date (datum). The query below works but only if I set id_artikla to specific one, but wont return every article with latest price and date, and that is what I am supposed to do. I am sorry if this is confusing, it's my first time writing here also I am still learning sql. Thank you
SELECT top 1
k.id_artikla,
k.maxdatum,
cena_neto
FROM ( SELECT id_artikla,
MAX(datum) AS maxdatum,
cena_neto
FROM st_kalk INNER JOIN dok ON st_kalk.iddg=dok.iddg
GROUP BY id_artikla,cena_neto) k
INNER JOIN _artikli ON k.id_artikla=k.id_artikla
WHERE k.id_artikla=718
ORDER BY k.maxdatum DESC
Thank you in advance for your help