Need to fetch ID with min value and tendertype.
Below is the query:
drop table if exists #temp
create table #temp (ID int,TenderType nvarchar(100),BidAmount int,isAwardCarrier bit)
Insert into #temp (ID , TenderType, BidAmount, isAwardCarrier)
VALUES
(1, 'Spot Bid', NULL, 0),
(1, 'Spot Bid', NULL, 0),
(1, 'Spot Bid', NULL, 0),
(1, 'Spot Bid', NULL, 0),
(1, 'Spot Bid', 390, 0),
(1, 'Spot Bid', 345, 1),
(1, 'Spot Bid', NULL, 0),
(1, 'Spot Bid', 653.35, 0),
(1, 'Ordinary', NULL, 1),
(2, 'Spot Bid', NULL, 0),
(2, 'Spot Bid', NULL, 0),
(2, 'Spot Bid', NULL, 0),
(2, 'Spot Bid', NULL, 0),
(2, 'Spot Bid', 4150, 1),
(2, 'Spot Bid', 3365, 0),
(2, 'Spot Bid', NULL, 0),
(2, 'Spot Bid', 3607.42, 0),
(2, 'Ordinary', NULL, 1);
select * from #temp
Expected Output
ID TenderType BidAmount IsAwardCarrier BidSuccess
1 Spot Bid NULL 0 0
1 Spot Bid NULL 0 0
1 Spot Bid NULL 0 0
1 Spot Bid NULL 0 0
1 Spot Bid 390 0 0
1 Spot Bid 345 1 1
1 Spot Bid NULL 0 0
1 Spot Bid 653 0 0
1 Ordinary NULL 1 1
2 Spot Bid NULL 0 0
2 Spot Bid NULL 0 0
2 Spot Bid NULL 0 0
2 Spot Bid NULL 0 0
2 Spot Bid 4150 1 0
2 Spot Bid 3365 0 1
2 Spot Bid NULL 0 0
2 Spot Bid 3607 0 0
2 Ordinary NULL 1 0
Logic to calculate BidSuccess:
If the BidAmount is Minimum for the ID and IsAwarCarrier is 1 and for the same ID tenderType = Ordinary and IsAwardCarrier is 1 Then BidSuccess=1 for both the rows (Eg: ID=1)
If the BidAmount is Minimum and IsAwardCarrir is 0 then BidSuccess = 1 for that row. (Eg: ID =2)
Can someone please help with the code.
You can use window functions for this
db<>fiddle
SQL Server 2008 doesn't have window functions, but if you are still on this version I strongly suggest you upgrade, because it's now completely out of date, insecure and unsupported.