Skip to main content
November 24, 2006
Question

Query

  • November 24, 2006
  • 1 reply
  • 769 views
Hello. How do I write the query for:

SELECT * FROM shirt;
+----+---------+--------+-------+
| id | name | num1| num2 |
+----+---------+--------+-------+
| 1 | nancy | 2 | 2 |
| 1 | bill | 1 | 4 |
| 2 | nancy | 3 | 16 |
| 2| bill | 7 | 2 |
| 2| steve | 7 | 6 |
| 3 | pam |8 | 7 |
| 3 | nancy | 1 | 8 |
| 3 | bill | 2 | 7 |
+----+---------+--------+-------+

Which will return this:

+----+---------+--------+-------+
| count | name | num1| num2 |
+----+---------+--------+-------+
| 3 | nancy | 6 | 26 |
| 3 | bill | 10 | 13 |
| 1 | steve | 7 | 6 |
| 1| pam | 8 | 7 |
+----+---------+--------+-------+

Primary Key (ID, unique)

I don't know if I've written everything correctly. I need to count the occurrance of ID for each Name and then show the sum of the values in Num1 and Num2 for each name.

Can anyone help me?

Thank you so much.
This topic has been closed for replies.

1 reply

Inspiring
November 24, 2006
.oO(Anhialator)

> I don't know if I've written everything correctly. I need to count the
>occurrance of ID for each Name and then show the sum of the values in Num1 and
>Num2 for each name.

Try

SELECT
COUNT(*) AS count,
name,
SUM(num1) AS num1,
SUM(num2) AS num2
FROM shirt
GROUP BY name

Micha
November 24, 2006


Micha! THANK YOU! It worked! I'm soooooooooo happy! You're brilliant, and you make it look so easy, which for you, it was, I'm sure!
Thank you!