0

I have table on like this :

id_biling  | disc_A  | disc_B | disc_C
    1         100        0        0
    2         200       300      400
    3         500        0       600

I want to get result like this :

id_biling  | disc |  Desc
    1        100     Disc_A
    2        200     Disc_A 
    2        300     Disc_B
    2        400     Disc_C
    3        500     Disc_A
    3        600     Disc_C
Mortred
  • 1
  • 1
  • Does this answer your question? [MySQL - How to unpivot columns to rows?](https://stackoverflow.com/questions/15184381/mysql-how-to-unpivot-columns-to-rows) – ahmed Sep 09 '22 at 05:41
  • Which dbms are you using? (You've already got one product specific answer, don't waste other people's time writing answers for 'wrong' dbms.) – jarlh Sep 09 '22 at 08:04
  • sorry , i forget about dbms, i using mariadb server 10.7.5 – Mortred Sep 10 '22 at 10:47
  • And what's your question about this? – Nico Haase Sep 10 '22 at 17:09

2 Answers2

0

Here's a solution with SQL server

You can use unpivot.

select *
from   t
UNPIVOT  
   (disc FOR "DESC" IN   
      (disc_A, disc_B, disc_C)  
)AS unpvt;    
id_biling disc DESC
1 100 disc_A
2 200 disc_A
2 300 disc_B
2 400 disc_C
3 500 disc_A
3 600 disc_C

Fiddle

DannySlor
  • 4,574
  • 1
  • 3
  • 11
  • 1
    Product specific answer to a question with no dbms specified. At least tell us which dbms this is for. – jarlh Sep 09 '22 at 08:05
0

Problem solved inspirated by MySQL - How to unpivot columns to rows?

(select id_billing, 'DISC_A' DESCR, DISC_A as DISC from tTable where DISC_A > 0 ) 
union all 
(select id_billing, 'DISC_B' DESCR, DISC_B as DISC from tTable where DISC_B > 0 ) 
union all 
(select id_billing, 'DISC_C' DESCR, DISC_C as DISC from tTable where DISC_C > 0 ) 
ORDER BY 1,2;
Mortred
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Albert Logic Einstein Sep 14 '22 at 16:41