-1

Sorry I should have been more specific with my question:

This is my current issue

I have a table that has this:

Customer_ID        H2_1      H2_2      H2_3     H2_4      H2_5
123                 7         3         13       5         2
456                 7         3         13       5         2
789                 7         3         13       5         2

and I need it to look like this:

Customer_ID         H2_Code
123                    7
123                    3
123                   13
123                    5
123                    2
456                    7
456                    3
456                   13
456                    5
456                    2
789                    7
789                    3
789                   13
789                    5
Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    Are you *creating* a table with these two columns, as the question title states? Or are you *querying* one or more other tables to get these results? It's not really clear what the issue is here. What specific problem have you encountered? – David Jan 09 '19 at 13:41
  • @Cid Creating tables does not have any thing to do with rows. – TiyebM Jan 09 '19 at 13:46
  • This kind of problem is symptomatic of poor design – Strawberry Jan 09 '19 at 13:56

4 Answers4

1

You need UNION ALL to combine all columns :

SELECT Customer_ID, H2_1 AS H2_Code
FROM table t
UNION ALL
SELECT Customer_ID, H2_2
FROM table t
UNION ALL
SELECT Customer_ID, H2_3
FROM table t 
UNION ALL
SELECT Customer_ID, H2_4
FROM table t 
UNION ALL
SELECT Customer_ID, H2_5
FROM table t
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
1

You seem to want union all:

select Customer_ID, H2_1 as h2_code from t
union all
select Customer_ID, H2_2 as h2_code from t
union all
select Customer_ID, H2_3 as h2_code from t
union all
select Customer_ID, H2_4 as h2_code from t
union all
select Customer_ID, H2_5 as h2_code from t;

Note that union all requires scanning the table 5 times. For small or medium sized tables, that should not be an issue. For larger tables, there are slightly more complicated mechanisms that would only scan the table once.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You might want to union the differents queries that fetch each H2_something, then order by customer_id

Schema (MySQL v5.7)

CREATE TABLE tableName (
  `Customer_ID` INTEGER,
  `H2_1` INTEGER,
  `H2_2` INTEGER,
  `H2_3` INTEGER,
  `H2_4` INTEGER,
  `H2_5` INTEGER
);

INSERT INTO tableName
  (`Customer_ID`, `H2_1`, `H2_2`, `H2_3`, `H2_4`, `H2_5`)
VALUES
  ('123', '7', '3', '13', '5', '2'),
  ('456', '7', '3', '13', '5', '2'),
  ('789', '7', '3', '13', '5', '2');

Query #1

SELECT customer_Id, H2_1 AS H2_Code FROM tableName
UNION ALL
SELECT customer_Id, H2_2 FROM tableName
UNION ALL
SELECT customer_Id, H2_3 FROM tableName
UNION ALL
SELECT customer_Id, H2_4 FROM tableName
UNION ALL
SELECT customer_Id, H2_5 FROM tableName
ORDER BY customer_Id;

| customer_Id | H2_Code |
| ----------- | ------- |
| 123         | 3       |
| 123         | 13      |
| 123         | 5       |
| 123         | 2       |
| 123         | 7       |
| 456         | 3       |
| 456         | 13      |
| 456         | 5       |
| 456         | 2       |
| 456         | 7       |
| 789         | 7       |
| 789         | 3       |
| 789         | 13      |
| 789         | 5       |
| 789         | 2       |

View on DB Fiddle

Cid
  • 14,968
  • 4
  • 30
  • 45
0

Note that a normalised schema would typically look something like this:

Customer_ID    H2    val    
123             1      7     
123             2      3     
123             3     13     
123             4      5     
123             5      2
456             1      7     
456             2      3     
456             3     13     
456             4      5     
456             5      2
789             1      7     
789             2      3     
789             3     13     
789             4      5     
789             5      2
Strawberry
  • 33,750
  • 13
  • 40
  • 57