无法在SQL Server中执行分组依据

时间:2019-12-06 04:58:46

标签: sql sql-server join group-by sql-order-by

尝试过滤详细信息

需要唯一具有BookingVersion组最多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

任何人都可以解释一下我所缺少的吗?

当前结果:

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

预期结果:

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

3 个答案:

答案 0 :(得分:1)

请尝试:

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

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

答案 1 :(得分:1)

我建议使用窗口函数来查找所需的特定记录。喜欢:

;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

答案 2 :(得分:1)

请按照提及的内容检查此查询,例如,我准备好您的预期输出。

注意:请根据您的实际查询更改此查询。

  

查询

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
  

输出

enter image description here