Reputation: 1527
Trying to filter the details
Need the only row which has max BookingVersion
group by shipmentNumber, shipmentDate,
select max(BookingVersion) BookingVersion, flightId, shipmentNumber, shipmentDate, FlightOffPoint, IIF(BookingStatusCode = 'XX', 'XX','SS') BookingStatusCode
from Exp_BookingDelta where flightid='625436'
group by flightId, shipmentNumber, shipmentDate, FlightOffPoint, BookingStatusCode
order by max(BookingVersion) desc
Can anyone please explain, what I'm missing?
Current results:
BookingVersion flightId shipmentNumber shipmentDate FlightOffPoint BookingStatusCode
4 625436 61823647238 2019-12-04 LHR XX
2 625436 61823647238 2019-12-04 LHR SS
1 625436 61826374895 2019-12-06 LHR XX
0 625436 61825364780 2019-11-26 LHR SS
0 625436 61825364791 2019-11-26 LHR SS
0 625436 61825364802 2019-11-26 LHR SS
0 625436 61826374895 2019-12-06 LHR SS
Expecting results:
BookingVersion flightId shipmentNumber shipmentDate FlightOffPoint BookingStatusCode
4 625436 61823647238 2019-12-04 LHR XX
1 625436 61826374895 2019-12-06 LHR XX
0 625436 61825364780 2019-11-26 LHR SS
0 625436 61825364791 2019-11-26 LHR SS
0 625436 61825364802 2019-11-26 LHR SS
Upvotes: 0
Views: 473
Reputation: 1883
Please check this query as per mention your expected output I'm prepared for example.
Note: Please change this query as per your actual query.
Query
DECLARE @Table TABLE
(
BookingVersion INT,
flightId INT,
shipmentNumber VARCHAR(20),
shipmentDate DATE,
FlightOffPoint VARCHAR(10),
BookingStatusCode VARCHAR(10)
)
INSERT INTO @Table
VALUES(
4 , 625436 , '61823647238', '2019-12-04', 'LHR','XX'),
(2, 625436, ' 61823647238', '2019-12-04', 'LHR','SS'),
(1, 625436, ' 61826374895', '2019-12-06', 'LHR','XX'),
(0, 625436, ' 61825364780', '2019-11-26', 'LHR','SS'),
(0, 625436, ' 61825364791', '2019-11-26', 'LHR','SS'),
(0, 625436, ' 61825364802', '2019-11-26', 'LHR','SS'),
(0, 625436, ' 61826374895', '2019-12-06', 'LHR','SS')
SELECT MAX(BookingVersion) AS BookingVersion,
flightId,
shipmentNumber,
shipmentDate,
FlightOffPoint,
MAX(BookingStatusCode) AS BookingStatusCode
FROM @Table
GROUP BY flightId, shipmentNumber, shipmentDate, FlightOffPoint
ORDER BY MAX(BookingVersion) DESC
Output
Upvotes: 1
Reputation: 18629
Please try:
select
max(BookingVersion) BookingVersion, flightId, shipmentNumber, shipmentDate, FlightOffPoint, MAX(BookingStatusCode) BookingStatusCode
from
Exp_BookingDelta where flightid='625436'
group by flightId, shipmentNumber, shipmentDate, FlightOffPoint
order by max(BookingVersion) desc
or
select
max(BookingVersion) BookingVersion, flightId, shipmentNumber, shipmentDate,
IIF((select count(*) from Exp_BookingDelta b where b.FlightId=a.FlightId and b.ShimpmentNumber=a.ShimpmentNumber)>0, 'XX', 'SS') BookingStatusCode
from
Exp_BookingDelta where flightid='625436'
group by flightId, shipmentNumber, shipmentDate, FlightOffPoint
order by max(BookingVersion) desc
Upvotes: 1
Reputation: 3130
I would suggest using a window function to find the specific records you want. Like:
;with c as (select BookingVersion, flightId, shipmentNumber, shipmentDate, FlightOffPoint, IIF(BookingStatusCode = 'XX', 'XX','SS') BookingStatusCode
, row_number() over (partition by shipmentNumber order by BookingVersion desc) rn
from Exp_BookingDelta
where flightid='625436')
select BookingVersion, flightId, shipmentNumber, shipmentDate, FlightOffPoint, BookingStatusCode
from c
where rn = 1
Upvotes: 1