Khalil Rumman Khalil Rumman - 2 years ago 82
SQL Question

SQL Counting Columns

I've the following set of data

id name in out
1 jhon True False
1 jhon True False
1 jhon True True
1 jhon True True
2 Smith True True

I want to get the count result as

jhon = 2
smith = 1

I need to count only the rows that contain true in both (in, out)

how I can accomplish this? I'm still newbie and it only works for me to count all the rows in standards sql

Answer Source

This is a simple group by:

select name, count(*)
from the_table
where in = 'True' 
  and out = 'True'
group by name;

You might need to properly quote the keywords in and out (e.g "in" and "out"), not sure if they are reserved keywords in SQL Server.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download