I need to compare figures across a row and show where there is any mismatch, excluding zero and null values. So in the example, ids 1, 4 and 5 would be returned. What is the most painless way of doing this without a ton of where and or statements?
CREATE TABLE #XX
(
ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
VAL1 INT,
VAL2 INT,
VAL3 INT,
VAL4 INT,
VAL5 INT
)
INSERT INTO #XX (VAL1,VAL2,VAL3,VAL4,VAL5) VALUES
(5,10,NULL,17,12),
(12,12,12,NULL,0),
(15,15,15,15,15),
(3,NULL,6,0,NULL),
(10,NULL,15,0,20)
Cheers.
Best Answer
dbfiddle
The above seems to do what you need - if you don't know the particular columns you'll need to UNPIVOT for then this will need to be reformed into a dynamic UNPIVOT script.