0

I was having problems in creating counting rows by grouping based on a given field value. For example: I have a Table A structure like this:

+------+------------+
| id   |  Person    |
+------+------------+
| 1    | "Sandy"    |
| 2    | "Piper"    |
| 3    | "Candy"    |
| 4    | "Pendy"    |
+------------+------+

Also I have a Table B structure like this:

+------+------------+---------+
| id   |  Person    |  Point  |
+------+------------+---------+
| 1    | "Sandy"    |  10     |
| 2    | "Piper"    |  20     |
| 3    | "Candy"    |  30     |
| 4    | "Sandy"    |  10     |
| 5    | "Piper"    |  20     |
| 6    | "Zafar"    |  30     |
+------------+------+---------+

And needed a result like:

+------+------------+---------+
| id   |  Person    |  Point  |
+------+------------+---------+
| 1    | "Piper"    |  40     |
| 2    | "Candy"    |  30     |
| 3    | "Zafar"    |  30     |
| 4    | "Sandy"    |  20     |
| 5    | "Pendy"    |   0     |
+------------+------+---------+

I hope the table examples are itself self-explanatory.

3
  • What is field id in result table? Is it the id of table 1 or is it an auto incremented value? Commented Aug 4, 2015 at 11:10
  • Actually the id shouldn't be related, the merge is based on the person name. And the resultant table should contain person from both the tables. I will make an edit more detailed. Commented Aug 4, 2015 at 11:14
  • I don't understand the 'id' column in the result set. What's that? Commented Aug 4, 2015 at 11:26

3 Answers 3

3
SELECT person
     , SUM(point) total 
  FROM 
     ( SELECT person,point FROM table_b
        UNION 
          ALL
       SELECT person,0 FROM table_a
     ) x
 GROUP 
    BY person 
 ORDER 
    BY total DESC;
Sign up to request clarification or add additional context in comments.

Comments

2

It is a simple left join with a group by

select tableA.person, sum(tableB.points) from tableA left join tableB on tableA.person = tableB.person group by tableA.person
union
select tableB.person, sum(tableB.points) from tableB left join tableA on tableA.person = tableB.person  where tableA.id is null group by tableA.person

5 Comments

Actually I need fields from both the tables if you see "Pendy" and "Zafar" are unique for table A and B respectively, won't using a left join cause a problem there??
@mane I have over seen it. It should be a text in your question. See my updated answer.
I tried your answer and it seems to work for the particular problem. But the answer provided by Strawberry seems works too and is clean and less sql as well. But, comparing based on performance which would be better??
@mane I don't know. it depends on the ddl of your tables. How many records you expected in your table. Build test data and try wich one is better
Ya, should do that. Thanks for helpin out.
0

I think below sql useful to you.

select a.id, a.Person,b.total_point  from (
select id, Person from tablea) as a join

(select Person, sum(Point) as total_point from tableb group by person) as b on a.person =b.person

Thank you

1 Comment

I tried your answer but I keep getting error, may be the unclosed ")"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.