Alejandro
Alejandro

Reputation: 1159

sql query group by count

Hello I have a simple table like this:

name date
n1   d1
n1   d1
n2   d1
n3   d1
n1   d2
n2   d2
n2   d2
n1   d3
n1   d3
n1   d3
n4   d3

Where n# is some name and d# is some date. I need the following result:

name  date   number
n1    d1     2
n2    d1     1
n3    d1     1
n1    d2     1
n2    d2     2
n1    d3     3
n4    d3     1

What I'm doing is for each date, I count the number of times n# appears. Then I display for each name and date the number.

How can I do this on SQL? thank you

Upvotes: 0

Views: 51

Answers (2)

Szymon
Szymon

Reputation: 43023

It's just a simple usage of group by

 select name, date, count(*)
 from table1
 group by name, date

Upvotes: 1

Guffa
Guffa

Reputation: 700342

Group on the name and date:

select
  name, date, count(*) as number
from
  SomeTable
group by
  name, date
order by
  name, date

Upvotes: 0

Related Questions