Trasporre le righe della tabella a colonna
Domanda
Ho una tabella che assomiglia a questo:
id response_id name value
6 13 gender female
5 13 workingArea Sch
3 12 workingArea IT
4 12 gender male
e voglio convertire in qualcosa di simile a questo:
response_id workingArea gender
12 IT male
13 Sch female
Posso sapere come fare questo?
Soluzione
Assumendo che il primo tavolo è SOURCETABLE e la vostra seconda tabella è TARGETTABLE
INSERT INTO TARGETTABLE(response_id, workingArea, gender)
SELECT a.response_id response_id,
a.value workingArea,
b.value gender
FROM SOURCETABLE a, SOURCETABLE b
WHERE a.response_id = b.response_id
AND a.name <> b.name
AND a.name = 'workingArea'
AND b.name = 'gender'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow