Quantcast
Channel: Questions in topic: "union-all"
Viewing all articles
Browse latest Browse all 18

Removing UNION ALL From Query

$
0
0
Hi there, Is there a smarter way to write a SELECT query so that the UNION ALL is removed and that the table is only queried once? Here is the test data: CREATE TABLE t2( ID INT, Condition1 VARCHAR(100), Condition2 VARCHAR(100), Condition3 VARCHAR(100), Condition4 VARCHAR(100)) INSERT INTO t2 SELECT 2425,'New','New','Reasonable',NULL UNION SELECT 2513,'Reasonable','Reasonable','Reasonable ',NULL UNION SELECT 2386,'Reasonable',NULL,'Reasonable','Reasonable' UNION SELECT 2391,'Reasonable','Poor','Poor',NULL UNION SELECT 3401,'New','Poor','New',NULL UNION SELECT 2402,'New','Reasonable','Reasonable',NULL UNION SELECT 2486,'Reasonable',NULL,'Reasonable','Reasonable' UNION SELECT 2955,'Poor',NULL,'Reasonable ',NULL UNION SELECT 4248,'Reasonable',NULL,'Reasonable','Reasonable' UNION SELECT 2399,'New','Reasonable',NULL,'Poor' UNION SELECT 2524,NULL,'Poor','Reasonable','Reasonable' UNION SELECT 2965,'Reasonable','Poor','Reasonable',NULL And here is the query that I'd like to execute but only query the table t2 once thus removing the UNION ALL commands: SELECT ID, 'Condition1 - ' + COALESCE(Condition1,NULL,'Unknown') AS Conditions FROM t2 GROUP BY ID, Condition1 UNION ALL SELECT ID, 'Condition2 - ' + COALESCE(Condition2,NULL,'Unknown') FROM t2 GROUP BY ID, Condition2 UNION ALL SELECT ID, 'Condition3 - ' + COALESCE(Condition3,NULL,'Unknown') FROM t2 GROUP BY ID, Condition3 UNION ALL SELECT ID, 'Condition4 - ' + COALESCE(Condition4,NULL,'Unknown') FROM t2 GROUP BY ID, Condition4 TIA

Viewing all articles
Browse latest Browse all 18

Trending Articles