I would like to select records where values across 5 columns, if not null, are unequal. Any number of columns can be non null.
schema: given table
**Schema (SQLite v3.30)**
CREATE TABLE test (
id INT,
col1 text,
col2 text,
col3 text,
col4 text,
col5 text
);
INSERT INTO test (id, col1, col2, col3, col4, col5) VALUES (1, 'EA', 'EA', null, null, null);
INSERT INTO test (id, col1, col2, col3, col4, col5) VALUES (2, 'EA', 'MT', null, null, null);
INSERT INTO test (id, col1, col2, col3, col4, col5) VALUES (3, null, 'EA', null, 'KG', null);
INSERT INTO test (id, col1, col2, col3, col4, col5) VALUES (4, null, null, 'KG', 'MT', 'AB');
INSERT INTO test (id, col1, col2, col3, col4, col5) VALUES (5, null, null, 'EA', 'MT', 'EA');
---
**Query #1**
SELECT * FROM test;
| id | col1 | col2 | col3 | col4 | col5 |
| --- | ---- | ---- | ---- | ---- | ---- |
| 1 | EA | EA | | | |
| 2 | EA | MT | | | |
| 3 | | EA | | KG | |
| 4 | | | KG | MT | AB |
| 5 | | | EA | MT | EA |
---
Desired output
| id | col1 | col2 | col3 | col4 | col5 |
| --- | ---- | ---- | ---- | ---- | ---- |
| 2 | EA | MT | | | |
| 3 | | EA | | KG | |
| 4 | | | KG | MT | AB |
| 5 | | | EA | MT | EA |
EDIT: To clarify, id=5 is included in the output since not all values are the same. When I stated values across all columns are unequal, I meant if if any value is distinct from the rest of the non null values. To state it differently, I require records which have at least 1 non null value different from the other non null value.