Piduna
Piduna

Reputation: 175

How to calculate rows in sql

I have a table:

+---------+-----------+--------------+
|  acc_id |    dns_id | mail_name    | 
+---------+-----------+--------------+
|        1|         5 | myac1        | 
|        2|         5 | myac2        | 
+---------+-----------+--------------+-

I showed to you two records. I have a lot of acc_id, but I have the same dns_id for a lot of this acc_id. How can I calculate how many acc_id in the same dns_id. I have not only dns_id = 5, I have other values. How can I calculate for each dns_id number of `acc_id'?

According to my table, I have two acc_id in one dns_id.

Upvotes: 0

Views: 56

Answers (2)

marcolz
marcolz

Reputation: 2970

Try GROUP BY and count():

SELECT dns_id, COUNT(*) FROM table GROUP BY dns_id;

It groups the results by different dns_id and counts the number of rows that were grouped.

Upvotes: 1

Tiago Luna
Tiago Luna

Reputation: 111

You can use count(acc_id) in the select statement and a group by with the column dns_id. It would look something like this::

SELECT dns_id, count(acc_id) FROM [TABLE_NAME]
GROUP BY dns_id

Upvotes: 1

Related Questions