5

Overview

I have a bunch of data on a competition I'm holding and I want to present it in a better format.

There's 4 tables; the first two are self-explanatory, the points and extras table are essentially the exact same thing, they're just stored in different tables with slightly different column names.

Data

    users
    +----+---------------+------+
    | id |     name      | team |
    +----+---------------+------+
    |  1 | John Doe      |    1 |
    |  2 | Jane Lane     |    1 |
    |  3 | Jack Black    |    4 |
    |  4 | Dan Bam       |    3 |
    |  5 | Pam Jan       |    2 |
    |  6 | Pop Tart      |    2 |
    |  7 | John Q        |    1 |
    |  8 | Hugo Strange  |    3 |
    |  9 | Jimmy Neutron |    2 |
    +----+---------------+------+
    teams
    +----+-----------------+
    | id |      name       |
    +----+-----------------+
    |  1 | Team Fun        |
    |  2 | The Dream Team  |
    |  3 | In It To Win It |
    |  4 | Buddies         |
    +----+-----------------+
    points
    +---------+--------+------------+
    | user_id | points |   event    |
    +---------+--------+------------+
    |       1 | 2      | Basketball |
    |       2 | 4      | Basketball |
    |       5 | 1      | Basketball |
    |       8 | 3      | Basketball |
    |       9 | 5      | Basketball |
    |       2 | 8      | Volleyball |
    |       5 | 5.5    | Volleyball |
    |       6 | 6.5    | Volleyball |
    |       7 | 2      | Volleyball |
    |       8 | 4      | Volleyball |
    |       9 | 9.5    | Volleyball |
    |       1 | 2.5    | Dodgeball  |
    |       3 | 3      | Dodgeball  |
    |       4 | 4      | Dodgeball  |
    |       6 | 9      | Dodgeball  |
    |       7 | 2.5    | Dodgeball  |
    |       9 | 3      | Dodgeball  |
    +---------+--------+------------+
    extras
    +---------+--------+---------------------+
    | user_id | points |     description     |
    +---------+--------+---------------------+
    |       1 | 5      | Great Sportsmanship |
    |       3 | 10     | Team Player         |
    |       8 | 5.5    | Most Improved       |
    +---------+--------+---------------------+

What I'm Trying To Do

I want to write a query to return all the events (and "extras") a specific team participated in, the total points from all members of the team, and the participating members in that event.

Example below uses Team Fun (Team 1):

+---------------------+--------+--------------------+------------+
|        event        | points |      members       | members_id |
+---------------------+--------+--------------------+------------+
| Basketball          |      6 | John Doe,Jane Lane | 1,2        |
| Volleyball          |     10 | Jane Lane,John Q   | 2,7        |
| Dodgeball           |      5 | John Doe,John Q    | 1,7        |
| Great Sportsmanship |      5 | John Doe           | 1          |
+---------------------+--------+--------------------+------------+

If anyone could help me with figuring this out, I'd appreciate it!

SQLFiddle

This is a SQLFiddle with the data schema above - http://sqlfiddle.com/#!2/e8f97a

4
  • 2
    +1 for good way to post,.,, You put a lots of effort before asking this.. I am sure.. Commented Oct 3, 2013 at 16:10
  • Show us what you've tried AND provide an sqlfiddle and/or proper DDLs for the above.
    – Strawberry
    Commented Oct 3, 2013 at 16:14
  • Sorry, forgot to add the SQL Fiddle! Here it is: sqlfiddle.com/#!2/e8f97a As for what I've tried, I've done a variety of things with JOINS that always seems to fail. SQL isn't my strongest skill unfortunately
    – Steve
    Commented Oct 3, 2013 at 16:25
  • 1
    @S.K. Use the lc.'s answer, it works very fine!
    – zavg
    Commented Oct 3, 2013 at 16:31

1 Answer 1

1

You can use a UNION to get the extras and points together:

SELECT user_id, points, event
FROM points
UNION ALL
SELECT user_id, points, description AS event
FROM extras

Then using this, you can compile your info with a SUM and a couple of GROUP_CONCATs:

SELECT P.event, SUM(P.points) AS points, 
    GROUP_CONCAT(U.name) AS members, GROUP_CONCAT(U.id) AS members_id
FROM teams T
INNER JOIN users U ON T.id = U.team
INNER JOIN
(
    SELECT user_id, points, event
    FROM points
    UNION ALL
    SELECT user_id, points, description AS event
    FROM extras
) P ON U.id = P.user_id
WHERE T.id = @teamId
GROUP BY P.event

SQL Fiddle Example

5
  • I didn't know about UNIONs, that looks handy! However, I tried your SQL in SQLFiddle and it didn't seem to return properly. sqlfiddle.com/#!2/e8f97a/3 Also just curious, what's the @teamId?
    – Steve
    Commented Oct 3, 2013 at 16:28
  • Yeah, there's something really strange going on in that UNION with the NUMERIC values. I'm trying to figure it out.
    – lc.
    Commented Oct 3, 2013 at 16:32
  • @teamId is just a parameter. You can SET @teamId = 1 to return team 1, as in your example result (or just omit the WHERE if you didn't need it)
    – lc.
    Commented Oct 3, 2013 at 16:33
  • Ah I see! With that, it works perfectly! I accidentally put NUMERIC values for points in my previous SQLFiddle, I changed it to doubles since that's what they are supposed to be. But as you can see from this SQLFiddle, it all works great! sqlfiddle.com/#!2/e8f97a/10 Thanks!
    – Steve
    Commented Oct 3, 2013 at 16:36
  • Specifically declaring them as NUMERIC(5,2) also fixes it. Strange as SELECT * FROM points works but as soon as you put it in a UNION something breaks. sqlfiddle.com/#!2/276e1/1
    – lc.
    Commented Oct 3, 2013 at 16:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.