SQL查询 - 指定如何选择特定列

时间:2016-12-15 22:49:52

标签: sql sql-server

预警:我不是SQL Dev。我正在查看此查询并尝试修改它以解决结果问题。

我有以下查询(语法上):

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的{​​{1}}

例如,如果数据库中特定incident_id的记录的UNION为:

issue_time

我希望结果是:

╔═════════════════════════╦══════════════╦═════════════════════════╦═════════════════════════╦═════════════════════╦══════════════╦═════════════════════╦═════════════════════════╦═════════════════════════╦═════════════════════════╦═══════════════╗
║       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 ║
╚═════════════════════════╩══════════════╩═════════════════════════╩═══════════════╩═════════════════════╩══════════════╩═════════════════════╩═════════════════════════╩═════════════════════════╩════════════╩═════════════════════════╩═══════════════╝

2 个答案:

答案 0 :(得分:1)

SELECT
  MIN(issue_time) AS incident_time,
  MIN(issue_status) AS issue_status,
  MIN(monitor_start_time) AS monitor_start_time,
  (SELECT TOP 1 xxx.resolved_time
   FROM
    (SELECT xx.issue_time,xx.resolved_time
     FROM x_network_device_node_status xx
     WHERE xx.incident_id=x.incident_id
     UNION ALL
     SELECT xx2.issue_time,xx2.resolved_time
     FROM x_network_device_node_status_hist xx2
     WHERE xx2.incident_id=x.incident_id) AS xxx
   ORDER BY xxx.issue_time DESC) AS resolved_time,
  incident_id,

答案 1 :(得分:0)

我不确定语法,但想法是使用CTE并查询它。

;WITH CTE AS
(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
)
)
SELECT
MIN(issue_time) AS incident_time,
MIN(issue_status) AS issue_status,
MIN(monitor_start_time) AS monitor_start_time,
(SELECT resolved_time FROM CTE WHERE issue_time = (SELECT MAX(issue_time) FROM CTE)) 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 CTE 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;