0

I have this table called, ACTIVITIES:

Name      Activity     Minutes
JOSE      videogames      50
MARISSA   videogames      50
TINA      CHESS           90
JAKE      CHESS           95

For each activity in the table, how do I make a count of how many persons did that activity? I would like to print the activity, the count, and the sum of the minutes in my query.

I tried working by parts & this was my attempt:

SELECT Distinct(Activity), count(Activity) as Total
FROM ACTIVITIES;

However, the query result gave me an error saying: "not a single-group group function"

1
  • since you need the totals by activity, add GROUP BY Activity, and you don't need DISTINCT(Activity); so try this: SELECT Activity, count(Activity), sum(NUMOFMINUTES) FROM Activities GROUP BY Activity Commented Dec 8, 2014 at 4:15

4 Answers 4

1

You can use a group by clause to first group the table by activity then do the count and the sum. For example:

SELECT count(*), sum(NUMOFMINUTES), Activity 
FROM activities
GROUP BY activity
Sign up to request clarification or add additional context in comments.

Comments

0

Use group by to get the count/sum per activity like so:

select activity, count (activity) as totalcount, sum(minutes) as totalminutes
from activities
group by activity

If you want to get some aggregate value such as a count of a specific value, you first need to group the data into subsets based on the distinct values of that column. That is what that error message was referring to. Note that when you group by activity, the records will consider and display only the distinct values of activity, so you won't need to use the distinct keyword.

Comments

0

TRY this SELECT DISTINCT(Activity),COUNT(Activity) AS Total FROM ACTIVITIES GROUP BY Activity

Comments

0

Whenever you use any agreegate function and retrieves another field with it on which you are not performing agreegation then you have to use group by clause with non-agreegate field. so add 'group by Activity' at the end of your query.

1 Comment

Not exactly. You can do select count(*) from tablename and it will run just fine. group by is required when you want to drill down by specific values.

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.