在MSSQL中连续打印不为null的值. [英] Print not null values in a row in MSSQL..

查看:76
本文介绍了在MSSQL中连续打印不为null的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的桌子..

I have a table like this..

Column1   Column2   Column3
-------   -------   --------
 1         NULL      NULL
 NULL       2        NULL
 NULL      NULL       3




我想要这样的输出..




I want to get an out put like this..

Column1   Column2   Column3
----------------------------
 1         2           3



请帮助我...我正在使用MSSQL 2008..



Please help me...I am using MSSQL 2008..

推荐答案

这根本是不可能的,因为您正在组合应该彼此独立的行字段.您不能假设任何排序顺序,并且需要有关每一行内容的信息.而且,您现在在其他不相关的数据之间也具有某种关系.例如,您假设:
1 NULL NULL
NULL 2 NULL
NULL NULL 3
4 NULL NULL

但这也很可能是:
NULL NULL 3
4 NULL NULL
1 NULL NULL
NULL 2 NULL

这样会产生理想的结果吗?

您似乎有2个选择:
首先是一个简单的解决方案,通过简单地执行3个select操作即可获得某些结果,甚至看起来像您想要的结果,每个操作都会获取column1,column2和column3的值.之后,您可以将这些结果合并以获得最终结果.

其次,看起来您正在将行合并为不应该的列.就像您为每个选项创建了单独的列,但有一个约束将所有其他选项排除在外.示例列:男性,女性,性别未知

也许是阅读规范化的好时机:
http://msdn.microsoft.com/en-us/library/aa933055%28v = sql.80%29.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/aa291817%28v = vs.71%29.aspx [ ^ ]

祝你好运!
This is simply impossible because you are combining fields of rows that should be independent of each other. You cannot assume any sort order and need information about the contents of each row. Also, you now have some relation between otherwise unrelated data. For example, you assume:
1 NULL NULL
NULL 2 NULL
NULL NULL 3
4 NULL NULL

But it could also easily be:
NULL NULL 3
4 NULL NULL
1 NULL NULL
NULL 2 NULL

Would this yield a desired result?

It looks like you have 2 options:
First is a simple solution to get some result that might even look what you want by simply doing 3 select operations, each to get values for column1, column2 and column3. After that you combine those results to get the end result.

Second, it looks like you are combining columns in a row that shouldn''t. It is like you created a separate column for each option but with a constraint that excludes all other options. Example columns: Male, Female, Unknown gender

Maybe a good time to read up on normalization:
http://msdn.microsoft.com/en-us/library/aa933055%28v=sql.80%29.aspx[^]
http://msdn.microsoft.com/en-us/library/aa291817%28v=vs.71%29.aspx[^]

Good luck!


它的简单朋友,

使用Max()会起作用.

例如:-选择Column1 = Max(Column1),Column2 = Max(Column2),Column3 = Max(Column3)
Its Simple friend,

Use Max() It will work.

Eg.:- Select Column1=Max(Column1),Column2=Max(Column2),Column3=Max(Column3)


这篇关于在MSSQL中连续打印不为null的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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