|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 7:30 AM
Points: 124,
Visits: 371
|
|
;WITH Mysample (Record_id, Col1, Col2, Col3, Col4, Col5) AS (SELECT 101, 'A', 'A', 'B', 'A', 'A' UNION ALL SELECT 102, 'B', 'B', NULL, NULL, 'B' UNION ALL SELECT 103, 'C', 'C', 'C', NULL, 'C' UNION ALL SELECT 104, 'D', 'C', 'D', 'D', 'D')
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 5:27 AM
Points: 921,
Visits: 3,749
|
|
CROSS APPLY VALUES was made for this;
;WITH Mysample (Record_id, Col1, Col2, Col3, Col4, Col5) AS ( SELECT 101, 'A', 'A', 'B', 'A', 'A' UNION ALL SELECT 102, 'B', 'B', NULL, NULL, 'B' UNION ALL SELECT 103, 'C', 'C', 'C', NULL, 'C' UNION ALL SELECT 104, 'D', 'C', 'D', 'D', 'D' )
SELECT Record_id, Newcol FROM Mysample CROSS APPLY ( SELECT Newcol = CASE WHEN MAX(Newcol) = MIN(Newcol) THEN MAX(Newcol) ELSE NULL END FROM (VALUES (Col1), (Col2), (Col3), (Col4), (Col5)) X (Newcol) ) d WHERE Newcol IS NOT NULL
Low-hanging fruit picker and defender of the moggies
For better assistance in answering your questions, please read this.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, April 26, 2013 3:18 AM
Points: 276,
Visits: 524
|
|
Presumably the OP's question was to find rows where all columns have the same value???
CROSS APPLY VALUES is definitely the way to go, but your CASE statement made me scratch my head for a while to determine the logic being implemented. To my mind, the following expresses that logic for more clearly.
ymmv, obviously
WITH Mysample (Record_id, Col1, Col2, Col3, Col4, Col5) AS ( SELECT 101, 'A', 'A', 'B', 'A', 'A' UNION ALL SELECT 102, 'B', 'B', NULL, NULL, 'B' UNION ALL SELECT 103, 'C', 'C', 'C', NULL, 'C' UNION ALL SELECT 104, 'D', 'C', 'D', 'D', 'D' ) SELECT m.Record_id, d.MAXVAL as NewCol FROM Mysample m CROSS APPLY ( SELECT MAX(X.Newcol) as MAXVAL, MIN(X.Newcol) as MINVAL FROM (VALUES (m.Col1), (m.Col2), (m.Col3), (m.Col4), (m.Col5)) X (Newcol) ) d WHERE d.MAXVAL = d.MINVAL ;
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
Where is the DDL? What is yuor question? Why did post half a statement in 1970's Sybase dialect? Is this what you meant, but did not know how to write?
CREATE TABLE Mysamples (sample_id INTEGER NOT NULL PRIMARY KEY, col1 CHAR(1) CHECK (col1 IN ('A', 'B', 'C', 'D')), col2 CHAR(1) CHECK (col2 IN ('A', 'B', 'C', 'D')), col3 CHAR(1) CHECK (col3 IN ('A', 'B', 'C', 'D')), col4 CHAR(1) CHECK (col4 IN ('A', 'B', 'C', 'D')), col5 CHAR(1) CHECK (col5 IN ('A', 'B', 'C', 'D')) );
INSERT INTO Mysamples VALUES(101, 'A', 'A', 'B', 'A', 'A' ), (102, 'B', 'B', NULL, NULL, 'B'), (103, 'C', 'C', 'C', NULL, 'C' ), (104, 'D', 'C', 'D', 'D', 'D');
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: 2 days ago @ 9:30 AM
Points: 595,
Visits: 2,143
|
|
Where is the DDL? What is yuor question? Why did post half a statement in 1970's Sybase dialect? Is this what you meant, but did not know how to write?
I wasn't aware that Sybase 1970 had Common Table Expressions, I thought that came in SQL 2005
========================================================================================================================== A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
|
|
|
|