如何使用sql组合两列 [英] How do combine two column using sql

查看:64
本文介绍了如何使用sql组合两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我必须按以下格式组合栏目





ID  ; 第一个  第二个  第三个

1   aaa   bbb   ccc

2   jina   -         bbb



输出应该是(合并项目数量)

ID  总计

1   aaa,bbb,ccc

2   jina,bbb



想要输出的格式(计算项目数)



ID  总计

1   3

2    2



任何建议请............. ..

解决方案

你有什么尝试?



连接下面的栏目

  SELECT  ID,Column1 +   + Column2 +   + Column3 ... 。

在上面的查询中,您必须检查列值是否为NULL(在您的情况下) - &省略if if返回NULL( - )。为此,您可以使用 CASE [ ^ ]声明。



第二查询你需要找到字符(,)的出现,检查一下

字符出现 [ ^ ]



现在轮到你了,继续吧。自定义你想要的位置。


选择ID,ISNULL(首先,'')+ ISNULL(','+ second,'')+ ISNULL(','+ third,'') AS Total_String,

(第一个为空时的情况,然后是0,否则为1结束)+(第二个为空时的情况,然后为0,否则为1结束)+(第三个为空时的情况为0,否则为1结束)AS Total_Int

Hi
I have to combine column in following format


ID    first    second    third
1    aaa    bbb    ccc
2    jina    -         bbb

The output should be(combine the number of items)
ID    total
1    aaa,bbb,ccc
2    jina,bbb

The want the output also in the format(count the number of items)

ID    total
1    3
2    2

Any suggestions pls................

解决方案

What have you tried?

Concatenate the columns like below

SELECT ID, Column1 + "," + Column2 + "," + Column3 ....

In above query you have to check if the column value is NULL(in your case -) & omit that if returns NULL(-). For that you could use CASE[^] statement.

For second query you need to find the occurence of character(,), check this one
Character Occurrences[^]

Now your turn, go ahead. Customize where you want.


Select ID, ISNULL(first,'')+ISNULL(','+second,'')+ISNULL(','+third,'') AS Total_String,
(Case when first is null then 0 else 1 end)+(Case when second is null then 0 else 1 end)+(Case when third is null then 0 else 1 end) AS Total_Int


这篇关于如何使用sql组合两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆