且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在MSSQL中连续打印不为null的值.

更新时间:2023-12-06 15:10:16

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