gpt4 book ai didi

mysql - [My]需要foreach处理的SQL查询语法

转载 作者:行者123 更新时间:2023-11-29 11:30:59 25 4
gpt4 key购买 nike

我有一个 SQL 查询任务,我可以在 C# 或 linqpad 中完成,但更喜欢它在 SQL 中,这样标准报告工具就可以完成它。

End DB 是 MySQL 下的 bugzilla

The problem is I need to loop through the bug_activity looking for particular changes to consider the parent record "valid", how ? e.g. pseudo logic like

  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

I am unsure how to do this as web examples only show DECLARE and LOOPs but how a loop fits into the "select, from, where" code.

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

最佳答案

SELECT 
bug_id,
SUM(CASE WHEN bug_status='IN BACKLOG' THEN 1 ELSE 0 END) as backlogCount,
SUM(CASE WHEN bug_status='ASSIGNED' THEN 1 ELSE 0 END) as assignedCount
FROM bugs_activity
WHERE action_date BETWEEN '2016-03-01' AND '206-03-31'
GROUP BY bug_id
HAVING backlogCount>0 AND assignedCount>0

选择返回在此期间处于“IN BACKLOG”和“ASSIGNED”状态的 bug_ids。您可以在 FROM 部分中使用上面的查询而不是 bugs_activity

更新:

添加到选择部分

  MAX(CASE WHEN bug_status='IN BACKLOG' THEN action_date ELSE NULL END) as backlogDate,
MAX(CASE WHEN bug_status='ASSIGNED' THEN action_date ELSE NULL END) as assignedDate

然后在 HAVING 部分 AND backlogDate<assignedDate

关于mysql - [My]需要foreach处理的SQL查询语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com