How do you put the last line on the SQL request?
-
There are two tables, the first Phones:
Ph_ID Phone_Num 1 2000 and second Sales:
Sale_ID Ph_ID Model_ID DateOfSale Price 1 1 3 1/07/2020 45000 2 3 5 3/07/2020 57000 3 1 3 23/07/2020 44000 4 2 6 2/08/2020 85000 Telephone numbers (Phone_Num) sold from 01.07.2020 to 30.07.2020 and last (in this range) sales should be removed.
I did a code for the withdrawal of all Phone_Num during this period, but how do you get the value of the last sale?
select p.phone_num, up.dateofsales from sales up join phones p on up.ph_id = p.ph_id where up.dt >= to_date('01.07.2020' ,'DD/MM/YY') and up.dt <= to_date('30.07.2020' ,'DD/MM/YY') order by 2
-
That's it. https://dbfiddle.uk/?rdbms=oracle_18&fiddle=1527f4b26971cabab58d7f6b6888ec3e
select p.phone_num, s.dateofsale, price, first_value (price) over (partition by p.phone_num order by s.dateofsale desc) lastprice from sales s join phones p on p.ph_id = s.ph_id where s.dateofsale between date'2020-07-01' and date'2020-07-30' order by 2 /
PHONE_NUM DATEOFSALE PRICE LASTPRICE
2000 2020-07-01 00:00:00 45000 44000 2000 2020-07-23 00:00:00 44000 44000