Home > database >  Turn to a column into a line of data problems
Turn to a column into a line of data problems

Time:10-05

There is a staff table
Id name xh
Zhang SAN 1
Li si 2
3 fifty 3
.
Hope can use SQL take into a record, each staff was quite a field, not in a field,
The select... The from...
Tom, dick and harry fifty

CodePudding user response:

 select case when id=1 then the name end, 
Case the when id=2 then name the end,
Case the when id=3 then name the end
From the table.

CodePudding user response:

Can not write id or serial number, for example there are 5 records, written statement of die?

CodePudding user response:

PIVOT in the query clause and UNPIVOT clauses

CodePudding user response:

Oracle 11 after, there was a pivot method
As
 with t(select 1 id, 'zhang' name from dual 
Union all
Select id, 2 'bill' name from dual
Union all
Select 3, 'Cathy' from dual
Union all
Select 4, 'zhao four from dual)

Select * from (select id, name from t) pivot (Max (name) for id in (1, 2, 3, 4))

CodePudding user response:

reference 4 floor jdsnhan response:
oracle 11, after a pivot method
As
 with t(select 1 id, 'zhang' name from dual 
Union all
Select id, 2 'bill' name from dual
Union all
Select 3, 'Cathy' from dual
Union all
Select 4, 'zhao four from dual)

Select * from (select id, name from t) pivot (Max (name) for id in (1, 2, 3, 4))


If there is a grouping column names for codeid, how is the pivot statements written under study
  • Related