SQL row to Column PIVOT column career UNPIVOT

Source: Internet
Author: User

Original: SQL row to Column PIVOT column career UNPIVOT

One:

Existing table one (T_TABLE1), want to convert to table two (t_table2) format.

Table I:

year

company

revenue

2013

Company 1

12

2013

Company 2

22

2013

company 3

32

2012

Company 1

42

2012

Company 2

52

2012

Company 3

62

Table II:

Years

Company 1

Company 2

Company 3

2012

42

52

62

2013

12

22

32

Pivot that can be supplied after using sql2005

Here's how:

Select
*
From T_table1 t

PIVOT
(Sum (revenue) for company in (company 1, Company 2, company 3)) a

The result is table two.

Two:

Table II can also be converted to table one, using Unpivot. Here's how:


Select
*

From T_table2 t
UNPIVOT
(Income for company in (company 1, Company 2, company 3)) a

The result is table I.

SQL row to Column PIVOT column career UNPIVOT

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.