Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL multiple columns grouping in one row

I have the following table in SQL

AL1 | AL2 | AL3 | ACB | LL1 | LL2 | LL3 | LCB  
------------------------------------------------
 1  |  2  |  3  |  4  |  5  |  6  |  7  |  8

I would like to convert it as

    | L1  | L2  | L3  | CB  
-----------------------------
A   |  1  |  2  |  3  |  4   
L   |  5  |  6  |  7  |  8

Any help would be appreciated.

like image 433
ssiderova Avatar asked May 21 '26 17:05

ssiderova


1 Answers

You can select both with different query and join them using UNION

SELECT 'A' AS COL, AL1, AL2, AL3, ACB
  FROM TBL
 UNION
SELECT 'L' AS COL, LL1, LL2, LL3, LCB
  FROM TBL;

Output:

| COL | AL1 | AL2 | AL3 | ACB |
-------------------------------
|   A |   1 |   2 |   3 |   4 |
|   L |   5 |   6 |   7 |   8 |

See this SQLFiddle

like image 76
Himanshu Jansari Avatar answered May 24 '26 06:05

Himanshu Jansari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!