Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to join two queries together?

Author  Topic 

smith2487
Starting Member

9 Posts

Posted - 2013-04-10 : 11:20:38
Hello Everyone.
I have 2 separate quesries that I would like to join together, but have been unable to.
I would like to run a single query that would return
3 columns 'Name0', 'Agenttime' and 'LastBootUpTime0'
Does anyone know how I can accomplish this?
Thanks!
------------------------------------------------------------------
select

Name0,

Agenttime

from

v_AgentDiscoveries AGD,

dbo.v_GS_COMPUTER_SYSTEM CS

Where

CS.ResourceID = AGD.ResourceID

and AGD.AgentName = 'Heartbeat Discovery'

order by

Name0
--------------------------------------------------------------------

SELECT distinct sys.Name0, os.LastBootUpTime0 FROM v_R_System sys

join v_GS_OPERATING_SYSTEM os on sys.ResourceID=os.ResourceID join v_FullCollectionMembership fcm on fcm.ResourceID=os.ResourceID

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-04-10 : 12:50:07
It makes it hard to help yo when yo havne't provided any sample data or expected output. Additioanlly, you didn't describe how the two queries relate. However, I assume that they are related on the Name0 column. Here are some links on how to ask your questions in the future to help us provide you with better answers:

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Again, without knowing anything about your table structure or relations, I tried to take the two queries you provided and made derived tables out of them. However, I suspect that this can all be done without derived tables (just joins). If that doesn't work, pleae follow the links I provided above and provide more information.
]SELECT 
* -- Put the columns you want here
FROM
(
SELECT
Name0,
Agenttime
FROM
v_AgentDiscoveries AS AGD
INNER JOIN
dbo.v_GS_COMPUTER_SYSTEM AS CS
ON CS.ResourceID = AGD.ResourceID
WHERE
AGD.AgentName = 'Heartbeat Discovery'
) AS A
INNER JOIN
(
SELECT DISTINCT
sys.Name0,
os.LastBootUpTime0
FROM
v_R_System AS sys
INNER JOIN
v_GS_OPERATING_SYSTEM AS os
ON sys.ResourceID = os.ResourceID
INNER JOIN
v_FullCollectionMembership AS fcm
ON fcm.ResourceID=os.ResourceID

) AS B
ON A.Name0 = B.Name0
Go to Top of Page
   

- Advertisement -