clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2805502 fiddles created (40750 in the last week).

CREATE TABLE item (`item_symbol` varchar(1), `price` DECIMAL(10,2), `source_date` Date) ; INSERT INTO item (`item_symbol`, `price`, `source_date`) VALUES ('A', 20.1, '2021-06-10'), ('A', 18.2, '2021-06-11'), ('A', 10.9, '2021-06-13'), ('A', 21.0, '2021-06-15'), ('B', 88.2, '2021-06-10'), ('B', 60.9, '2021-06-11'), ('B', 78.16, '2021-06-13'), ('B', 79.0, '2021-06-15') ;
 hidden batch(es)


SELECT i.`item_symbol`, `price` FROm item i INNER JOIN (SELECT `item_symbol`,MAX(`source_date`) maxdate FROM item GROUP By `item_symbol`) t1 ON i.`item_symbol` = t1.`item_symbol` AND i.`source_date` = t1.maxdate
item_symbol price
A 21.00
B 79.00
 hidden batch(es)


SELECT `item_symbol`, `price`,rn FROM(SELECT `price` ,`source_date` , IF(@id = `item_symbol`,@rn:= @rn +1,@rn := 1) As rn ,@id := `item_symbol` as item_symbol FROM item , (SELECT @rn := 0, @id := 0) t1 ORDER BY `item_symbol`,`source_date` DESC) t2 WHERE rn <= 2
item_symbol price rn
A 21.00 1
A 10.90 2
B 79.00 1
B 78.16 2
 hidden batch(es)