预警:我不是SQL开发人员。我正在查看此查询并尝试修改它以解决结果问题。SQL查询 - 指定如何选择特定列
我有后续的查询该作品(语法):
SELECT
MIN(issue_time) AS incident_time,
MIN(issue_status) AS issue_status,
MIN(monitor_start_time) AS monitor_start_time,
MAX(resolved_time) AS resolved_time,
incident_id,
MAX(model_handle) AS model_handle,
device_retrieval_id,
MAX(last_status_time) AS last_status_time,
MAX(last_analysis_time) AS last_analysis_time,
n.node_name,
MAX(last_problem_time) AS last_problem_time,
SUM(problem_count) AS problem_count
FROM (
SELECT
issue_time, issue_status, monitor_start_time, resolved_time, incident_id,
model_handle, device_retrieval_id, last_status_time, last_analysis_time,
last_problem_time, problem_count
FROM x_network_device_node_status
UNION
SELECT
issue_time, issue_status, monitor_start_time, resolved_time, incident_id,
model_handle, device_retrieval_id, last_status_time, last_analysis_time,
last_problem_time, problem_count
FROM x_network_device_node_status_hist
WHERE incident_id IN (
SELECT DISTINCT incident_id
FROM (
SELECT incident_id
FROM x_network_device_node_status
UNION
SELECT incident_id
FROM x_network_device_node_status_hist
WHERE issue_time >= '2016-12-10'
) AS i
)
) AS x LEFT JOIN
x_nodes AS n ON x.device_retrieval_id = n.node_retrieval_id
GROUP BY incident_id, device_retrieval_id, node_name
ORDER BY incident_time DESC;
最近我意识到,我们不应该选择MAX(resolved_time) AS resolved_time
,而是应与被获取resolved_time
每个incident_id
从记录最近的issue_time
。
例如,如果在数据库中记录特定incident_id的联盟:
╔═════════════════════════╦══════════════╦═════════════════════════╦═════════════════════════╦═════════════════════╦══════════════╦═════════════════════╦═════════════════════════╦═════════════════════════╦═════════════════════════╦═══════════════╗
║ issue_time ║ issue_status ║ monitor_start_time ║ resolved_time ║ incident_id ║ model_handle ║ device_retrieval_id ║ last_status_time ║ last_analysis_time ║ last_problem_time ║ problem_count ║
╠═════════════════════════╬══════════════╬═════════════════════════╬═════════════════════════╬═════════════════════╬══════════════╬═════════════════════╬═════════════════════════╬═════════════════════════╬═════════════════════════╬═══════════════╣
║ 2016-12-13 17:15:00.000 ║ 1 ║ 2016-12-13 17:46:28.000 ║ 2016-12-13 18:16:34.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-13 18:16:34.000 ║ 2016-12-13 18:01:28.000 ║ NULL ║ 0 ║
║ 2016-12-13 18:15:00.000 ║ 1 ║ NULL ║ 2016-12-13 18:31:28.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-13 18:31:28.000 ║ NULL ║ NULL ║ 0 ║
║ 2016-12-13 18:31:28.000 ║ 2 ║ 2016-12-14 09:16:28.000 ║ 2016-12-15 08:31:30.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 08:31:30.000 ║ 2016-12-15 08:16:28.000 ║ 2016-12-15 08:16:28.000 ║ 6 ║
║ 2016-12-15 08:30:00.000 ║ 1 ║ 2016-12-15 10:01:31.000 ║ 2016-12-15 11:46:28.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 11:46:28.000 ║ 2016-12-15 11:31:31.000 ║ 2016-12-15 11:31:31.000 ║ 3 ║
║ 2016-12-15 11:45:00.000 ║ 1 ║ NULL ║ 2016-12-15 12:01:31.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 12:01:31.000 ║ NULL ║ NULL ║ 0 ║
║ 2016-12-15 12:00:00.000 ║ 1 ║ 2016-12-15 12:16:28.000 ║ 2016-12-15 12:31:29.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 12:31:29.000 ║ NULL ║ NULL ║ 0 ║
║ 2016-12-15 12:30:00.000 ║ 1 ║ NULL ║ 2016-12-15 13:01:31.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 13:01:31.000 ║ NULL ║ NULL ║ 0 ║
║ 2016-12-15 13:01:31.000 ║ 2 ║ 2016-12-15 16:46:28.000 ║ NULL ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 22:31:28.000 ║ 2016-12-15 22:31:28.000 ║ 2016-12-15 17:31:28.000 ║ 2 ║
╚═════════════════════════╩══════════════╩═════════════════════════╩═════════════════════════╩═════════════════════╩══════════════╩═════════════════════╩═════════════════════════╩═════════════════════════╩═════════════════════════╩═══════════════╝
我想结果是:
╔═════════════════════════╦══════════════╦═════════════════════════╦═══════════════╦═════════════════════╦══════════════╦═════════════════════╦═════════════════════════╦═════════════════════════╦════════════╦═════════════════════════╦═══════════════╗
║ incident_time ║ issue_status ║ monitor_start_time ║ resolved_time ║ incident_id ║ model_handle ║ device_retrieval_id ║ last_status_time ║ last_analysis_time ║ node_name ║ last_problem_time ║ problem_count ║
╠═════════════════════════╬══════════════╬═════════════════════════╬═══════════════╬═════════════════════╬══════════════╬═════════════════════╬═════════════════════════╬═════════════════════════╬════════════╬═════════════════════════╬═══════════════╣
║ 2016-12-13 17:15:00.000 ║ 1 ║ 2016-12-13 17:46:28.000 ║ NULL ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 22:31:28.000 ║ 2016-12-15 22:31:28.000 ║ MyNodeName ║ 2016-12-15 17:31:28.000 ║ 11 ║
╚═════════════════════════╩══════════════╩═════════════════════════╩═══════════════╩═════════════════════╩══════════════╩═════════════════════╩═════════════════════════╩═════════════════════════╩════════════╩═════════════════════════╩═══════════════╝
但是,相反,它是回来作为:
╔═════════════════════════╦══════════════╦═════════════════════════╦═════════════════════════╦═════════════════════╦══════════════╦═════════════════════╦═════════════════════════╦═════════════════════════╦════════════╦═════════════════════════╦═══════════════╗
║ incident_time ║ issue_status ║ monitor_start_time ║ resolved_time ║ incident_id ║ model_handle ║ device_retrieval_id ║ last_status_time ║ last_analysis_time ║ node_name ║ last_problem_time ║ problem_count ║
╠═════════════════════════╬══════════════╬═════════════════════════╬═════════════════════════╬═════════════════════╬══════════════╬═════════════════════╬═════════════════════════╬═════════════════════════╬════════════╬═════════════════════════╬═══════════════╣
║ 2016-12-13 17:15:00.000 ║ 1 ║ 2016-12-13 17:46:28.000 ║ 2016-12-15 13:01:31.000 ║ 1427068756009427097 ║ 85983911 ║ 332265337 ║ 2016-12-15 22:31:28.000 ║ 2016-12-15 22:31:28.000 ║ MyNodeName ║ 2016-12-15 17:31:28.000 ║ 11 ║
╚═════════════════════════╩══════════════╩═════════════════════════╩═════════════════════════╩═════════════════════╩══════════════╩═════════════════════╩═════════════════════════╩═════════════════════════╩════════════╩═════════════════════════╩═══════════════╝
虽然这个代码片断可以解决这个问题,它没有解释为什么不然怎么回答了这个问题。请[请提供您的代码解释](// meta.stackexchange.com/q/114762/269535),因为这确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 **举报人/评论者:** [仅限代码解答,例如downvote,请勿删除!](// meta.stackoverflow.com/a/260413/2747593) –