Michele
Michele

Reputation: 21

Sql count distinct values based on column

I need an help about a sql query. I have a table as the following

error   session
error1     1 
error1     1
error2     1
error1     2
error1     2
error2     2

I have to count errors once related to session. The result should be

error   number of errors     
error1         2
error2         2

Thank you

Upvotes: 1

Views: 4963

Answers (2)

Stefano Zanini
Stefano Zanini

Reputation: 5916

You have to group by the error and count the distinct sessions, and you can do that pretty much repeating these words in a query

select  error, count(distinct session)
from    yourTable
group by error

Upvotes: 1

India.Rocket
India.Rocket

Reputation: 1245

Use aggregated function count and group by to do this.

Try this:-

Select error, count(distinct session) as number_of_errors
from
your_table_name
group by error

Upvotes: 4

Related Questions