如果找到 USER,则选择最后一行
我在 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
所以基本上我们记录所有问题以及谁负责/必须处理该问题。 我需要一个查询来查找一个人涉及哪些问题:
例如:
- ID 0900 的人同时涉及 155 和 159。ID
- 0282 的人仅涉及 155。
- 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:
- Person with ID 0900 was involved in both 155 and 159.
- Person with ID 0282 was involved in 155 only.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这会为您提供 0900 人处理过的所有问题,并且最后一行中有 ProblemCode=1。我无法测试它,所以可能会有一些错误。
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.
也许这个查询会有所帮助:
其中 LOGTABLE 是表的名称,x 是人员的 ID。
Perhaps this query could help:
where LOGTABLE is the name of the table, and x is the Person's ID.