重复的行返回了工作日志条目。我只需要最新的条目。

huangapple go评论119阅读模式
英文:

Duplicate rows returned with worklog entries. I just need the latest entry

问题

我创建了这个查询,它可以正常工作。问题在于它显示了每个工作日志的行。我只需要每个事件的最新工作日志。

SELECT
    `HPD:Help Desk`.`Incident Number` AS Incident_Number,
    `HPD:Help Desk`.`Status` AS Status,
    `HPD:Help Desk`.`Priority` AS Priority,
    `HPD:Help Desk`.`Assigned Group` AS Assigned_Group,
    `HPD:Help Desk`.`Assignee` AS Assignee,
    `HPD:Help Desk`.`Submit Date` AS Submit_Date,
    `HPD:Help Desk`.`Last Modified Date` AS Last_Modified_Date,
    `HPD:Help Desk`.`Description` AS Description,
    `HPD:Help Desk`.`Submitter` AS Submitter,
    `HPD:Search-Worklog`.`Worklog Description` AS Worklog_Description,
    MAX(`HPD:Search-Worklog`.`Work Log ID`) AS Work_Log_ID
FROM
    `HPD:Help Desk`
INNER JOIN `HPD:Search-Worklog` ON (`HPD:Search-Worklog`.`InstanceId` = `HPD:Help Desk`.`InstanceId`)
WHERE
    (`HPD:Help Desk`.`Status` IN ('Assigned', 'Pending', 'In Progress')
    AND `HPD:Help Desk`.`Submit Date` >= $__from/1000 AND `HPD:Help Desk`.`Submit Date` <= $__to/1000)
GROUP BY
    `HPD:Help Desk`.`Incident Number`, `HPD:Help Desk`.`Status`, `HPD:Help Desk`.`Priority`,
    `HPD:Help Desk`.`Assigned Group`, `HPD:Help Desk`.`Assignee`, `HPD:Help Desk`.`Submit Date`,
    `HPD:Help Desk`.`Last Modified Date`, `HPD:Help Desk`.`Description`, `HPD:Help Desk`.`Submitter`,
    `HPD:Search-Worklog`.`Worklog Description`
ORDER BY
    `HPD:Help Desk`.`Submit Date` ASC
LIMIT 20

我尝试了HPD:Help DeskHPD:WorklogHPD:Search-Worklog表单。我还尝试使用CASE语句仅显示每个事件的最新/最大工作日志ID,但没有成功。我确信我错过了一些明显/简单的东西。

英文:

I made this query which works. The issue is it displays rows for every worklog. I only need the latest worklog for each incident.

SELECT DISTINCT
`HPD:Help Desk`.`Incident Number` AS Incident_Number,
`HPD:Help Desk`.`Status` AS Status,
`HPD:Help Desk`.`Priority` AS Priority,
`HPD:Help Desk`.`Assigned Group` AS Assigned_Group,
`HPD:Help Desk`.`Assignee` AS Assignee,
`HPD:Help Desk`.`Submit Date` AS Submit_Date,
`HPD:Help Desk`.`Last Modified Date` AS Last_Modified_Date,
`HPD:Help Desk`.`Description` AS Description,
`HPD:Help Desk`.`Submitter` AS Submitter,
`HPD:Search-Worklog`.`Worklog Description` AS Worklog_Description,
`HPD:Search-Worklog`.`Work Log ID` AS Work_Log_ID
FROM
`HPD:Help Desk` 
INNER JOIN `HPD:Search-Worklog` on (`HPD:Search-Worklog`.`InstanceId` = `HPD:Help Desk`.`InstanceId`)
WHERE 
(`HPD:Help Desk`.`Status` in (&#39;Assigned&#39;,&#39;Pending&#39;,&#39;In Progress&#39;)
And `HPD:Help Desk`.`Submit Date` &gt;= $__from/1000 and `HPD:Help Desk`.`Submit Date` &lt;= $__to/1000)
ORDER BY
`HPD:Help Desk`.`Submit Date` ASC
LIMIT 20

I've tried the HPD:Help Desk, HPD:Worklog, and HPD:Search-Worklog forms. I've also tried using the CASE statement to only display the latest/max work log id for each incident to no luck. I'm sure I'm missing something obvious/simple.

答案1

得分: 1

以下是您要翻译的内容:

假设使用支持窗口函数的MySQL(或变种)的最新版本,获取“最新”的某些内容的非常有效的方法是使用 row_number() over()。在over子句中使用partition by来控制何时触发行号重置,并使用order by来控制分区中获取行号1的行。现在要获取“最新”需要在工作日志表中有一列使其成为可能 - 我已经假定在该表中存在一个时间戳列(worklog_entry_date),您需要替换实际确定“最新”的列。将当前查询嵌套一层,以便随后可以过滤行号为1的情况 - 这些将是“最新”的行:

SELECT
    *
FROM (
    SELECT
          h.`Incident Number` AS Incident_Number
        , h.`Status` AS STATUS
        , h.`Priority` AS Priority
        , h.`Assigned Group` AS Assigned_Group
        , h.`Assignee` AS Assignee
        , h.`Submit Date` AS Submit_Date
        , h.`Last Modified Date` AS Last_Modified_Date
        , h.`Description` AS Description
        , h.`Submitter` AS Submitter
        , w.`Worklog Description` AS Worklog_Description
        , w.`Work Log ID` AS Work_Log_ID
        , row_number() over(partition by h.`Incident Number`
                            order by w.worklog_entry_date DESC) as rn
    FROM `HPD: HELP Desk` h
    INNER JOIN `HPD: Search - Worklog` w ON w.`InstanceId` = h.`InstanceId`
    WHERE h.`Status` IN ('Assigned', 'Pending', 'In Progress')
        AND h.`Submit Date` >= $_ _from / 1000
        AND h.`Submit Date` <= $_ _to / 1000
    ) d
WHERE d.rn = 1
ORDER BY
      d.`Submit Date` ASC 
LIMIT 20

注意:当行号存在时,您不应该需要使用 select distinct - 通常情况下,如果可以的话,尽量避免使用它。此外,为了简化您的代码,请开始使用表别名。

英文:

Assuming a recent version of MySQL(or variant of) that supports window function, a very effective way to get "the most recent" of something is to use row_number() over(). Within the over clause use partition by to control what will trigger a reset of row numbering and use the order by to control which row within the partition that gets the row number 1. Now to get "the most recent" requires some column in the worklog table that enables this - I have assumed a timestamp column (worklog_entry_date exists in that table (worklog_entry_date), you will need to substitute the actual column(s) that determine "most recent". Nest the current query one layer so that you can then filter for when row numbers are 1 - these will then be "the most recent" rows:

SELECT
*
FROM (
SELECT
h.`Incident Number` AS Incident_Number
, h.`Status` AS STATUS
, h.`Priority` AS Priority
, h.`Assigned Group` AS Assigned_Group
, h.`Assignee` AS Assignee
, h.`Submit Date` AS Submit_Date
, h.`Last Modified Date` AS Last_Modified_Date
, h.`Description` AS Description
, h.`Submitter` AS Submitter
, w.`Worklog Description` AS Worklog_Description
, w.`Work Log ID` AS Work_Log_ID
, row_number() over(partition by h.`Incident Number`
order by w.worklog_entry_date DESC) as rn
FROM `HPD: HELP Desk` h
INNER JOIN `HPD: Search - Worklog` w ON w.`InstanceId` = h.`InstanceId`
WHERE h.`Status` IN (&#39;Assigned&#39;, &#39;Pending&#39;, &#39;In Progress&#39;)
AND h.`Submit Date` &gt;= $_ _from / 1000
AND h.`Submit Date` &lt;= $_ _to / 1000
) d
WHERE d.rn = 1
ORDER BY
d.`Submit Date` ASC 
LIMIT 20

nb: you should not need select distinct when row numbering is in place - and in general avoid this when you can. Also, do yourself a favour and start using table aliases to simplify your code.

huangapple
  • 本文由 发表于 2023年8月11日 01:16:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定