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
 General SQL Server Forums
 New to SQL Server Programming
 Union of two queries?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mosiki
Starting Member

12 Posts

Posted - 07/09/2013 :  10:18:52  Show Profile  Reply with Quote
Any ideas why this code wont work? Attempting to join Home Games and Away games data from two queries.

SELECT		Soccer_Base.dbo.Teams.TeamName
				,Sum(Games) as games
				
	FROM		
		
			(		
			SELECT		Soccer_Base.dbo.Results.HomeTeam as Team
					  ,COUNT(Soccer_Base.dbo.Results.HomeTeam) as [Games]
					  ,SUM(Case WHEN Soccer_Base.dbo.Results.HomeFT> Soccer_Base.dbo.Results.AwayFT THEN 1 ELSE 0 END) AS [Win]
					  ,SUM(Case WHEN Soccer_Base.dbo.Results.HomeFT= Soccer_Base.dbo.Results.AwayFT THEN 1 ELSE 0 END) AS [Draw]
					  ,SUM(Case WHEN Soccer_Base.dbo.Results.HomeFT< Soccer_Base.dbo.Results.AwayFT THEN 1 ELSE 0 END) AS [Loss]
		 	  

			FROM		Soccer_Base.dbo.Results		LEFT JOIN Soccer_Base.dbo.Teams ON
													Soccer_Base.dbo.Teams.TeamName = Soccer_Base.dbo.Results.HomeTeam

			WHERE		Season = '2012-13' AND Competition = 'Premier League'
						

			GROUP BY	Soccer_Base.dbo.Results.HomeTeam
			
			
	UNION

			SELECT		Soccer_Base.dbo.Results.AwayTeam as Team
					  ,COUNT(Soccer_Base.dbo.Results.AwayTeam) as [Games]
					  ,SUM(Case WHEN Soccer_Base.dbo.Results.HomeFT< Soccer_Base.dbo.Results.AwayFT THEN 1 ELSE 0 END) AS [Win]
					  ,SUM(Case WHEN Soccer_Base.dbo.Results.HomeFT= Soccer_Base.dbo.Results.AwayFT THEN 1 ELSE 0 END) AS [Draw]
					  ,SUM(Case WHEN Soccer_Base.dbo.Results.HomeFT> Soccer_Base.dbo.Results.AwayFT THEN 1 ELSE 0 END) AS [Loss]
		 	  

			FROM		Soccer_Base.dbo.Results			LEFT JOIN Soccer_Base.dbo.Teams ON
														Soccer_Base.dbo.Teams.TeamName = Soccer_Base.dbo.Results.AwayTeam

			WHERE		Season = '2012-13' AND Competition = 'Premier League'
						

			GROUP BY	Soccer_Base.dbo.Results.AwayTeam

			) z
	
	GROUP BY	Soccer_Base.dbo.Teams.TeamName


Many Thanks

Mosiki

webfred
Flowing Fount of Yak Knowledge

Germany
8760 Posts

Posted - 07/09/2013 :  10:30:14  Show Profile  Visit webfred's Homepage  Reply with Quote
Outside the derived table named z you can't use names like Soccer_Base.dbo.Teams.TeamName
instead you have to use z.Team for example


Too old to Rock'n'Roll too young to die.
Go to Top of Page

mosiki
Starting Member

12 Posts

Posted - 07/09/2013 :  10:55:11  Show Profile  Reply with Quote
Oh right, it works now. Thanks a lot.
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.05 seconds. Powered By: Snitz Forums 2000