简体   繁体   中英

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. There can also be a NULL in these columns, in that case I'd like to get the other result.

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.

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. I can't get the expected '???' result that I want.

Haven't tried using arrays.

我的代码

So you are using the Field Calculator to compute the values for Column C from the values of Column A and Column B.

Note that QGIS Feature attributes have types they could be either integer, float, or text. Assuming your Column A and Column B are integers, then Column C would also be an integer column, therefore the value '???' would not be acceptable as this cannot be converted to an integer value.

One common approach to this is to reserve a specific value from the integer range as an additional INVALID DATA value. For instance if all your valid values are positive you could use -1 as your INVALID DATA value.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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