Solved

join two records

Posted on 2004-03-23
4
536 Views
Last Modified: 2011-09-20
i have a database  called  results in sql server 2000

the fields are the following  :
gameid, idteam, points, goalsinfavor, goalsagainst

everytime a manager reports a game played i have set up 2 commands one inserts the record for hometeam and the other inserts the record for the visiting team

so if a game  had the score 4-2   home wins   then it will insert 2 records
and the record for the home is:
gameid= 300
idteam= 4 (unique for each team)
points = 3
goalsinfavor=4
goals against= 2


and the record for the visitor is:
gameid= 300
idteam= 3(unique for each team)
points = 0
goalsinfavor=2
goals against= 4

how can i do to query by gameid=300
so i can show the score for this game?

or any ideas on how to combine those 2 records
0
Comment
Question by:gianitoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:lschweer
lschweer earned 250 total points
ID: 10662772
i assume you need both team ids in one row then use

SELECT t1.gameid, t1.idteam, t2.idteam, t1.goalsinfavor, t1.[goals against]
  FROM Games as t1
  LEFT JOIN Games as t2 ON t1.GameID = t2.GameID and t1.idteam<>t2.idteam
 
there is a drawback for this solution .. you will receive 2 rows per game

if you need one row just filter out the winning teams

  WHERE t1.goalsinfavor >= t1.[goals against]

and use a DISTINCT after the select to remove the duplicates for even games

lars
0
 
LVL 1

Accepted Solution

by:
cubical10 earned 250 total points
ID: 10664181
This query will give you the score by game (I assumed that your table is called results).

         SELECT  gameid, MAX(goalsinfavor) AS Winner, MIN(goalsagainst) AS Loser
         FROM results
         GROUP BY gameid;
This is the short fast answer, but no reference to team is here.

You did not provide any detail on the other tables in your db, so I did not have much to work with.  
With some detail of other tables, I could have built a better query...

For a more complete solution all based on this one table (results), you can create two views.
winnersByGameID and losersByGameID.

winnersByGameID is defined as:
    SELECT     gameid, idteam, goalsinfavor, goalsagainst
    FROM         dbo.results
    WHERE     (points = 3);

losersByGameID is defined as:
   SELECT     gameid, idteam, goalsinfavor, goalsagainst
   FROM         dbo.results
   WHERE     (points =0)

then your report query is this:
    SELECT
         winnersbygame.gameid, winnersbygame.idteam AS winningteam, winnersbygame.goalsinfavor AS   WinnerGoals,
         losersbygame.idteam AS losingteam, losersbygame.goalsinfavor AS LoserGoals
    FROM  losersbygame INNER JOIN winnersbygame
    ON losersbygame.gameid = winnersbygame.gameid

N.B: this solution does not allow for tie games!  As I said this is a work around because this is the only table defined.

Hope this helps...
0
 
LVL 2

Expert Comment

by:lschweer
ID: 10665341
as i said this will also work for tie games

SELECT DISTINCT t1.gameid, t1.idteam, t2.idteam, t1.goalsinfavor, t1.[goals against]
  FROM Games as t1
  LEFT JOIN Games as t2 ON t1.GameID = t2.GameID and t1.idteam<>t2.idteam
  WHERE t1.goalsinfavor >= t1.[goals against]

lars
0
 
LVL 1

Author Comment

by:gianitoo
ID: 10792932
with this query  

SELECT distinct t1.gamesid, t1.idofteam, t2.idofteam, t1.goalsinfavor, t1.goalsagainst
FROM dbo.gameresults as t1    LEFT JOIN dbo.gameresults as t2 ON t1.GamesID = t2.GamesID and t1.idofteam<>t2.idofteam
WHERE t1.goalsinfavor >= t1.goalsagainst

i see results like this

gamesid  |   idofteam |   idofteam | goalsinfavor   |  goalsagainst |

230                5                  5                  3                     null


i would like to show different idofteam in the third column and i do not know why the goals against is not showing?

please help
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

710 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