Do chart statistics you need to master SQL Server row to column and column career

Source: Internet
Author: User

Say in front

To do a data statistics and analysis of the project, face a variety of data every day, after the storage process from the source table after the summary of the need to write intermediate results table to improve the efficiency of data use, then you need to use row to column and column change.

1. List of career change

After the data is processed, the data source of the front-end chart will be generated directly, but the data must be written into the intermediate table by the program, and the data can be queried directly from the intermediate table the next time the data is queried.

1.1 Column Line Wrapping syntax
Table_sourceunpivot (value_column for pivot_columnin (<column_list > ))

1.2 Row to column case
 withT as(    SELECT 1  asTeamid,'Test Team 1'  asTeam, the ' Men', - 'Women'    UNION     SELECT 2  asTeamid,'Test Team 2'  asTeam, - ' Men', - 'Women' )---Career------------------------------------SELECTTeamid,team, TYPE=Attribute,cnt=VALUE fromT UNPIVOT (VALUE forATTRIBUTEinch([ Men],[Women]))  asUPV

2, Row to column

Row-to-column data is primarily queried from the middle table, and the following versions of SQL SERVER2005 can be done using aggregate functions.

2.1 Row to column syntax
Table_sourcepivot (Aggregation function (value_column) for pivot_columnin (<column_ List>))
2.2, using pivot to achieve
 withT as(  SELECT  1  asId'Test Team 1'TEAM,' Men'ITEM, theCENTUNION  SELECT  1  asId'Test Team 1'TEAM,'Women'ITEM, -CENTUNION  SELECT  2  asId'Test Team 2'TEAM,' Men'ITEM, -CENTUNION  SELECT  2  asId'Test Team 2'TEAM,'Women'ITEM, -CENT)SELECT *  fromT PIVOT (SUM(CENT) forITEMinch([ Men],[Women])) A
2.3, using the aggregation function to implement
 withT as(  SELECT  1  asId'Test Team 1'TEAM,' Men'ITEM, theCENTUNION  SELECT  1  asId'Test Team 1'TEAM,'Women'ITEM, -CENTUNION  SELECT  2  asId'Test Team 2'TEAM,' Men'ITEM, -CENTUNION  SELECT  2  asId'Test Team 2'TEAM,'Women'ITEM, -CENT)SELECTId,team,SUM( Case  whenITEM=' Men'  ThenCENTELSE 0 END)' Men',SUM( Case  whenITEM='Women'  ThenCENTELSE 0 END)'Women'  fromTGROUP  byId,team

Resources

Http://www.cnblogs.com/zhangzt/archive/2010/07/29/1787825.html

Http://www.cnblogs.com/aspnethot/articles/1762665.html

Do chart statistics you need to master SQL Server row to column and column career

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.