Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
CREATE TABLE Price ( IDBrewery int, ProductCode int, ProductPrice float, PeriodID int ); CREATE TABLE Period ( PeriodID int, PeriodDate date ); INSERT INTO Price VALUES (1, 1001, 123.45, 1), (1, 1001, 156.78, 2), (2, 1002, 23.99, 1), (2, 1002, 24.78, 2); INSERT INTO Period Values (1, '2020-12-01'), (2, '2021-01-01'); SELECT * FROM ( SELECT IDBrewery, ProductCode, ProductPrice, PeriodDate, ROW_NUMBER() OVER (PARTITION BY ProductCode ORDER BY PeriodDate DESC) RN FROM Price JOIN Period ON Period.PeriodID = Price.PeriodID ) LastPrice WHERE RN = 1 ;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear