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