In Oracle, row-and-column swaps can be achieved through decode, and SQL server also has corresponding measures; Case
Below is a column 1 -- Create a table
2 Create Table Test ( [ Name ] Char ( 10 ), Km Char ( 10 ), CJ Int )
3 Go
4 -- Initialize data
5 Insert Into Test Values ( ' Zhang San ' , ' Chinese ' , 80 )
6 Insert Into Test Values ( ' Zhang San ' , ' Mathematics ' , 86 )
7 Insert Into Test Values ( ' Zhang San ' , ' English ' , 75 )
8 Insert Into Test Values ( ' Li Si ' , ' Chinese ' , 78 )
9 Insert Into Test Values ( ' Li Si ' , ' Mathematics ' , 85 )
10 Insert Into Test Values ( ' Li Si ' , ' English ' , 78 )
11 Go
12 -- Row-column Interchange
13 Select Name, Sum ( Case Km When ' Chinese ' Then CJ End ) As Chinese,
14 Sum ( Case Km When ' Mathematics ' Then CJ End ) As Mathematics,
15 Sum ( Case Km When ' English ' Then CJ End ) As English
16 From Test
17 Group By Name
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.