SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to join two queries together?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

smith2487
Starting Member

Canada
9 Posts

Posted - 04/10/2013 :  11:20:38  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 04/10/2013 :  12:50:07  Show Profile  Reply with Quote
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

Edited by - Lamprey on 04/10/2013 12:52:46
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000