Usage of SQL pivot (row to column) and Unpivot (column change)

Source: Internet
Author: User

1. Pivot usage (row to column)

Select *  from  as apivot (sum for in ([ language ],[ Math  ],[ Foreign Language ], [ Literature ],[ Comprehensive Management ] as B

Data:

ID    name    score1    Chinese    2    math    3     Foreign Languages    4 of    5 Comprehensive    300 

Results:

ID Chinese Mathematics Foreign Language comprehensive comprehensive theory1     -    NULL    NULL    NULL    NULL2    NULL     the    NULL    NULL    NULL3    NULL    NULL     -    NULL    NULL4    NULL    NULL    NULL     -    NULL5    NULL    NULL    NULL    NULL     -

2. Unpivot usage (career change)

Select  from (Selectfrom[dbo]. [table_week]   for in as B

Data:

week1 week2 week3 week4 week5 One     A     -     -     - A     -     -     -     the -     -     -     the     the -     -     the     the     the

Results:

Orders Weekend OneWeek1 AWeek2 -Week3 -Week4 -Week5 AWeek1 -Week2 -Week3 -Week4 theWeek5 -Week1 -Week2 -Week3 theWeek4 theWeek5 -Week1 -Week2 theWeek3 theWeek4 theWeek5

Usage of SQL pivot (row to column) and Unpivot (column change)

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.