Row-to-column (PIVOT) and column-changing for SQL (UNPIVOT)

Source: Internet
Author: User

When it comes to statistics, it is often a problem to change rows and columns. Case when mode is too cumbersome, and scalability is not strong, you can use Pivot,unpivot to quickly achieve row to column, column change, and Scalability strong

First, row to column

1. Test data Preparation

CREATE  TABLE [studentscores] (   [UserName]         NVARCHAR (a),        --Student name   [Subject]          NVARCHAR (30),        --Subjects   [Score]            FLOAT,               --score) INSERT into [studentscores] select ' Zhang San ', ' language ', 80INSERT into [studentscores] select ' Zhang San ', ' math ', 90INSERT I NTO [studentscores] select ' Zhang San ', ' English ', 70INSERT into [studentscores] select ' Zhang San ', ' creature ', 85INSERT into [studentscores] Sele CT ' John Doe ', ' language ', 80INSERT into [studentscores] select ' John Doe ', ' math ', 92INSERT into [studentscores] select ' John Doe ', ' English ', 76INSERT into [studentscores] select ' John Doe ', ' creature ', 88INSERT into [studentscores] select ' yards ', ' languages ', 60INSERT into [studentscores] SEL ECT ' Code farmer ', ' math ', 82INSERT into [studentscores] select ' yards ', ' English ', 96INSERT into [studentscores] select ' yards ', ' creature ', 78

2. Row to column SQL

SELECT * FROM [studentscores]/* Data source */as ppivot (    SUM (the value of column after score/* row to column */)     for p.subject/* the column that requires row to column */in ([Language],[math],[ English],[Biological]/* column value */)) as T

Execution Result:

Second, the list of career change

1. Test data Preparation

CREATE TABLE progrectdetail (    progrectname         NVARCHAR (20),--project name    overseasupply        INT,          --supply quantity of overseas suppliers    nativesupply         int,          --Domestic supplier supply quantity    southsupply          int-          -South supplier Supply Quantity    northsupply          int           -North supplier supply quantity) INSERT into Progrectdetailselect ' A ', 50UNION allselect ' B ', 150UNION allselect ' C ', 159, 400 , 320UNION All

2. SQL for career change

Select P.progrectname,p.supplier,p.supplynumfrom (    select Progrectname, overseasupply, nativesupply,           Southsupply, northsupply from     progrectdetail) tunpivot (supplynum    -Supplier    in (overseasupply, Nativesupply, Southsupply, northsupply) P

Execution Result:

Row-to-column (PIVOT) and column-changing for SQL (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.