預警:我不是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) –