简体   繁体   English

将 2 列合二为一

[英]Mix 2 columns in one

I have 2 columns filled with different numbers, and I'd like to make a third column relocating the numbers that get repeated.我有 2 列填充了不同的数字,我想在第三列中重新定位重复的数字。 There can also be a NULL in these columns, in that case I'd like to get the other result.这些列中也可以有 NULL,在这种情况下我想得到其他结果。

Here's an example.这是一个例子。

Column A Column B
1 2
2 2
3 NULL
NULL 4
NULL NULL

I's like to join both columns in one (Column C) with the following rules.我想按照以下规则将两列合二为一(C 列)。

Value A != Value B --> A string of characters looking like this '???'
Value A == Value B --> Get Value A or B, doesn't matter

With this result, everything works fine except the first case shown before, where Column A has X value and Column B has Y value.有了这个结果,除了前面显示的第一种情况外,一切正常,其中 A 列有 X 值,B 列有 Y 值。 I can't get the expected '???'我无法得到预期的“???” result that I want.我想要的结果。

Haven't tried using arrays.还没有尝试使用 arrays。

我的代码

So you are using the Field Calculator to compute the values for Column C from the values of Column A and Column B.因此,您正在使用字段计算器根据列 A 和列 B 的值计算列 C 的值。

Note that QGIS Feature attributes have types they could be either integer, float, or text.请注意,QGIS 要素属性的类型可以是 integer、浮点数或文本。 Assuming your Column A and Column B are integers, then Column C would also be an integer column, therefore the value '???'假设您的 A 列和 B 列是整数,那么 C 列也将是 integer 列,因此值为'???' would not be acceptable as this cannot be converted to an integer value.不可接受,因为这不能转换为 integer 值。

One common approach to this is to reserve a specific value from the integer range as an additional INVALID DATA value.一种常见的方法是保留 integer 范围中的特定值作为附加的 INVALID DATA 值。 For instance if all your valid values are positive you could use -1 as your INVALID DATA value.例如,如果您所有的有效值都是正数,您可以使用-1作为您的 INVALID DATA 值。

Then your code could look like this:那么您的代码可能如下所示:

if( ("PUNTS INICIALS ed50_EQM1 C d" is NULL) or ("PUNTS FINALS ed50_EQM1 C d" is NULL),
    coalesce("PUNTS INICIALS ed50_EQM1 C d","PUNTS FINALS ed50_EQM1 C d"),
    if("PUNTS INICIALS ed50_EQM1 C d"="PUNTS FINALS ed50_EQM1 C d","PUNTS INICIALS ed50_EQM1 C d",-1))

Note that coalesce() can be used to handle the case when one of the values is NULL.请注意, coalesce()可用于处理其中一个值为 NULL 的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM