Home > database >  This complex SQL.. How make
This complex SQL.. How make

Time:09-16

Data table structure is as follows:
Keyword regions
Shanghai it training
Dark horse hui Shanghai education college
It is the best school school what school is NULL
It education in jiangsu jiangsu
Where to learn good performance NULL
Wuhan Hepburn makeup nail art training school introduced wuhan

line column, requirement, need the following format

CodePudding user response:

There are a great god? Trouble,,

CodePudding user response:

Refer to the
The CREATE TABLE # t
(
Name a varchar (20),
Course varchar (20),
Score int
)
Insert into # t values (' zhang ', '11', '88')
Insert into # t values (' zhang ', '22', '99')
Insert into # t values (' zhang ', '33', '77')
Insert into # t values (' bill ', '11', '77')
Insert into # t values (' bill ', '22', '88')
Insert into # t values (' bill ', '33', '99')
Insert into # t values (' Cathy ', '11', '99')
Insert into # t values (' bill ', '22', '99')
Select name, course, scores from # t -- query round table data
-- -- -- -- -- - transfer line column
Declare @ SQL varchar (8000)
The set @ SQL='select name'
Select SQL + SQL=@ @ ', Max (case course when ' ' '+ + course' ' 'then score else 0 end) [' + + course'] '
The from (select distinct course from # t) as a
SQL=set @ @ + SQL 'from # t group by the name'
The exec (@ SQL)
Drop table # t
  • Related