简体   繁体   中英

SQL COUNT UNION for multi-column queries

I have a query which, for each Operating System, provides the total count of computers, total number with Program XYZ, total number exempt from requiring Program XYZ, and total number missing Program XYZ (which is all those who do not have program XYZ and are not exempt). My data is in two tables and I was hoping I could use a union to collapse the data into a single result. Here's what I mean:

SELECT [OS Name],
(SELECT COUNT (DISTINCT statement for total computers including a few joins and where))
(SELECT COUNT (DISTINCT statement for installed including a few joins and where))
(SELECT COUNT (DISTINCT statement for exempt including a few joins and where))
(SELECT COUNT (DISTINCT statement for missing including a few joins and where and a subquery))
FROM table

I can post the real query if it's helpful. Each SELECT is SELECT COUNT (DISTINCT Guid) FROM table JOIN table2 JOIN table3 WHERE condition1 and condition2, some of which use a subquery in the WHERE statement.

It returns results like this:

OS Name Total computers for this OS Program Installed   Exempt  Missing Program
Microsoft Windows XP    4776    819 12  3955
Windows 7 Enterprise    4   1   1   2

The second query runs against a different database:

OS Name Total computers for this OS Program Installed   Exempt  Missing Program
Microsoft Windows XP    42  7   0   36
Windows 7 Enterprise    2196    2143    21  33

My hope was that simply putting a UNION between these two would add up the numbers and my data would have two rows, but instead it has 4 -- each OS is listed twice, once per database.

I've searched Google and Stackoverflow, no luck.

Threads like this SQL Count(*) on multiple tables and this two SQL COUNT() queries? seem like the right approach, but I'm struggling to wrap my head around how I could employ it since my COUNTs are complicated. It's not COUNT (emp_id) for a simple column name, but draws on JOINs, conditions, and sometimes a subquery.

Use your union but then you need to further aggregate based on those results. Query approximate.

SELECT
    D.[OS Name]
,   SUM(D.[Computer Count]) AS [Computer Count]
,   D. ...
FROM
(
SELECT [OS Name],
(SELECT COUNT (DISTINCT statement for total computers including a few joins and where))
(SELECT COUNT (DISTINCT statement for installed including a few joins and where))
(SELECT COUNT (DISTINCT statement for exempt including a few joins and where))
(SELECT COUNT (DISTINCT statement for missing including a few joins and where and a subquery))
FROM table

UNION ALL
SELECT [OS Name],
(SELECT COUNT (DISTINCT statement for total computers including a few joins and where))
(SELECT COUNT (DISTINCT statement for installed including a few joins and where))
(SELECT COUNT (DISTINCT statement for exempt including a few joins and where))
(SELECT COUNT (DISTINCT statement for missing including a few joins and where and a subquery))
FROM table2
) D
GROUP BY
    D.[OS Name]

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