简体   繁体   中英

SQL Query - Specify how to select a particular column

A forewarning: I am not a SQL Dev. I am looking at this query and trying to modify it to resolve an issue with the results.

I have the follow query which works (syntactically):

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;

I recently realized that that we should not be selecting the MAX(resolved_time) AS resolved_time , but instead should be getting the resolved_time for each incident_id from the record with the most recent issue_time .

For example, if the UNION of records in the database for a particular incident_id was:

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

I would like the result to be:

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

But instead, it is coming back as:

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

I'm not sure about the syntax, but the idea is to use CTE and query it.

;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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM