I have a data table
SQL Fiddle
http://sqlfiddle.com/#!18/b33c86
Schema
create table lenderdata
(
ID int identity
primary key,
LinkID varchar(250) null,
Lender varchar(250) null,
Item varchar(250) null,
Priority int null,
Quantity int null,
Status varchar(250) null
);
INSERT INTO lenderdata (LinkID, Lender, Item, Priority, Quantity, Status) VALUES ('001', 'A', 'Apple', 1, 100, 'PENDING');
INSERT INTO lenderdata (LinkID, Lender, Item, Priority, Quantity, Status) VALUES ('001', 'B', 'Orange', 2, 100, 'PENDING');
INSERT INTO lenderdata (LinkID, Lender, Item, Priority, Quantity, Status) VALUES ('002', 'C', 'Strawberry', 1, 1000, 'PENDING');
INSERT INTO lenderdata (LinkID, Lender, Item, Priority, Quantity, Status) VALUES ('002', 'D', 'grapes', 2, 100, 'PENDING');
INSERT INTO lenderdata (LinkID, Lender, Item, Priority, Quantity, Status) VALUES ('003', 'E', 'coffee', 1, 1000, 'PROCESSING');
INSERT INTO lenderdata (LinkID, Lender, Item, Priority, Quantity, Status) VALUES ('003', 'F', 'mango', 2, 1000, 'PENDING');
I want to group by Link ID and pick up only the one with minimum of priority if the status is PENDING If the group by data has other status e.g. PROCESSING then it should simply ignore that group Only if all the status is either PENDING excluding the group if there is one with PROCESSING status
If I was to run the query it would only return Link ID 001 and 002