【问题标题】:Get the Count of Concatenating Column in sql获取sql中连接列的计数
【发布时间】:2019-04-14 08:07:29
【问题描述】:

我有连接多个列,我想要连接多少列的计数

查询输出和预期输出是

用于连接

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   |               |
-------------------------

如果我得到的计数绰绰有余

【问题讨论】:

  • 在我的脑海中,您可以使用CASE 语句和子查询来实现此目的。您已经尝试过什么计算?
  • 请注意列数是动态的

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


【解决方案1】:

你可以试试这个。

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

【讨论】:

  • count 而不是 sum,但你打败了我... +1 :-)
  • @ZoharPeled 是的,谢谢
  • 请注意,这些列可能是一个字符串,只是为了理解我将其作为数字给出
  • @RamapriyanC 如果列是字符串dbfiddle.uk/…则可以工作
  • 如果要将空字符串视为 NULL,则必须添加 NULLIF,例如`RTRIM(NULLIF(col1),''))
【解决方案2】:

你可以统计连接字符串中*的个数:

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 
;

当然,如果您的数据包含*,这将返回错误的数字

【讨论】:

  • 如果该列是空的,我需要计为 0,但我得到的是 1
  • 你说的是什么意思?空字符串''?它适用于 D-Shih 的 dbfiddle.uk/…
  • 在最后一个插入列中检查它
  • 插入 T 值 (NULL, '' , NULL , NULL );
  • 嗯,空字符串和NULL是不一样的,当然要计算在内。
【解决方案3】:

在 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

【讨论】:

    【解决方案4】:
     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      相关资源
      最近更新 更多