r/learnSQL • • Aug 01 '26

I need help creating a league table

So for a bit of context I have a football score prediction league where everyweek a number of players will tell me home_goals, away_goals, goal_scorer and this will be compared to the actual result of a Norwich City match. This is played over 46 games.

There is 3 points for the exact scoreline, 1 point for the correct result and 1 point for the correct goal scorer.

I can manually input everyones prediction and the result from Norwich each week.

But my problem is I am absolutely stuck on where or how to start to auto-populate or create the query to compare the results and give me an updated table after every game week.

Last season I made a python programme where I could semi easily set up the maths to accumulate over the season. Is this something that can be done on SQL or am I trying to do something that sql just isn't capable of doing?

Please any tips would be much appreciated. Oh and I am using Postgresql

1 Upvotes

17 comments sorted by

View all comments

Show parent comment

2

u/__agletesque Aug 01 '26

My initial idea was something like user DatabaseSpace suggested (I didn't mention anything about players table because if you kept track of goalscorers for both sides (what about own goals) you'd have to enter a lot of records into players table), so definitely separated (given the sub, normalized tables is the answer you'll get, and that's approach I'd take).

If you had one table for result and goalscorers, would you have reserved like 5+ columns for each week for goalscorers or would you alter table every time you need another column to put in another goalscorer. Also this approach would make it impossible to compare predicted data and goalscorers data, unless fixed number of goalscorers for each week, but don't do that.

Filtering tables is easy if you're worried about finding id to enter, just search about where clauses, and most likely like operator (and wildcard) or even regex, but I doubt that you'd need it.

Yeah, didn't mention for result, but I also agree with aforementioned user, go with view (prepared query).

1

u/AdvertisingOne7942 Aug 01 '26

For the goal scorers it is only the scorers for Norwich or No goal scorer not own-goals or the opposition team so it is limited to maybe 10-20 possibilities.

The way my brain worked was to put like 5 different columns but only the first column would be needed if it was say 1-0 or 0-0 then the other columns would be NULL I just assumed I could say if the players gw1_goal_scorer is equal to gw1_norwich_goal_scorer_1 or gw1_norwich_goal_scorer_2 etc then return 1 if not return 0.

2

u/__agletesque Aug 02 '26

Technically you can do whatever you want, but it should go to another table (relation 1:n). If there are more than 5 different goalscorers, you'd have to alter the table and alter the view accordingly. If it's in another table, you just add another row for each different goalscorer.

Just of curiosity, if they guessed 0 goals for Norwich, do they get another point if no Norwich player scored or if they didn't predict goalscorer and there was only own goal(s) or no goals for Norwich?

I'd probay add status for each match and in a view add condition where "match"."status" = '{id of status finished or simply string}', as I assume you'd be inserting predictions before the match had been played.