Jump to content

Group By Year, Name, and Count from Two Fields on Same Row


Go to solution Solved by Barand,

Recommended Posts

Alright, I've been doing this for too long to not understand what is going wrong here. This is pretty straightforward. I have one table with the columns...

 

ID | Year | Away_Team | Home_Team

-1---2010-----tacoma--------puyallup

-2---2010-----puyallup-------renton

-3---2010-----renton---------tacoma

-4---2010-----puyallup-------tacoma

 

What I want is just a simple count of the total # of games played by year and team regardless of it being an away or home game.

 

Year | Team | Games

-2010--tacoma---3

-2010--renton----2

 

Any help will be appreciated. Thanks

  • Solution

You need to use a UNION

SELECT Year
, Team
, COUNT(*) as Played
FROM (
    SELECT Year, Away_Team as Team
    FROM table
    UNION
    SELECT Year, Home_Team as Team
    FROM table
    ) teams
GROUP BY Year, Team
Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.