Count the number of columns that has a true value then divide it to the total number of columns

Jean2020

Lets assume that the table below is called Table

    **---------------------------------------------
    ID    Col1    Col2    Col3    Col4  ...  Total
    --------------------------------------------
    1      1       0      NULL     1          30.33
    2      0       1       1       1          60.12
    3      1       1       0       0          20.12
    4      1       0       1       1          60.12
    5      0      NULL    NULL     1          10.19
    6      1       1      NULL     1          90.00   
    7      0       0      NULL     0          0.00
    --------------------------------------------**

I wanted to count and get the average number of columns that has a "true" in it. And display the total average of it in the Total Columns. For example there are 10 columns and 5 columns are true so I divide it and got 50% in total. Assuming that all of the columns that I will counting are bit and has a value of null,0 and 1. How do I achieve this one?

Tim Biegeleisen

You could use:

SELECT
    ID,
    100.0*(COALESCE(Col1, 0) + COALESCE(Col2, 0) + ... + COALESCE(Col10, 0)) / 10 AS pct
FROM yourTable;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to count the number of columns with a value on each row in python?

Count number of columns by row that exceed a value in dataframe

Count number of columns in pyspark Dataframe?

Python Pandas Count Number of DataFrame Columns in which a Particular Value Occurs

PostgreSQL: Count number of columns in a row that contain a value

Count the number of columns in a row with a specific value

How to count the number of columns with a single value?

count number of rows where value in two columns are both true

Count the number of "columns" in an array (php)

How to divide a rectangle in specific number of rows and columns?

How can I count the number of columns a value has after a Where?

total number of columns in a matrix having at least 1 negative value

R - Count number of "True" value in matrix columns and assign "False"

Count number of columns having zeros

Count number of columns in array php?

How to get count of particular column value from total number of records and display difference in two different columns in SQL Server

Count the number of columns in an array with PowerShell

query to count the number of values in a columns

COUNT number of NULLs in multiple columns

How to divide a number into multiple columns or rows in excel?

count a character in each row and divide it into the number of columns

Count number of distinct columns in SQL

Number of Columns a DataRowView has?

PostgreSQL: Count Number of Occurrences in Columns

Count number of columns above a date

Divide multiple columns by a fix number in pandas

Google Sheets - IF columns contains text and another columns contains number return total value of number column

How to Count The Number Of Columns Of An AdaptativeGridView

Count number of rows where a value appears in any of two columns in R