SQL Server row-to-column, column-career [Goto]

Source: Internet
Author: User
Tags goto

To create a table:

CREATE TABLE [dbo]. [Tbl_student] (
[ID] [int] IDENTITY (*) Not NULL,
[Student ID] [varchar] (COLLATE) Chinese_prc_ci_as NULL,
[Student name] [nvarchar] (COLLATE) Chinese_prc_ci_as NULL,
[Course ID] [NCHAR] (Ten) COLLATE chinese_prc_ci_as NULL,
[Course name] [nvarchar] (COLLATE) Chinese_prc_ci_as NULL,
Results [INT] Null
[Teacher ID] [varchar] (COLLATE) Chinese_prc_ci_as NULL,
[Teacher's name] [nvarchar] (COLLATE) Chinese_prc_ci_as NULL
) on [PRIMARY]

Insert data:

INSERT into tbl_student values (
' S3 ', ' Harry ', ' K4 ' , ' politics ', ' a ', ' T4 ', ' Miss Zhao ';
INSERT into tbl_student values (
' S1 ', ' Zhang San ', ' K1 ' , ' math ', ' a ', ' T1 ', ' Mr. Zhang ';
INSERT into tbl_student values (
' S2 ', ' John Doe ', ' K3 ' , ' English ', and ' T3 ', ' Miss Li ';
INSERT into tbl_student values (
' S1 ', ' Zhang San ', ' K4 ' , ' politics ', and ' T4 ', ' Miss Zhao ';
INSERT into tbl_student values (
' S2 ', ' John Doe ', ' K4 ' , ' politics ', and ' T5 ', ' Zhou Teacher ';
INSERT into tbl_student values (
' S3 ', ' Harry ', ' K2 ' , ' Chinese ', ' a ', ' T2 ', ' Miss Wang ';
INSERT into tbl_student values (
' S3 ', ' Harry ', ' K1 ' , ' mathematics ', ', ' T1 ', ' Zhang ';
INSERT into tbl_student values (
' S1 ', ' Zhang San ', ' K2 ' , ' Chinese ', Bayi, ' T2 ', ' Miss Wang ';
INSERT into tbl_student values (
' S4 ', ' Zhao Liu ', ' K2 ' , ' Chinese ', ' I ', ' T1 ', ' Miss Wang ';
INSERT into tbl_student values (
' S1 ', ' Zhang San ', ' K3 ' , ' English ', Notoginseng, ' T3 ', ' Miss Li ');
Null

Line train

Method 1:

Select Student name, course ID, score, teacher ID, teacher name,

Max (case course name when ' language ' then score else 0 end) language,
Max (case course name when ' math ' then score else 0 end) Math,
Max (case course name when ' English ' then score else 0 end) English
From Tbl_student Group by student name, course ID, score, teacher ID, teacher name

Method 2:

SELECT *

A pivot (max (score) for Course name in (Chinese, maths, English, politics)) b

Results


List of career change:

To create a table:

CREATE TABLE [dbo]. [Tbl_ Row Test] (
[UserID] [INT] Null
[Userno] [INT] Null
[A] [int] NULL,
[B] [int] NULL,
[C] [int] NULL
) on [PRIMARY]

Insert data:

UserID Userno. A B C
1 1 11) 22 33

Method 1:

SELECT  Userid,userno,ttype=attribute
From (SELECT * from Tbl_ row test) a
UNPIVOT
(
Value for attribute in (A, b,c)
) as UPV

Results

SQL Server row-to-column, column-career [Goto]

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.