SQL Сервер: я хочу транспонировать строки в столбцы - PullRequest
0 голосов
/ 13 марта 2020

Я хочу транспонировать строки в столбцы.

Create table table1 
(
     Emptype varchar(20), 
     id int, 
     empratingid int, 
     emprating varchar(10), 
     empdate datetime, 
     empjust varchar(20)
)

insert into table1 ('test1', 1, 1, 'high','2019-11-01','testing1')
insert into table1 ('test2', 1, 2, 'med','2019-11-04','testing2')
insert into table1 ('test3', 1, 2, 'med','2019-11-05','testing3')
insert into table1 ('test4', 1, 3, 'low','2019-11-05','testing4')
insert into table1 ('test1', 2, 1, 'high','2019-11-01','testing1')
insert into table1 ('test2', 2, 2, 'med','2019-11-04','testing2')
insert into table1 ('test3', 2, 3, 'low','2019-11-05','testing3')
insert into table1 ('test4', 2, 3, 'low','2019-11-05','testing4')

Вывод - в рамках test1, test2..fields Я хочу использовать данные поля empratingid. Я хочу выбрать заявление.

+-----+---------+-------------+--------+-------------+--------+-------------+-------+-------------+
| id  |  test1  |  test1just  |  test2 |  test2just  |  test3 |  test3just  | test4 |  test4just  |
+-----+---------+-------------+--------+-------------+--------+-------------+-------+-------------+
|   1 |      1  |  'testing1' |      2 |  'testing2' |      2 |  'testing3' |     3 |  'testing4' |
|   2 |      1  |  'testing1' |      2 |  'testing2' |      3 |  'testing3' |     3 |  'testing4' |
+-----+---------+-------------+--------+-------------+--------+-------------+-------+-------------+
...