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

  • SEARCH
  • Home
  • 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 8148491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:39:23+00:00 2026-06-06T14:39:23+00:00

I asked a similar question yesterday that was answered correctly. SQL requiring self join

  • 0

I asked a similar question yesterday that was answered correctly. SQL requiring self join and ranking
Although this question is similar, I’m unable to adapt the answer to fit.

I have a table of football results:

    CREATE TABLE matches(
    season NUMBER(4),
    matchDate DATE,
    homeTeam VARCHAR2(25),
    awayTeam VARCHAR2(25),
    homeGoals NUMBER(2),
    awayGoals NUMBER(2),
    totalGoals NUMBER(3));

INSERT statements can be found here: https://gist.github.com/2941229

I wish to calculate a value for totalGoals as follows. It is the total number of goals scored (homeGoals+awayGoals) in the 5 most recent matches played by the current home team added to the number of goals scored in the 5 most recent matches played by the current away team BEFORE the current match. It must NOT include the goals from the current match.

The difference with yesterday’s question was that it only counted previous matches where the home team played at home and the away team played away. With this question, it doesn’t matter if the home team previously played at home or away. They just had to be one of the teams in that previous match.

As before, only matches from the current season should be used, and if either team has not played 5 matches in the season, then totalGoals should remain NULL.

I can do this using PL/SQL, but I’d much prefer a SQL query. Yesterday’s solution was about a thousand times faster than my PL/SQL.

Thanks
Tams

  • 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-06-06T14:39:24+00:00Added an answer on June 6, 2026 at 2:39 pm

    Essentially you need to to be able introduce an ‘or’ of some kind to the home/away team calculations, but you can’t do that (as far as I know) inside the analytic functions. It looks like you need to create an intermediate table (which exists only during the query, so more a view I guess) that has an additional calculated column with either team name. You can use unpivot for that, but you lose the original columns, which you may still need. So you can fall back to a pre-11g psuedo-unpivot, something like:

    select season, matchdate, hometeam, awayteam, homegoals, awaygoals,
        case when rn = 1 then hometeam else awayteam end as anyteam
    from matches
    cross join (select level as rn from dual connect by level <= 2)
    

    That will give you two rows for every one in your original table. You should be able to use that as a(nother) subquery in place of matches in yesterday’s answer, with some tweaking of the analytics clauses, and something to remove duplicates.

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

Sidebar

Related Questions

This one is similar to a question that I asked yesterday. However my concern
This question is about a similar javascript scope problem that I asked yesterday .
I asked a similar question yesterday, but recognize that i need to rephase it
I asked a similar question like this yesterday but after waiting for ever I
I asked a similar question yesterday that was specific to a technology, but now
Its similar question that I asked yesterday here with a slight modification. Here are
I asked a similar question yesterday but I included some code that basically took
This is similar to a question I asked yesterday but more specific to the
I asked a similar question about this previously, but I did not specify that
Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:

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.