I have a scenario, there is an escalator with 1000 lbs capacity. The total no of persons weight entering the escalator should not exceed 1000 lbs .LINE table contains persons name,weight and turn in the queue.
Below is the table syntax and values in it
create table line (id int not null PRIMARY KEY,
name varchar(255) not null,
weight int not null,
turn int unique not null,
check (weight > 0)
);
INSERT INTO LINE VALUES(6,'George Washington', 250, 1);
INSERT INTO LINE VALUES(5,'Thomas Jefferson',175, 7);
INSERT INTO LINE VALUES(3,'John Adams',350, 2);
INSERT INTO LINE VALUES(7,'Thomas Jefferson',800, 3);
INSERT INTO LINE VALUES(1,'James Elephant',500, 6);
INSERT INTO LINE VALUES(2,'Andy',200, 5);
INSERT INTO LINE VALUES(4,'Will Smith',400, 4);
Now i need to write a query to print last person name who enters the lift .Means, with the last person, lift capacity will be filled. Priority should be given based on TURN value i.e 1st person should be given 1st priority and 2nd person 2nd priority and so on. Sum of first 2 persons weight in turn is 600 ,if next person(Thomas Jefferson,Weight-800) enters the escalator,it exceeds escalator capacity .So, this person should be ignored/excluded and add next person(Will Smith,Weight-400) to the escalator .Now ,the sum of all persons weight is 1000 ,there by ,Will Smith name should be displayed in the output.
Could you please guide me to write SQL query for this.
PS:This is my first post. Kindly ignore in case of errors.