如果找到 USER,则选择最后一行

发布于 2024-12-12 03:52:19 字数 1172 浏览 0 评论 0原文

我在 SQL Server 中有一个日志表。表格的结构如下:

Unique  ProblemID  ResponsibleID  AssignedToID  ProblemCode

155     155        0282                         4
156     155                       0900
157     155                                     3
158     155                       0147          1
159     159        0111                         2
160     159                       0333          4
161     159        0900                         1

所以基本上我们记录所有问题以及谁负责/必须处理该问题。 我需要一个查询来查找一个人涉及哪些问题:

例如:

  1. ID 0900 的人同时涉及 155 和 159。ID
  2. 0282 的人仅涉及 155。
  3. ID号为0333的人仅涉及159。

另外我忘了提到我需要通过 ProblemCode 过滤 ProblemID 的最后一行。例如,找到涉及人员的问题 ID,但该问题的最后一个日志行中的 ProblemCode 为 1(这意味着该问题现已关闭)。

此外,我正在使用查询:

    select ProblemID, EntryTime, ResponsibleID, ProblemCode, AssignedToID
    from (select ProblemID, EntryTime, ResponsibleID, ProblemCode,  AssignedToID,
    row_number() over(partition by ProblemID order by EntryTime desc) as rn
    from myTable) as T
    where rn = 1 and ResponsibleID = '00282' OR AssignedToID = '00282' 
    and veiksmoid <> 4

但是,它只匹配最后一行。

I have a log table in SQL Server. Table is structured this way:

Unique  ProblemID  ResponsibleID  AssignedToID  ProblemCode

155     155        0282                         4
156     155                       0900
157     155                                     3
158     155                       0147          1
159     159        0111                         2
160     159                       0333          4
161     159        0900                         1

So basically we log all problems and who was responsible/had to deal with the problem.
I need a query that would find which problems one person was involved in:

For example:

  1. Person with ID 0900 was involved in both 155 and 159.
  2. Person with ID 0282 was involved in 155 only.
  3. Person with ID 0333 was involved in 159 only.

Also I forgot to mention that I need to filter the last row of ProblemID by the ProblemCode. For example find problemID where person is involved, but there the ProblemCode in the last log line of that problem is 1 (Which means the problem is now closed).

Furthermore, I was working with query:

    select ProblemID, EntryTime, ResponsibleID, ProblemCode, AssignedToID
    from (select ProblemID, EntryTime, ResponsibleID, ProblemCode,  AssignedToID,
    row_number() over(partition by ProblemID order by EntryTime desc) as rn
    from myTable) as T
    where rn = 1 and ResponsibleID = '00282' OR AssignedToID = '00282' 
    and veiksmoid <> 4

However, it ONLY matches the last rows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不打扰别人 2024-12-19 03:52:19

这会为您提供 0900 人处理过的所有问题,并且最后一行中有 ProblemCode=1。我无法测试它,所以可能会有一些错误。

SELECT problemID FROM <table> t1
WHERE problemID IN  (
      SELECT problemID FROM <table>
      WHERE (ResponsibleID = 0900 OR AssignedToID = 0900))
AND problemCode = 1
AND unique = (SELECT MAX(unique) FROM <table> WHERE problemID = t1.problemID)

This gives you all the problems that a person 0900 dealed with and that have problemCode=1 in their last line. I can't test it so there might be some errors.

SELECT problemID FROM <table> t1
WHERE problemID IN  (
      SELECT problemID FROM <table>
      WHERE (ResponsibleID = 0900 OR AssignedToID = 0900))
AND problemCode = 1
AND unique = (SELECT MAX(unique) FROM <table> WHERE problemID = t1.problemID)
生生漫 2024-12-19 03:52:19
SELECT ag.UserId, ag.ProblemID
FROM(
   SELECT ProblemID, ResponsibleID as UserId
   FROM Table 
   WHERE ResponsibleID IS NOT NULL

   UNION ALL

   SELECT ProblemID, AssignedToID  as UserId
   FROM Table 
   WHERE AssignedToID IS NOT NULL
) ag
GROUP BY ag.UserId, ag.ProblemID
SELECT ag.UserId, ag.ProblemID
FROM(
   SELECT ProblemID, ResponsibleID as UserId
   FROM Table 
   WHERE ResponsibleID IS NOT NULL

   UNION ALL

   SELECT ProblemID, AssignedToID  as UserId
   FROM Table 
   WHERE AssignedToID IS NOT NULL
) ag
GROUP BY ag.UserId, ag.ProblemID
也只是曾经 2024-12-19 03:52:19

也许这个查询会有所帮助:

SELECT ProblemID
FROM LOGTABLE
WHERE (ResponsibleID = x) OR (AssignedToID = x)

其中 LOGTABLE 是表的名称,x 是人员的 ID。

Perhaps this query could help:

SELECT ProblemID
FROM LOGTABLE
WHERE (ResponsibleID = x) OR (AssignedToID = x)

where LOGTABLE is the name of the table, and x is the Person's ID.

软糯酥胸 2024-12-19 03:52:19
SELECT distinct problemid
   from YourTable
   where 
         ( Responsible = 0900
      OR AssignedToID = 0900 )
      AND ProblemCode <> 1
SELECT distinct problemid
   from YourTable
   where 
         ( Responsible = 0900
      OR AssignedToID = 0900 )
      AND ProblemCode <> 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文