gpt4 book ai didi

sql - 多个选择查询合并为一个查询

转载 作者:行者123 更新时间:2023-12-02 08:41:46 25 4
gpt4 key购买 nike

我需要将多个 select 语句分组到一个查询和多个列中

我已经尝试过使用 UNION,但它导致我的一个选择丢失(可能误用它)

SELECT COUNT(*) AS EMERGENCIA FROM Incidentes WHERE Prioridad_Real = 'Emergencia' AND Estado = 'Resuelto a Tiempo'
AND Fecha_Asigna >= '2013-08-22 13:30:00.000' and Fecha_Resul <= '2013-08-23 11:16:00.000'

SELECT COUNT(*) AS ALTA FROM Incidentes WHERE Prioridad_Real = 'Alta' AND Estado = 'Resuelto a Tiempo'
AND Fecha_Asigna >= '2013-08-22 13:30:00.000' and Fecha_Resul <= '2013-08-23 11:16:00.000'

SELECT COUNT(*) AS MEDIA FROM Incidentes WHERE Prioridad_Real = 'Media' AND Estado = 'Resuelto a Tiempo'
AND Fecha_Asigna >= '2013-08-22 13:30:00.000' and Fecha_Resul <= '2013-08-23 11:16:00.000'

SELECT COUNT(*) AS BAJA FROM Incidentes WHERE Prioridad_Real = 'Baja' AND Estado = 'Resuelto a Tiempo'
AND Fecha_Asigna >= '2013-08-22 13:30:00.000' and Fecha_Resul <= '2013-08-23 11:16:00.000'

预期结果:

Expected result

最佳答案

尝试使用以下查询:

SELECT SUM(CASE WHEN Prioridad_Real = 'Emergencia' THEN 1 ELSE 0 END) as EMERGENCIA,
SUM(CASE WHEN Prioridad_Real = 'Alta' THEN 1 ELSE 0 END) as ALTA,
SUM(CASE WHEN Prioridad_Real = 'Media' THEN 1 ELSE 0 END) as MEDIA,
SUM(CASE WHEN Prioridad_Real = 'Baja' THEN 1 ELSE 0 END) as BAJA
FROM Incidentes WHERE Estado = 'Resuelto a Tiempo'
AND Fecha_Asigna >= '2013-08-22 13:30:00.000' and Fecha_Resul <= '2013-08-23 11:16:00.000'

关于sql - 多个选择查询合并为一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55680525/

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