Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 16th, 2003, 11:02 PM
silverduck silverduck is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 7 silverduck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 0
Soccer League table

Hi, Im trying to put together a database that will keep track of soccer results and display league tables after matches are played.
The problem i have run into is how to display the updated league tables properly.
The results from played matches comes from a table called Match.

MATCHID, LEAGUEID ,ROUNDNO ,MDATE, HOMETEAM ,AWAYTEAM ,
REFEREE , SPECTACTORS ,HOMEGLS_H , AWAYGLS_H ,
HOMEGLS_F ,AWAYGLS_F ,RESULT char (H,D or A)

My league table have to contain the following data :
Teamid,
Played(number of matches each team has played)
Homewins
Homedraws
Homelost
homegoals scored
homegoals conceded
awaywins
awaydraws
awaylost
awaygoals scored
awaygoals coceded
Points

So my question is, is it possible to make a view to get all the information I want from the match table, or do I have to make a separate physical table for the league_table...
I just think I would benefit from having it as a view because i wouldnt have to update the league_table table for every match played, and it would also dramatically reduce data redundancy.

I have managed to put together a view that displays each teams home stats or away stats, but not the complete table. These views are defined as :

CREATE VIEW Hometable as
SELECT teamid, sum(homegls_f) as HF, sum(awaygls_f) as HA, ( sum(homegls_f) - sum(awaygls_f) ) as HDiff
FROM team, match
WHERE hometeam in
(SELECT team.teamid
FROM team, league_teams
WHERE team.teamid = league_teams.teamid AND league_teams.leagueid = 2)
AND team.teamid = match.hometeam
GROUP BY teamid,name
ORDER BY HDiff DESC,HF DESC;

and

CREATE VIEW Awaytable as
SELECT teamid, sum(awaygls_f) as AF, sum(homegls_f) as AA, ( sum(awaygls_f) - sum(homegls_f) ) as ADiff,
FROM team, match
WHERE awayteam in
(SELECT team.teamid
FROM team, league_teams
WHERE team.teamid = league_teams.teamid AND league_teams.leagueid = 2)
AND team.teamid = match.awayteam
GROUP BY teamid
ORDER BY ADiff DESC,AF DESC;

Is there any way to combine these two views to get a complete table, or do I have to make a new view | make a separate league_table table, |or change the structure of my match table ??

Can anyone give me some pointers here ??


Thanks in advance

Arve

Reply With Quote
  #2  
Old August 17th, 2003, 08:32 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,339 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 47 m 13 sec
Reputation Power: 891
i can do this for you, but you will have to define and explain the relationships and differences between _H, _F, H, D, A, scored, and conceded

your Match table is fine

the key trick here is to use a UNION ALL to create two rows for every row in the Match table --

from (
select matchid
, leagueid
, roundno
, mdate
, awayteam as teamid
, hometeam as oppid
, awaygls_h as gls_h
, awaygls_f as gls_f
, homegls_h as oppgls_h
, homegls_f as oppgls_f
from match
union all
select matchid
, leagueid
, roundno
, mdate
, hometeam
, awayteam
, homegls_h
, homegls_f
, awaygls_h
, awaygls_f
from match
) as teammatches

see this thread --
http://dbforums.com/showthread.php?threadid=863102

rudy
http://r937.com/

Reply With Quote
  #3  
Old August 17th, 2003, 08:15 PM
silverduck silverduck is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 7 silverduck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 0
Thanks for your reply Rudy,

Now for explaining the thinks you wanted :
HOMEGLS_H (Goals the hometeam has scored by Half Time)
AWAYGLS_H (Goals the awayteam has scored by Half Time)
HOMEGLS_F ,(Goals the hometeam has scored by Full Time)
AWAYGLS_F (Goals the awayteam has scored by Full Time)

Scored and conceded would be like Homegls_F ( Homegoals For) and Homegls_A ( Homecoals Against) sorry about all the confusion

The H,D and A is for Home, Draw and Away. (Homewin,Draw, Awaywin)
I just thought I should put something in so I could calculate the points after a match.
I also thought of putting a Points field instead of the RESULT and putting 3,1 or 0 points after each match, but then I would have to have 2 columns, one for HomeTeamPoints and one for AwayteamPoints....I dont know if that is the best sollution though, I just want to make it as easy to query as possible

Another thing I was wondering is when I display a leaguetable ordered by Points, Is there any possible way to actually calculate which posistion each team has in the table 2nd, 3rd etc ?

Thanks again Rudy

Regards

Arve

Reply With Quote
  #4  
Old August 19th, 2003, 04:46 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,339 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 47 m 13 sec
Reputation Power: 891
Code:
select teams.name 
     , count(matchid)                     as played
     , sum(case when homeaway='home' 
                 and teamgls > oppgls
                then 1 else 0 end)        as homewins 
     , sum(case when homeaway='home' 
                 and teamgls = oppgls
                then 1 else 0 end)        as homedraws 
     , sum(case when homeaway='home' 
                 and teamgls < oppgls
                then 1 else 0 end)        as homelost
     , sum(case when homeaway='home' 
                then teamgls else 0 end)  as homegoalsscored
     , sum(case when homeaway='home' 
                then oppgls else 0 end)   as homegoalsconceded
     , sum(case when homeaway='away' 
                 and teamgls > oppgls
                then 1 else 0 end)        as awaywins 
     , sum(case when homeaway='away' 
                 and teamgls = oppgls
                then 1 else 0 end)        as awaydraws 
     , sum(case when homeaway='away' 
                 and teamgls < oppgls
                then 1 else 0 end)        as awaylost
     , sum(case when homeaway='away' 
                then teamgls else 0 end)  as awaygoalsscored
     , sum(case when homeaway='away' 
                then oppgls else 0 end)   as awaygoalsconceded
     , sum(case when teamgls > oppgls
                then 3
                when teamgls = oppgls
                then 1
                else 0 end)               as points                            
  from league_teams 
inner
  join teams
    on league_teams.teamid = teams.teamid 
left outer 
  join (
select matchid 
     , roundno
     , mdate
     , 'away'        as homeaway
     , awayteam      as teamid
     , hometeam      as oppid 
     , awaygls_f     as teamgls
     , homegls_f     as oppgls
  from match 
union all
select matchid 
     , roundno
     , mdate
     , 'home'
     , hometeam      
     , awayteam
     , homegls_f
     , awaygls_f
  from match 
       ) as teammatches
    on team.teamid = teammatches.teamid
 where league_teams.leagueid = 2
group 
    by team.name
you have leagueid in the Match table, that's redundant, it's in the Teams table

rudy

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Soccer League table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway