Favorite columns on the Internet

Source: Internet
Author: User

If exists (select * From information_schema.tables where table_name = 'tb ')
Drop table TB
Go
Create Table Tb (name varchar (10), course varchar (10), score int, class varchar (10 ))
Insert into TB values ('zhang san', 'China', 74, 'class 1 ')
Insert into TB values ('zhang san', 'mat', 83, 'class 1 ')
Insert into TB values ('zhang san', 'Physical ', 93, '2 class ')
Insert into TB values ('Li si', 'China', 74, '2 class ')
Insert into TB values ('Li si', 'mat', 84, 'class 1 ')
Insert into TB values ('Li si', 'Physical ', 94, 'class 1 ')
Insert into TB values ('Li si', 'Physical ', 100, 'class 1 ')
Go
Select * from TB

Go -- sql2005
Declare @ SQL varchar (8000)
Select @ SQL = isnull (@ SQL + ',', '') + course from TB group by course
Exec ('select * from (select * from TB) a round (max (score) for course in ('+ @ SQL +') B ')
Go
-- SQL2000
Select name as name, class,
Max (Case course when 'China' then score else 0 end) language,
Max (Case course when 'mate' then score else 0 end) math,
Max (Case course when 'physical 'Then score else 0 end) Physical
From TB
Group by name, class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.