【发布时间】:2016-05-25 07:10:43
【问题描述】:
我有一个 SQL 查询任务,我可以在 C# 或 linqpad 中完成,但更喜欢它在 SQL 中,以便标准报告工具可以完成。
End DB 是 MySQL 下的 bugzilla
问题是我需要遍历 bug_activity 寻找特定的更改以考虑父记录“有效”,如何? 例如像
这样的伪逻辑
if bug_status went to IN BACKLOG
then bug_status went to ASSIGNED
and this happened 2016-03-01 to 206-03-31
then consider valid record
我不确定如何执行此操作,因为 Web 示例仅显示 DECLARE 和 LOOP,但循环如何适合“选择、来自、何处”代码。
set @BugID = 64252;
select
bugs_activity.bug_id,
-- profiles.realname,
-- profiles.login_name,
bugs_activity.bug_when,
fielddefs.name,
bugs_activity.added
-- bugs_activity.removed
from
bugs_activity,
profiles,
fielddefs
-- Real world 'Where xx' will have more logic and result in a number of bugzilla records
-- Each bugzilla record has its own 'bugs_activity'
-- Logic needs to look at each buzilla records historyto filter results
-- Want to end up with a filtered record set and a total number of records
Where bug_id = @BugID AND bugs_activity.who = profiles.userid AND bugs_activity.fieldid = fielddefs.id
bug_activity 示例
bug_id bug_when name added
64252 26/01/2016 6:51:30 AM status_whiteboard ID:103138574
64252 26/01/2016 6:52:10 AM cc xxx@abc.com
64252 28/01/2016 9:49:10 AM bug_status IN BACKLOG
64252 28/01/2016 9:49:10 AM cf_escalation_notes Effort: 2
Reproduced by support
64252 28/01/2016 9:49:10 AM assigned_to def@abc.com
64252 2/05/2016 4:33:05 PM bug_status ASSIGNED
【问题讨论】:
-
您能再解释一下您的要求吗?您可能可以在没有循环的情况下做到这一点。 SQL 是基于集合的,循环失败了。