我有以下查询。
SELECT DISTINCT (Count(ID)) AS Stat, Site AS Other
FROM Network_Devices
WHERE Function Like '*irewall*'
GROUP BY Site;
Union
SELECT DISTINCT (Count(ID)) AS Stat, Site AS Other
FROM Network_Devices
WHERE Decom = "No"
GROUP BY Site;
union
SELECT DISTINCT (Count(ID)) AS Stat, Site AS Other
FROM Physical_Devices
WHERE Decom = "No"
GROUP BY Site;
union
SELECT DISTINCT (Count(ID)) AS Stat, Rack_Site AS Other
FROM Racks
GROUP BY Rack_Site;
union
SELECT DISTINCT (Count(ID)) AS Stat, Cluster AS Other
FROM Virtual_Devices
WHERE Decomissioned = "No"
GROUP BY Cluster;
union
SELECT DISTINCT (Count(ID)) AS Stat, OS AS Other
FROM Virtual_Devices
WHERE Decomissioned = "No"
GROUP BY OS;
union
SELECT DISTINCT (Count(ID)) AS Stat, Location AS Other
FROM Virtual_Devices
WHERE Decomissioned = "No"
GROUP BY Location;
内部有多个select语句,当运行查询时,这些行是按随机顺序排列的,而不是在各自的语句中分组。在查看结果或将结果导出到Excel时,有没有办法分隔或分组行?
答案 0 :(得分:1)
您可以这样做:
SELECT * FROM (
SELECT DISTINCT 1 as GroupId, (Count(ID)) AS Stat, Site AS Other
FROM Network_Devices
WHERE Function Like '*irewall*'
GROUP BY Site;
Union
SELECT DISTINCT 2, (Count(ID)) AS Stat, Site AS Other
FROM Network_Devices
WHERE Decom = "No"
GROUP BY Site;
union
SELECT DISTINCT 3, (Count(ID)) AS Stat, Site AS Other
FROM Physical_Devices
WHERE Decom = "No"
GROUP BY Site;
union
SELECT DISTINCT 4, (Count(ID)) AS Stat, Rack_Site AS Other
FROM Racks
GROUP BY Rack_Site;
union
SELECT DISTINCT (Count(ID)) AS Stat, Cluster AS Other
FROM Virtual_Devices
WHERE Decomissioned = "No"
GROUP BY Cluster;
union
SELECT DISTINCT 5, (Count(ID)) AS Stat, OS AS Other
FROM Virtual_Devices
WHERE Decomissioned = "No"
GROUP BY OS;
union
SELECT DISTINCT 6, (Count(ID)) AS Stat, Location AS Other
FROM Virtual_Devices
WHERE Decomissioned = "No"
GROUP BY Location;
) ORDER BY GroupId
您可以根据需要使用GroupId列进行排序或分组。
答案 1 :(得分:0)
在您的对帐单中添加其他列,例如
SELECT 'irewall' [group], DISTINCT (Count(ID)) AS Stat, Site AS Other
FROM Network_Devices
WHERE Function Like '*irewall*'
GROUP BY Site;
Union
SELECT 'Decom=No', DISTINCT (Count(ID)) AS Stat, Site AS Other
FROM Network_Devices
WHERE Decom = "No"
GROUP BY Site;
union
...
因此您可以按[group]
的值对这些行进行分组