Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 330991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:43:58+00:00 2026-05-12T09:43:58+00:00

I’ll try to give this as generically as I can so it’s reusable. I

  • 0

I’ll try to give this as generically as I can so it’s reusable.

I am running a site with a fairly large MySQL database which has grown to need some summary/rollup tables initialized. For the example’s sake, let’s say it’s soccer statistics. Since I handle multiple soccer leagues in the same database, many of them play games of different lengths – for instance, indoor soccer leagues play four quarters while most outdoor leagues play halves.

I have three tables important to this exercise. I’ve redacted all of the fields that I don’t consider significant to the answer I’m looking for.

GAME
`game`.id
`game`.home_team_id
`game`.away_team_id
`game`.number_of_periods

GOAL 
// Records for each goal scored in the game   
`goal`.id
`goal`.game_id
`goal`.team_id
`goal`.period_number
`goal`.player_id
`goal`.assist_player_id

PERIOD_SUMMARY
`period`.id
`period`.game_id
`period`.team_id
`period`.number
`period`.goals_scored    

Ultimately I should have records for EVERY period played in the period summary table, regardless of whether or not a goal was scored. This table only needs to be initialized once, as it’s fairly easy to add the appropriate zero-filled records via a trigger on game creation and fire on insert/update requests to update the period_summary table.

It is also fairly easy for me to group all of the goals and initialize the period summary table with the SUM(), what I am having a bit of trouble figuring out an efficient way to “fill” any periods that don’t have a goal scored with a 0.

What I am trying to figure out is if it’s easier/more efficient to:

  1. Write the trigger and prefill the entire period_summary table with 0-filled values, then run the query I already know to update the appropriate records for periods in which goals were scored.
  2. Use some other method (perhaps a temporary stored procedure?) that will only 0-fill records where there is not a match in the goals table.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:43:59+00:00Added an answer on May 12, 2026 at 9:43 am

    You already have a placeholder. The “placeholder for unknown data” in SQL is null.

    You don’t need to pre-fill anything: either you have a row with some columns having an unknown value (null), or you have no row at all, so that doing an outer join will get a row that is all null. Either way, the attribute data (essentially, non-id fields) will be null.

    And the sum() aggregate will ignore nulls.

    So let’s say that you do have a row for a game (since it’s pre-scheduled), but no corresponding rows for its periods (since they have not yet been played). Then you do an outer join form game to period (outer, so that you include both games with and games without, period data):

    select a.*, sum(b.goals_scored)
    from game a left outer join period b on (b.game_id = a.id)
    group by a.id;
    

    This shows you the total goals (for both teams) by game; for games with no periods, you get back null (which means in SQL, “we don’t (yet) know”)

    This query shows you only the total goals for completed games and games in progress (games for which at least one period has been played):

    select a.*, sum(b.goals_scored)
    from game a join period b on (b.game_id = a.id)
    group by a.id;
    

    This view filters out incomplete games (assuming you always add early periods before later ones) :

    create view complete_games as
    select a.* from games a
    where exists (select * from period b 
    where b.game_id = a.id and b.number = a.number_of_periods)
    

    Using that view, we can then sum only completed games:

    select a.*, sum(b.goals_scored)
    from complete_games a join period b on (b.game_id = a.id)
    group by a.id;
    

    So, no need to pre-fill, no need for a trigger, most importantly, no need to add false data (claiming zero goals when in fact the period has not yet been played), no need to update with correct data. Just insert the period when you have data for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.