2

I need help with a SQL statement. The goal is to count the amount of alarms of each date. My table looks something like this:

|----DATE----|---COUNTER---|---ALARM_ID---|
|2012-01-01  |     30      |      1       |
|2012-01-01  |     20      |      2       |
|2012-01-01  |     10      |      3       |
|2012-01-02  |     5       |      1       |
|2012-01-02  |     25      |      2       |
|2012-01-02  |     12      |      3       |
|2012-01-03  |     33      |      1       |
|2012-01-03  |     43      |      2       |
|2012-01-03  |     11      |      3       |

And I'm looking for a SQL statement that gives this result:

|----DATE----|---COUNTER---|
|2012-01-01  |     60      |
|2012-01-02  |     42      |
|2012-01-03  |     87      |

I've been working on this SELECT date, SUM(counter) FROM all_stats but all I get is:

|----DATE----|---COUNTER---|
|2012-01-01  |     60      |

Do I have to create a loop to go through all dates and count?

Thanks in advance, Steve-O

3 Answers 3

2
SELECT date, SUM(counter)
FROM all_stats
GROUP BY date
Sign up to request clarification or add additional context in comments.

Comments

1

Try this instead

SELECT date, SUM(counter) FROM all_stats GROUP BY date;

"GROUP BY date" puts all the individual dates on a separate line and does the sum separately per date.

Comments

1
select date, sum(counter)
from all_stats
group by date

Comments

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.