Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Top 2 Angebote mit Summe aller Angebote

 with x as 
 (select row_number() over(partition by customer,make order by offer desc) rn,
  customer, make, zipcode, offer from tablename)
 , y as (select customer, make, zipcode, offer from x where rn <=2)
 , z as (select customer, make, zipcode, 
         case when rn = 1 then offer else 0 end as offer_1, 
         case when rn = 2 then offer else 0 end as offer_2 
         from y)
  select customer, make, zipcode, offer_1, offer_2, offer_1+offer_2 total_offer
  from z

Dies nutzt rekursive CTEs, um Ihre Aufgabe zu erfüllen.