获取SQL中的连接列数

时间:2018-11-11 10:53:23

标签: c# sql .net sql-server model-view-controller

我具有串联多个列,并且我想要计数串联的列数

查询输出和预期输出为

用于串联

select  ConcateColumn = STUFF(
                              COALESCE('* ' + RTRIM(col1),'') 
                             +COALESCE('* ' + RTRIM(col2),'') 
                             +COALESCE('* ' + RTRIM(col4),'') 
                             +COALESCE('* ' + RTRIM(col3),'') 
                       , 1, 2, '') 

===================================
| col1   | col2  |  col3  |  col4 | 
===================================
|  1     | 2     |  NULL  | NULL  |
|  NULL  | NULL  |  NULL  | NULL  |
|  1     | NULL  |  NULL  | NULL  |
|  NULL  | 2     |  3     | 4     |
|        | NULL  |  NULL  | NULL  |       
==================================

结果输出

 ==============
|ConcateColumn|
 ==============
|1*2          |
|NULL         |
|1            |
|2*3*4        |
|             |
===============

预期输出

------------------------
| Count | ConcateColumn |
-------------------------
|   2   |     1*2       |
|   0   |     NULL      |
|   1   |     1         |
|   3   |     2*3*4     |
|   0   |               |
-------------------------

如果我得到的数量足够多

4 个答案:

答案 0 :(得分:2)

您可以尝试一下。

select  (SELECT count(NULLIF(val,''))
        FROM   (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
          ConcateColumn = STUFF(
                              COALESCE('* ' + NULLIF(RTRIM(col1),''),'') 
                             +COALESCE('* ' + NULLIF(RTRIM(col2),''),'') 
                             +COALESCE('* ' + NULLIF(RTRIM(col3),''),'') 
                             +COALESCE('* ' + NULLIF(RTRIM(col4),''),'') 
                       , 1, 2, '')
FROM T

如果您想不使用NULL''进行计数,则可以尝试使用NULLIF函数。

sqlfiddle

答案 1 :(得分:1)

您可以计算串联字符串中的*数:

with cte as 
 (
   select  
      ConcateColumn = STUFF(  COALESCE('* ' + NULLIF(RTRIM(col1),''),'') 
                             +COALESCE('* ' + NULLIF(RTRIM(col2),''),'') 
                             +COALESCE('* ' + NULLIF(RTRIM(col3),''),'') 
                             +COALESCE('* ' + NULLIF(RTRIM(col4),''),'') 
                       , 1, 2, '') 
   FROM T
 )
select ConcateColumn,
   -- how many '*' have been removed?
   coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte 
;

当然,如果您的数据包含*

,这将返回错误的数字

答案 2 :(得分:1)

在SQL Server 2017+中,您可以简单地执行以下操作:

select v.*
from t cross apply
     (select count(*) as cnt,
             string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
      from (values (1, col1), (2, col2), (3, col4), (4, col3)
           ) v(col, ord)
      where col is not null
     ) v;

在早期版本中,我可能会遵循与您相同的结构:

select ( (case when col1 is not null then 1 else 0 end) +
         (case when col2 is not null then 1 else 0 end) +
         (case when col4 is not null then 1 else 0 end) +
         (case when col3 is not null then 1 else 0 end)
       ) as cnt

答案 3 :(得分:0)

 select sum(
case when col1 = '' then 0 when col1 is null then 0 else 1 end +
case when col2 = '' then 0 when col2 is null then 0 else 1 end + 
case when col3 = '' then 0 when col3 is null then 0 else 1 end +
case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
      ConcateColumn = STUFF(
                          COALESCE('* ' + RTRIM(col1),'') 
                         +COALESCE('* ' + RTRIM(col2),'') 
                         +COALESCE('* ' + RTRIM(col4),'') 
                         +COALESCE('* ' + RTRIM(col3),'') 
                   , 1, 2, '') from temp1
                   group by col1,col2,col3,col4