nav100
nav100

Reputation: 3133

SQL Query group by and count

I have the following SQL query and I would like to get the count of rows group by column.

  SELECT COL1, COL2
  FROM TABLE-A 
  GROUP BY COL1, COL2

Here is the result set I am getting for the above query. Thank you for any suggestion.

Col1            Col2
-------------------
Atlanta         122
Atlanta         133
Atlanta         323
Boston          44
Boston          99
LA              3323

Here is the result set I need help with the query.

Col1            Col2
-------------------
Atlanta         3
Boston          2
LA              1

Upvotes: 0

Views: 104

Answers (2)

Sachin
Sachin

Reputation: 40970

Try this

SELECT COL1,COUNT(COL1) as Col2
FROM TABLE-A 
GROUP BY COL1

Upvotes: 1

Luis Tellez
Luis Tellez

Reputation: 2973

you just need to use the aggregate function and dont group by the one you need to count, in this case col2

 SELECT COL1, count(COL2)
  FROM TABLE-A 
  GROUP BY COL1

Upvotes: 3

Related Questions