计算并获取单行中非空列的平均值

时间:2016-07-11 11:03:42

标签: sql linq sql-server-2008

我想计算并计算每行中非空列的平均值 例如,我有一个像这样的表

Name | a  | b  | c  | d  | e   | f |
     |    |    |    |    |     |   |
umar | 2  |null| 3  | 5  | null| 4 |
ali  |null|null| 3  |null| 1   | 4 |
ali  |null|null| 3  |null| null| 4 |

结果应该是

    Name | a  | b  | c  | d  | e   | f | average
         |    |    |    |    |     |   |
    umar | 2  |null| 3  | 5  | null| 4 |  3.5
    ali  |null|null| 3  |null| 1   | 4 |  2.66
    ali  |null|null| 3  |null| null| 4 | 3.5

5 个答案:

答案 0 :(得分:4)

您可以使用case表达式:

select ((case when a is not null then 1 else 0 end) +
        (case when b is not null then 1 else 0 end) +
        (case when c is not null then 1 else 0 end) +
        (case when d is not null then 1 else 0 end) +
        (case when e is not null then 1 else 0 end) +
        (case when f is not null then 1 else 0 end)
       ) as NumNotNull

答案 1 :(得分:4)

var query = from x in table
            select new 
            {
                CountNotNull = (x.a ?? 0) + (x.b ?? 0) + (x.c ?? 0) + (x.d ?? 0) + (x.e ?? 0) + (x.f ?? 0) 
            };

如果您不想计算它但计算平均值(您已经更改了问题):

var query = 
     from x in table
     let NotNullColCount = (x.a == null ? 0 : 1) + (x.b == null ? 0 : 1) + (x.c == null ? 0 : 1) + (x.d == null ? 0 : 1) + (x.e == null ? 0 : 1) + (x.f == null ? 0 : 1)
     let NotNullColSum = (x.a ?? 0) + (x.b ?? 0) + (x.c ?? 0) + (x.d ?? 0) + (x.e ?? 0) + (x.f ?? 0)
     select new 
     {
        AverageNotNull = NotNullColCount == 0 
                         ? 0.0 
                         : 1.0 * NotNullColSum / NotNullColCount 
     };

答案 2 :(得分:2)

您可以使用CASE EXPRESSION

SELECT t.name,
       (CASE WHEN t.a is not null then 1 else 0 END +
       CASE WHEN t.b is not null then 1 else 0 END +
       CASE WHEN t.c is not null then 1 else 0 END +
       CASE WHEN t.d is not null then 1 else 0 END +
       CASE WHEN t.e is not null then 1 else 0 END +
       CASE WHEN t.f is not null then 1 else 0 END) as not_null_count
FROM YourTable t

答案 3 :(得分:0)

如果您的列数很大,您也可以使用动态查询

DECLARE @SQL NVARCHAR(max);
SET @SQL ='SELECT Tablename.Columnname,'+     STUFF((选择'+ CASE WHEN'+ QUOTENAME(COLUMN_NAME)+         '过去是1,过了0结束'         来自INFORMATION_SCHEMA.COLUMNS         WHERE TABLE_NAME ='Tablename'FOR XML PATH('')),1,1,'')     +'FROM Tablename';
EXEC(@SQL);

答案 4 :(得分:0)

SQL下面可能会帮助你:

SELECT  Name
      , a
      , b
      , c
      , d
      , e
      , f
      , CONVERT(FLOAT, ( tab.a1 + tab.b1 + tab.c1 + tab.d1 + tab.e1 + tab.f1 )) / CONVERT(FLOAT, ( tab.a2 + tab.b2
                                                                                                   + tab.c2 + tab.d2
                                                                                                   + tab.e2 + tab.f2 )) AS [average]
FROM    ( SELECT    Name
                  , a
                  , b
                  , c
                  , d
                  , e
                  , f
                  , CASE WHEN a IS NULL THEN 0
                         ELSE a
                    END AS a1
                  , CASE WHEN b IS NULL THEN 0
                         ELSE b
                    END AS b1
                  , CASE WHEN c IS NULL THEN 0
                         ELSE c
                    END AS c1
                  , CASE WHEN d IS NULL THEN 0
                         ELSE d
                    END AS d1
                  , CASE WHEN e IS NULL THEN 0
                         ELSE e
                    END AS e1
                  , CASE WHEN f IS NULL THEN 0
                         ELSE f
                    END AS f1
                  , CASE WHEN a IS NULL THEN 0
                         ELSE 1
                    END AS a2
                  , CASE WHEN b IS NULL THEN 0
                         ELSE 1
                    END AS b2
                  , CASE WHEN c IS NULL THEN 0
                         ELSE 1
                    END AS c2
                  , CASE WHEN d IS NULL THEN 0
                         ELSE 1
                    END AS d2
                  , CASE WHEN e IS NULL THEN 0
                         ELSE 1
                    END AS e2
                  , CASE WHEN f IS NULL THEN 0
                         ELSE 1
                    END AS f2
          FROM      YourTable
        ) tab