Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible create a sql view from "horizontal" to "vertical"

Tags:

sql

sql-server

please see my attached below: my actual data will be store in table 1, can I create a view that display data like table 2? enter image description here

like image 797
Joehom Sum Avatar asked Dec 06 '22 05:12

Joehom Sum


1 Answers

Yes it is possible and it is called a UNPIVOT

For your case:

SELECT period, value, category
FROM 
   (SELECT VendorID, charcge,nocharge
   FROM Table) p
UNPIVOT
   (value FOR category IN 
      (charge,nocharge)
)AS unpvt;
like image 100
Giannis Paraskevopoulos Avatar answered Dec 08 '22 18:12

Giannis Paraskevopoulos