How do you put the last line on the SQL request?



  • There are two tables, the first Phones:

    Ph_IDPhone_Num
    12000

    and second Sales:

    Sale_IDPh_IDModel_IDDateOfSalePrice
    1131/07/202045000
    2353/07/202057000
    31323/07/202044000
    4262/08/202085000

    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
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2