3

How do I select like this:

SELECT * FROM
 (SELECT 11 AS Value UNION
 SELECT 24 AS Value UNION
 SELECT 53 AS Value UNION
 SELECT 124 AS Value UNION
 SELECT 2215 AS Value) AS ValueTable

This query give me a perfect result, just this query is ugly.

How create this just nicely such as:

select 11,24...
select arrray(22,24...)

These don't works just examples.

Thx

Cipo
  • 161
  • 2
  • 14
  • Each number should be on different row or one row with comma separated numbers? – mitkosoft Jun 28 '16 at 14:54
  • 3
    What is wrong with your query? ***Ugly*** can be solve with beer goggles. What is your expected result? Please read [**How-to-Ask**](http://stackoverflow.com/help/how-to-ask) And here is a great place to [**START**](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) to learn how improve your question quality and get better answers. – Juan Carlos Oropeza Jun 28 '16 at 14:56
  • yes diferent row, its important. thx – Cipo Jun 28 '16 at 14:56
  • if this is ugly for you. you should create table `ValueTable` with those values :-) – Alex Jun 28 '16 at 14:59
  • Possible duplicate of [SQL split values to multiple rows](http://stackoverflow.com/questions/17942508/sql-split-values-to-multiple-rows) I dont think this one is more pretty. But is more generic and scalable. – Juan Carlos Oropeza Jun 28 '16 at 15:11

2 Answers2

3

Your query is correct. The only thing I can do to make it pretty is remove all the field name declaration after the first one

SELECT * 
FROM   (SELECT 11 AS Value  UNION 
        SELECT 24  UNION 
        SELECT 53  UNION 
        SELECT 124 UNION 
        SELECT 2215) AS ValueTable 
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • This is awesome putting lipstick on a pig. +1 ... you don't get paid enough around here. – Drew Jun 29 '16 at 03:09
0

There is no option like this you propose, because it is usual to have separate data from logic:

/* Data */  
CREATE TABLE ValueTable (Value INT);
INSERT INTO ValueTable (Value) VALUES (11), (24), (53), (124), (2215);

/* Logical */
SELECT * FROM ValueTable;
Sueiras
  • 91
  • 6