calculation based on columns in row

Zaran 21 Reputation points
2021-02-07T02:43:54.44+00:00

Hi,

Could you please help me with the below result?
I have the below table and looking for the below result.

create table test(Name1 varchar(10),Name2 varchar(10),Color varchar(10),ID_Test float)

insert into test
(' ',' ','Brown',' ')
,(' ',' ','Red',1)
,('Alice',' ',' ',' ')
,('Fred','Bron ',' ',' ')
,('Fred','Rose',' ',111)
,('Pop','Bin ',' ',10)
,('Blace','Bin ',' ',0)

result:
value1= #distinct each column
valu2= # blank for each coulmn/total records

I need to have the below result:

column_name, value1, value2

Name1, 5, 0.28 --- means 2/7=0.28
Name2, 4, 0.42 ---means 3/7=0.42
Color, 3, 0.71 --- means 5/7=0.71
ID_Test, 5, 0.42 --- means 3/7=0.42

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Nasreen Akter 10,801 Reputation points
    2021-02-07T17:07:25.257+00:00

    Hi @Zaran ,

    Please try the following. Thanks!

    CREATE TABLE test(Name1 VARCHAR(10),Name2 VARCHAR(10), Color VARCHAR(10), ID_Test VARCHAR(10))  
          
    INSERT INTO test VALUES  
    (' ',' ','Brown',' ')  
    ,(' ',' ','Red','1')  
    ,('Alice',' ',' ',' ')  
    ,('Fred','Bron ',' ',' ')  
    ,('Fred','Rose',' ','111')  
    ,('Pop','Bin ',' ','10')  
    ,('Blace','Bin ',' ','0')  
      
      
    DECLARE @ColumnTable TABLE (id INT IDENTITY, column_name VARCHAR(100))  
    DECLARE @DestinationTable TABLE (id INT IDENTITY, column_name VARCHAR(100), value1 INT, value2 DECIMAL(10,2))  
      
    INSERT INTO @ColumnTable (column_name)  
    SELECT COLUMN_NAME  
    FROM INFORMATION_SCHEMA.COLUMNS  
    WHERE TABLE_NAME = 'test'      
    
    DECLARE @columnName VARCHAR(100)  
    DECLARE @id AS INT  
    DECLARE @sqlText VARCHAR(1000);       
      
    WHILE EXISTS (SELECT * FROM @ColumnTable)  
     BEGIN   
     SELECT TOP 1 @columnName =  column_name, @id = id FROM @ColumnTable  
     SET @sqlText = 'SELECT '''+ @columnName + ''' AS column_name, COUNT(DISTINCT ' + @columnName + ' ) AS value1, CAST(SUM(CASE WHEN ' + @columnName +   
    ' = NULL OR TRIM( '+ @columnName + ' ) = '''' THEN 1 ELSE 0 END) * 1.0 / COUNT(*)  AS DECIMAL(10,2)) AS value2 FROM test'  
      
     INSERT INTO @DestinationTable  
     Exec (@sqlText)  
         DELETE FROM @ColumnTable WHERE id = @id  
     END  
      
    SELECT * FROM @DestinationTable  
    
    2 people found this answer helpful.
    0 comments No comments

  2. Nasreen Akter 10,801 Reputation points
    2021-02-07T04:43:24.803+00:00

    Hi @Zaran ,

    Would you please try the following. Thanks!

    Declare @test Table(Name1 varchar(10),Name2 varchar(10),Color varchar(10),ID_Test varchar(10))  
      
    insert into @test Values  
    (' ',' ','Brown',' ')  
    ,(' ',' ','Red','1')  
    ,('Alice',' ',' ',' ')  
    ,('Fred','Bron ',' ',' ')  
    ,('Fred','Rose',' ','111')  
    ,('Pop','Bin ',' ','10')  
    ,('Blace','Bin ',' ','0')  
      
      
    SELECT '1' AS serial_no, 'Name1' AS column_name, COUNT(DISTINCT Name1) AS value1, CAST(SUM(CASE WHEN Name1 = NULL OR TRIM(Name1) = '' THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS DECIMAL(10,2)) AS value2 FROM @test  
    UNION  
    SELECT '2' AS serial_no, 'Name2' AS column_name, COUNT(DISTINCT Name2) AS value1, CAST(SUM(CASE WHEN Name2 = NULL OR TRIM(Name2) = '' THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS DECIMAL(10,2)) AS value2 FROM @test  
    UNION  
    SELECT '3' AS serial_no, 'Color' AS column_name, COUNT(DISTINCT Color) AS value1, CAST(SUM(CASE WHEN Color = NULL OR TRIM(Color) = '' THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS DECIMAL(10,2)) AS value2 FROM @test  
    UNION  
    SELECT '4' AS serial_no, 'ID_Test' AS column_name, COUNT(DISTINCT ID_Test) AS value1, CAST(SUM(CASE WHEN ID_Test = NULL OR TRIM(ID_Test) = '' THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS DECIMAL(10,2)) AS value2 FROM @test  
    

    64718-query-20210207.jpg

    ----------

    If the above response is helpful, please accept as answer and up-vote it. Thanks!

    1 person found this answer helpful.

  3. EchoLiu-MSFT 14,591 Reputation points
    2021-02-09T07:52:19.807+00:00

    nasreen-akter has provided the latest answer, do you have any updates?

    Echo

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.