Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display results of 2 queries in one result

Posted on 2011-03-10
4
Medium Priority
?
399 Views
Last Modified: 2012-05-11
I am hoping fo r aquick turnaround on this question, probably really easy....

I nned to produce a query which shows the total number of servers per building AND the total number of servers down in the same result.

Example:

BLDG          TotalServers         DownServers
--------------------------------------------------------------
123                   20                              0
456                   16                              2

The queries I am using are:
For all servers
select bldg, count(*) as TotalServers from nodes
where vendor = 'windows' and status = 1
group by bldg

For down servers
select bldg, count(*) as DownServers from nodes
where vendor = 'windows' and status <> 1
group by bldg

Can these be combined?
0
Comment
Question by:edrz01
  • 2
4 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 35094796
Use UNION to combine them

select bldg, count(*) as TotalServers from nodes
where vendor = 'windows' and status = 1
group by bldg

UNION ALL

select bldg, count(*) as DownServers from nodes
where vendor = 'windows' and status <> 1
group by bldg

0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 35094812
Remove the first group by, sorry

select bldg, count(*) as TotalServers from nodes
where vendor = 'windows' and status = 1

UNION ALL

select bldg, count(*) as DownServers from nodes
where vendor = 'windows' and status <> 1
group by bldg
0
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 total points
ID: 35094837

select isnull(a.bldg, b.bldg), isnull(TotalServers, 0) as 'UpServers', isnull(DownServers, 0) as 'DownServers' from (
select bldg, count(*) as TotalServers from nodes
where vendor = 'windows' and status = 1
group by bldg) a
full outer join (
select bldg, count(*) as DownServers from nodes
where vendor = 'windows' and status <> 1
group by bldg) b
on a.bldg = b.bldg
0
 

Author Closing Comment

by:edrz01
ID: 35095521
Perfect! This was just what I needed. (The others were unions and would have stacked the data rather than in columns.)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question