来自UNION ALL语句中查询的多个列

时间:2019-03-05 15:44:05

标签: mysql union-all

我有一个查询(不是我自己写的),它使用UNION ALL-但是,如果由联合中的任一SELECT语句返回,则“分离分数”列中返回的数据需要显示NULL值。

以下查询提供了我希望查看的部分数据:

SELECT 
switches.HostName
, SUM(score) AS realTimeCap
, st.description AS 'Switch Type'
, switches.switchid
, GROUP_CONCAT(score) AS 'Separated Score'
FROM 
(
    SELECT 
        SwitchID, CallStackDepth * COUNT(*) AS score 
    FROM huntandpagingfeatures
    JOIN huntgroupmembers 
        ON huntgroupmembers.HuntGroupDN = huntandpagingfeatures.ListDN 
    WHERE IsHuntGroup = 1 
    GROUP BY SwitchID
UNION ALL 
    SELECT 
        SwitchID, COUNT(*) / 2 AS score 
    FROM userprogbuttons 
    JOIN maes 
        ON maes.MAE_DN=userprogbuttons.DialNumberDN 
    WHERE userprogbuttons.FunctionID = 30 
    GROUP BY SwitchID
    ) t 
JOIN switches 
ON t.SwitchID = switches.SwitchID 
JOIN Switchtypes st ON switches.SwitchTypeID = st.SwitchTypeID
GROUP BY t.SwitchID, st.description
ORDER BY realtimecap DESC

有时“分隔分数”列可能仅产生一个值,但不确定使用哪个选择语句创建该值。即使该值为NULL,我是否可以通过任何方式正确存储它以获取所需的数据?我不在乎该列是否分为两列。

谢谢!

0 个答案:

没有答案