显示一个月的最后一次训练日期

问题描述 投票:0回答:1

我正在使用新的报告系统。我正在创建一份报告,以按类别显示每月完成的培训。我已经得到了报告,我得到了我的参与者和日期,但我只能让它计算每月的小时数。有没有办法只显示一个月内上课的最后一天?我假设它是 SUM,但我似乎无法找到正确的语法来仅将月份中的某一天显示为值。

enter image description here

SELECT
    "t4"."Attendee_Name__Last__First_Middle_Suffix_" AS "Attendee_Name__Last__First_Middle_Suffix__res",
    "t4"."datetime_months_Class_Sessions_Session_Start_Date" AS "datetime_months_Class_Sessions_Session_Start_Date_res",
    SUM("t4"."Hours") AS "sum__Hours_res"
FROM
    (SELECT
         "t3"."Attendee_Name__Last__First_Middle_Suffix_",
         DATE_TRUNC('MONTH', "t2"."Session_Start_Date") AS "datetime_months_Class_Sessions_Session_Start_Date",
         "t0"."Hours"
     FROM
         (SELECT
              "_CLASSSESSIONID_PART1",
              "_CLASSSESSIONID_PART2",
              "Hours",
              "Credit Name" AS "Credit_Name"
          FROM
              "PM_CLASSES"."Class Session Credits"
          WHERE
              "Credit Name" = 'FAR 139 CATEGORY 1 - AIRPORT FAMILIARIZATION') AS "t0"
     INNER JOIN 
         ((SELECT
               "_CLASSSESSIONID_PART1",
               "_CLASSSESSIONID_PART2",
               "Session Start Date" AS "Session_Start_Date"
           FROM
               "PM_CLASSES"."Class Sessions"
           WHERE
               "Session Start Date" >= '2023-04-10 00:00:00'
               AND "Session Start Date" < '2024-04-04 00:00:00') AS "t2" 
     INNER JOIN 
         (SELECT
              "_CLASSSESSIONID_PART1",
              "_CLASSSESSIONID_PART2",
              "Attendee Name (Last, First Middle Suffix)" AS "Attendee_Name__Last__First_Middle_Suffix_"
          FROM
              "PM_CLASSES"."Class Session Attendees") AS "t3" ON "t2"."_CLASSSESSIONID_PART1" = "t3"."_CLASSSESSIONID_PART1"
        AND "t2"."_CLASSSESSIONID_PART2" = "t3"."_CLASSSESSIONID_PART2"
      ) ON "t0"."_CLASSSESSIONID_PART1" = "t2"."_CLASSSESSIONID_PART1"
      AND "t0"."_CLASSSESSIONID_PART2" = "t2"."_CLASSSESSIONID_PART2"
    ) AS "t4"
GROUP BY
    "t4"."Attendee_Name__Last__First_Middle_Suffix_",
    "t4"."datetime_months_Class_Sessions_Session_Start_Date"
ORDER BY
    "t4"."Attendee_Name__Last__First_Middle_Suffix_",    
    "t4"."datetime_months_Class_Sessions_Session_Start_Date"

到目前为止,我已经多次破坏报告,但成功地给了我每月的总小时数。我浏览了其他帖子,但似乎找不到合适的解决方案。

sql date datetime
1个回答
0
投票

我假设它是SUM

这是不正确的,如果您想要某个字段的 last,那么您的常规选项是

MAX
/
MIN
或对集合进行排序并获取第一个响应。

在这种情况下,您已经按月分组,因此我们应该能够在组中获取“MAX(Session_Start_Date)”,但您需要公开它以使其可用:

SELECT
    "t4"."Attendee_Name__Last__First_Middle_Suffix_" AS "Attendee_Name__Last__First_Middle_Suffix__res",
    "t4"."datetime_months_Class_Sessions_Session_Start_Date" AS "datetime_months_Class_Sessions_Session_Start_Date_res",
    SUM("t4"."Hours") AS "sum__Hours_res",
    MAX("t4"."Session_Start_Date") as "Last_Session_Start_Date"
FROM
    (SELECT
         "t3"."Attendee_Name__Last__First_Middle_Suffix_",
         DATE_TRUNC('MONTH', "t2"."Session_Start_Date") AS "datetime_months_Class_Sessions_Session_Start_Date",
         "t0"."Hours",
         "t2"."Session_Start_Date"
     FROM
         (SELECT
              "_CLASSSESSIONID_PART1",
              "_CLASSSESSIONID_PART2",
              "Hours",
              "Credit Name" AS "Credit_Name"
          FROM
              "PM_CLASSES"."Class Session Credits"
          WHERE
              "Credit Name" = 'FAR 139 CATEGORY 1 - AIRPORT FAMILIARIZATION') AS "t0"
     INNER JOIN 
         ((SELECT
               "_CLASSSESSIONID_PART1",
               "_CLASSSESSIONID_PART2",
               "Session Start Date" AS "Session_Start_Date"
           FROM
               "PM_CLASSES"."Class Sessions"
           WHERE
               "Session Start Date" >= '2023-04-10 00:00:00'
               AND "Session Start Date" < '2024-04-04 00:00:00') AS "t2" 
     INNER JOIN 
         (SELECT
              "_CLASSSESSIONID_PART1",
              "_CLASSSESSIONID_PART2",
              "Attendee Name (Last, First Middle Suffix)" AS "Attendee_Name__Last__First_Middle_Suffix_"
          FROM
              "PM_CLASSES"."Class Session Attendees") AS "t3" ON "t2"."_CLASSSESSIONID_PART1" = "t3"."_CLASSSESSIONID_PART1"
        AND "t2"."_CLASSSESSIONID_PART2" = "t3"."_CLASSSESSIONID_PART2"
      ) ON "t0"."_CLASSSESSIONID_PART1" = "t2"."_CLASSSESSIONID_PART1"
      AND "t0"."_CLASSSESSIONID_PART2" = "t2"."_CLASSSESSIONID_PART2"
    ) AS "t4"
GROUP BY
    "t4"."Attendee_Name__Last__First_Middle_Suffix_",
    "t4"."datetime_months_Class_Sessions_Session_Start_Date"
ORDER BY
    "t4"."Attendee_Name__Last__First_Middle_Suffix_",    
    "t4"."datetime_months_Class_Sessions_Session_Start_Date"
  • 快速说明:为子表使用任意别名会使此逻辑难以解释,请考虑使用有意义的名称。

  • 我故意不在 SQL 中格式化日期时间值,理想情况下这将在稍后的报告或报告定义中实现,而不是 SQL 本身。

© www.soinside.com 2019 - 2024. All rights reserved.