How to count based on multiple columns in SQL Server?

Ranjith K Avarachan

I have columns in a table like this:

race  | active
===============
asian | false
black | true 
asian | false
white | true 
asian | false
black | false
asian | false
white | false
asian | false
black | true 
black | true 
asian | false
white | true 
asian | false
black | false

I need to calculate the count like

 white-active : 2
 asian-active : 
 black-active : 
 asian-inactive : 
 white-inactive : 
 black-inactive : 

How can I achieve this in SQL Server 2008?

Nguyễn Hải Triều

You can try this code:

SELECT race + '-active : ' + CAST(count(active) OVER(PARTITION BY race) AS VARCHAR) output
FROM Your_Table WHERE active='true'
union all
SELECT race + '-inactive : ' + CAST(count(active) OVER(PARTITION BY race) AS VARCHAR) output
FROM Your_Table WHERE active='false'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count Based on Columns in SQL Server

SQL: count based on multiple columns

SQL Server : how to combine multiple rows based on two different columns?

How to count unique dates based on multiple or columns

SQL Server 2008, Pivot based on multiple columns

sql how to get a count based on two columns

How to count multiple columns in SQL (Oracle) with criteria?

How to get distinct count of values of all the columns of a table based on where condition in sql server?

SQL Count multiple columns

Count distinct based on multiple columns

How to drop multiple columns in SQL Server

How to group by multiple columns in SQL Server

How to pivot on SQL-Server multiple columns?

How to PIVOT multiple columns using SQL Server

How to pivot using multiple columns in SQL Server?

How to get percentage count based on multiple columns in pandas dataframe?

Splitting a column into multiple columns based on the text in SQL Server

SQL Server CTE result with except based on multiple columns

Sum a Single column into multiple columns based on criteria SQL Server

How to create a columns based on other columns SQL Server 2012

Count by two columns in SQL Server?

Count multiple Columns in Oracle SQL

SQL Distinct Count on Multiple Columns

SQL GROUP BY and COUNT with multiple columns

SQL To count values of multiple columns

Groupby multiple columns with count in sql

How to group by 3 columns -and- get a count in SQL Server?

How to COUNT DISTINCT on basis of Selective columns SQL Server

How to count the number of values in a table with 2 columns as the parameter on SQL Server?