Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Value A AND Value B = NULL --> Get Value A
Value A = NULL AND Value B --> Get Value B
Value A and B == NULL --> Get NULL



All of this using QGIS, a tophography program I'm wotking on right now. From what I've reserched, the language is SQL or Python, but I think they're similar in some ways.

Sorry if my english is not perfect. First time here.

What I have tried:

So here's what I've done. Since I can only work on 'if' (QGIS looks to fall short in programming stuff) I've done a chain of if's that gets close to what I'm looking for.


[My code](https://imgur.com/a/j7EjjjU)


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.
Posted
Updated 29-Dec-22 10:04am

1 solution

I don't know QGIS, but i'd suggest to use SQL, like this:
SQL
SELECT ColumnA, ColumnB, CASE 
  WHEN ColumnA = ColumnB THEN ColumnA
  WHEN ColumnA <> ColumnB THEN -1
  WHEN ColumnA IS NOT NULL AND ColumnB IS NULL THEN ColumnA
  WHEN ColumnA IS NULL AND ColumnB IS NOT NULL THEN ColumnB
  END AS ColumnC
FROM MyData;


Result:
ColumnA 	ColumnB 	ColumnC
1			2 			-1
2	 		2 			2
3 			null 		3
null 		4 			4
null 		null 		null


More at: SQL Server 2022 | db<>fiddle[^]

Is this helpful?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900