i have this table below named **roombooking**:
[![enter image description here](https://i.stack.imgur.com/ihcTu.png)](https://i.stack.imgur.com/ihcTu.png)

I wrote this code that inserts a new row into roombooking(dont mind the details, just the **hotelbookingID**):

```
CREATE OR REPLACE FUNCTION my_function(startdate date , enddate date,idForHotel integer)
RETURNS void AS
$$
BEGIN

INSERT INTO roombooking("hotelbookingID","roomID","bookedforpersonID"
                                     ,checkin,checkout,rate)
                                SELECT rb."hotelbookingID", r."idRoom", p."idPerson"
                                ,startdate-integer'20', startdate-integer'10', rr.rate 
                                FROM(SELECT "hotelbookingID" FROM roombooking
                                    WHERE "hotelbookingID"=
                                    (select "hotelbookingID"
                                     from roombooking
                                    order by "hotelbookingID" desc 
                                    limit 1)+1) rb,
                                (SELECT "idRoom" FROM room 
                                WHERE "idHotel"=idForHotel) r ,
                                (SELECT "idPerson" FROM person
                                ORDER BY random()
                                LIMIT 1) p,
                                (SELECT rate FROM roomrate
                                WHERE "idHotel"=idForHotel) rr;

 END;
 $$
 LANGUAGE 'plpgsql';

```

The problem here is that i want to insert after the last row based on the last hotelbookingID(it is in asc order)

My function works but as i guess it cant find the last row ,in order to perform the insertion after . (I think that the problem can be spotted here :

```
SELECT "hotelbookingID" FROM roombooking
                                    WHERE "hotelbookingID"=
                                    (select "hotelbookingID"
                                     from roombooking
                                    order by "hotelbookingID" desc 
                                    limit 1)+1)

```

Any help would be valuable. Thank you.