SQL Query Group by issue

时间:2017-02-08 07:06:00

标签: sql-server group-by

我有一张包含历史记录表的记录表。

我创建了以下Query以获得所需的结果。但是,它并没有给我我想要的最终结果。

DECLARE @Customer_Id BIGINT=4
DECLARE @Month INT=2

SELECT 
    FH.UniqueFileId,FH.DocumentNo,MIN(FH.DateStamp) AS ReceivedTime,MAX(FH.DateStamp) SentTime
FROM 
    [dbo].[FileMovement_History] FH 
    INNER JOIN [dbo].[User_Master] UM ON UM.User_Id=FH.User_Id
WHERE 
    FH.UniqueFileId Is Not NULL AND FH.UniqueFileId!=''
    AND (@Customer_Id IS NULL OR UM.Customer_id = @Customer_Id)
    AND MONTH(FH.DateStamp)=@Month  
GROUP BY 
    FH.UniqueFileId,FH.DocumentNo
Order BY 
    FH.UniqueFileId

╔════════════════╦══════════════════════╦═════════════════════════════╦═════════════════════════════╗
║  UniqueFileId  ║      DocumentNo      ║        ReceivedTime         ║          SentTime           ║
╠════════════════╬══════════════════════╬═════════════════════════════╬═════════════════════════════╣
║ HS5536387_10HE ║ NULL                 ║ 2017-02-02 13:13:37.3451906 ║ 2017-02-02 13:19:19.0114047 ║
║ HS5536387_10HE ║ 120097583 / MC / CL7 ║ 2017-02-02 13:19:18.9801503 ║ 2017-02-02 13:19:19.1051605 ║
║ HS5536387_1HE  ║ NULL                 ║ 2017-02-02 13:13:34.6887572 ║ 2017-02-02 13:21:37.7405652 ║
║ HS5536387_1HE  ║ 3701341              ║ 2017-02-02 13:21:37.6936641 ║ 2017-02-02 13:21:37.8499434 ║
║ HS5536387_3HE  ║ NULL                 ║ 2017-02-02 13:13:35.5950873 ║ 2017-02-02 13:19:19.9958519 ║
╚════════════════╩══════════════════════╩═════════════════════════════╩═════════════════════════════╝

我想要以下结果,其中我需要一个条目用于一个UniqueFileId,其中datestamp和datestamp的最小值最小。我想要额外的列,其中显示最小和最大日期之间的总时间。

╔════════════════╦══════════════════════╦═════════════════════════════╦═════════════════════════════╗
║  UniqueFileId  ║      DocumentNo      ║        ReceivedTime         ║          SentTime           ║
╠════════════════╬══════════════════════╬═════════════════════════════╬═════════════════════════════╣
║ HS5536387_10HE ║ 120097583 / MC / CL7 ║ 2017-02-02 13:13:37.3451906 ║ 2017-02-02 13:19:19.0114047 ║
║ HS5536387_1HE  ║ 3701341              ║ 2017-02-02 13:13:34.6887572 ║ 2017-02-02 13:21:37.7405652 ║
║ HS5536387_3HE  ║ NULL                 ║ 2017-02-02 13:13:35.5950873 ║ 2017-02-02 13:19:19.9958519 ║
╚════════════════╩══════════════════════╩═════════════════════════════╩═════════════════════════════╝

1 个答案:

答案 0 :(得分:1)

仅按UniqueFileId字段进行分组,而是取此列的MIN(DocumentNo)值:

SELECT FH.UniqueFileId,
       MIN(FH.DocumentNo) AS DocumentNo,
       MIN(FH.DateStamp)  AS ReceivedTime,
       MAX(FH.DateStamp)  AS SentTime
FROM [dbo].[FileMovement_History] FH 
INNER JOIN [dbo].[User_Master] UM
    ON UM.User_Id = FH.User_Id
WHERE COALESCE(FH.UniqueFileId, '') <> '' AND
      (@Customer_Id IS NULL OR UM.Customer_id = @Customer_Id) AND
      MONTH(FH.DateStamp) = @Month  
GROUP BY FH.UniqueFileId
ORDER BY FH.UniqueFileId

如果您想以hh:mm:ss格式显示发送和接收时间之间的时差,则可以使用以下表达式:

CAST(DATEDIFF(HOUR,   MIN(FH.DocumentNo), MAX(FH.DocumentNo)) AS NVARCHAR(200)) + ":" +
CAST(DATEDIFF(MINUTE, MIN(FH.DocumentNo), MAX(FH.DocumentNo)) % 60 AS NVARCHAR(200)) +":" +
CAST(DATEDIFF(SECOND, MIN(FH.DocumentNo), MAX(FH.DocumentNo)) % 60 AS NVARCHAR(200))