SQL Cross Table instance

Source: Internet
Author: User
Copyright Notice: CSDN is the blog hosting service provider. If the copyright issues involved in this article, csdn do not bear the relevant responsibility, please contact the copyright owner directly with the author of the article to resolve.



SQL Cross Table instance

A very simple thing, see a lot of friends on the internet asked "how to implement the Cross table?" ", here's an example I wrote, the database is based on SQL SERVER 2000.

-- ======================================================

--cross-table instance

-- ======================================================

Build table:

Running in Query Analyzer:

CREATE TABLE [Test] (

[ID] [int] IDENTITY (1, 1) not NULL,

[Name] [nvarchar] (m) COLLATE chinese_prc_ci_as NULL,

[Subject] [nvarchar] (m) COLLATE chinese_prc_ci_as NULL,

[Source] [Numeric] (18, 0) Null

) on [PRIMARY]

Go

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' John ', n ' language ', 60)

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' Dick ', n ' math ', 70)

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' Harry ', N ' English ', 80)

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' Harry ', n ' math ', 75)

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' Harry ', n ' language ', 57)

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' Dick ', n ' language ', 80)

INSERT into [test] ([Name],[subject],[source]) VALUES (n ' John ', N ' English ', 100)

Go



450) this.width=450 "o:title=" SQL2 ">

Implementation of crosstab statements:

--For: The number of columns in the crosstab is OK

Select Name,sum (case subject when ' math ' then source else 0 end) as ' mathematics ',

SUM (case subject when ' English ' then source else 0 end) as ' English ',

SUM (case subject when ' language ' then source else 0 end) as ' language '

From Test

Group BY name





--For: The number of columns in the crosstab is indeterminate

DECLARE @sql varchar (8000)

Set @sql = ' Select Name, '



Select @sql = @sql + ' sum (case subject when ' +subject+ ' "

Then source else 0 end) as ' +subject+ ', '

From (select distinct subject from test) as a



Select @sql = Left (@sql, Len (@sql)-1) + ' from test group by name '

EXEC (@sql)

Go



Run Result:

450) this.width=450 "o:title=" SQL >


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.