Discover pivot and unpivot in sql, include the articles, news, trends, analysis and practical advice about pivot and unpivot in sql on alibabacloud.com
) VALUES (' Xiao Wang ', ' C # Advanced programming ', 8); INSERT into S (name,book,salednumber) values (' Xiao Li ', ' Java from beginner to proficient ', 7); INSERT into S (name,book,salednumber) VALUES (' Xiao Wang ', ' Java from beginner to proficient ', 9); INSERT into S (name,book,salednumber Values (' Xiao Li ', ' Java from beginner to proficient ', 2); INSERT into S (name,book,salednumber) VALUES (' Xiao Wang ', ' C # Advanced Programming ', 3); INSERT into S (Name,book, Salednumber) VAL
SQL Server row and column conversion Pivot UnPivotPivot is used to rotate column values to column names (row to column), and SQL Server 2000 can be implemented with an aggregate function with a case statementThe general syntax for pivot is: Pivot (aggregate function (column)
Pivot is used to rotate column values to column names (row to column), and SQL Server 2000 can be implemented with an aggregate function with a case statementThe general syntax for pivot is: Pivot (aggregate function (column) for column in (...)) As PFull syntax:Table_sourcePIVOT (Aggregation function (value_column)For
Pivot is used to rotate column values to column names (row to column), and SQL Server 2000 can be implemented with an aggregate function with a case statementThe general syntax for pivot is: Pivot (aggregate function (column) for column in (...)) As PFull syntax:Table_sourcePIVOT (Aggregation function (value_column)For
Pivot is used to rotate column values to column names (row to column)Grammar:Table_sourcepivot (Aggregation function (value_column) for Pivot_columnin (Unpivot used to set the column value to a column (that is, a column change)Grammar:Table_sourceunpivot (value_columnfor pivot_columnin (Note: PIVOT, Unpivot is the synt
, -,94)
Go
Select * fromTb Results: 2. UNION all notation Select * from
(
SelectName'language' asCourses, Languages asScores fromTBUnion All
SelectName'Mathematics' asCourses, Mathematics asScores fromTBUnion All
SelectName'Physical' asCourses, physics asScores fromTB) asTOrder byName Results: 3, the Unpivot wording Select * from for inch as T Results: Summary: Previously also used in piv
: Table_source Pivot (aggregate function (Value_column) pivot_column for (columnlist)) A little explanation: Table_source: Is the table we want to convert. Pivot_column: Is the column name for row to column. Value_column: Is the value of the column after the conversion. Columnlist is the column to be generated. This is also the case with pivot, which can be used to write the same result: Select Studentname,
Tags: style blog http color ar using SP for strongOriginal: SQL row to Column PIVOT column career UNPIVOTOne:Existing table one (T_TABLE1), want to convert to table two (t_table2) format.Table I:
year
company
revenue
2013
Company 1
12
2013
Company 2
22
2013
[Studentscores] SELECT 'Yard Farm','English', theINSERT into [Studentscores] SELECT 'Yard Farm','Biological', +2. Row to column SQLSELECT * from [Studentscores] /*Data Source*/ asPpivot (SUM(Score/*value of column after row to column*/) forP.subject/*columns that require row to column*/ inch([language],[Mathematics],[English],[Biological]/*the value of the column*/)) asTExecution Result:Second, the list of career change1. Test data PreparationCREATE TABLEProgrectdetail (ProgrectnameNVARCHAR(
PRODUCT. Name as Product_Name, product. Color as Product_color, product. ReorderPoint, Product_inventory. Quantity as product_quantity from production.product product left JOIN Production . ProductInventory product_inventory on PRODUCT. ProductID = Product_inventory. ProductID) SELECT * from Product_data PIVOT (SUM (product_quantity) for Product_color in ('; withcolorlist as(SELECT DISTINCTProduct.color asColor_name fromproduction.productWHEREProduct
Tags: font order weight sel math ble tab sum ROM1. Pivot usage (row to column) Select * from as a
pivot (sum for in ([ language ],[ Mathematics ],[ Foreign Language ], [ Literature ],[ Comprehensive Management ] as B Data: ID name score
1 language
2 Mathematics 95
3 Foreign languages
4
5 Management of the comprehensive Results: ID Chinese Mathematics Foreign Langua
,'English','Little Li',98 EndGOView CodeOne row to the other pivot and UnpivotMSDN ReferenceMethod 1:SELECTName,sum( CaseCoursename when 'Mathematics' ThenScoreELSE 0 END) as 'Mathematics',sum( CaseCoursename when 'language' ThenScoreELSE 0 END) as 'language',sum( CaseCoursename when 'English' ThenScoreELSE 0 END) as 'English' fromStudentGroup byNameMethod 2:SELECTT.name,sum(T. Mathematics) asMathematics,sum(T. Language) aslanguages,sum(T. Engl
Tags: note span print log spin BER TTY aggregate GroupThe pivot clause is a new feature of Oracle database 11g that enables you to rotate rows into columns in the query output and use aggregate functions on the data. The UNPIVOT clause is also added, and he can rotate columns into rows in the query output;IntroducedThe following table data is now available: (not shown completely ...) ) Now we want to observ
the original table, The inverse perspective does not lose any information. (because there is no aggregate operation)You need to borrow the result set from the perspective transformation. The code is as follows:With CC as(SelectStuname,isnull (Mathematics,0) asMathematics, IsNull (Chinese,0) asChinese, isnull (English,0) asEnglish from(SelectStuname,isnull (Chinese,0) asChinese, isnull (mathematics,0) asMathematics, IsNull (English,0) asEnglish fromStudent P
', '
insert INTO [studentscores] select ' yards ', ' maths ', ' math ',
To [Studentscores] select ' yards ', ' English ', '
INSERT into [studentscores] select ' yards ', ' creature ', 78 2. Row to column SQL SELECT * FROM [studentscores]/* Data source */As
P
PIVOT
(
SUM (Value of column after score/* row to column */)
for p.subject/* columns that require row-to-column * /In ([Language],[math],
1. Basic Tables2. Row to column, note the use of ISNULL function, in the overall statistics, ISNULL (-,0) is necessary to use3. The list of career changes, the list of languages, mathematics, English, politics, the conversion to 2 columns, score scname These two columns, is a new column, can be named Score11 Scname22, casual4. Test scriptsSELECT * fromdbo. SC--inserting test DataINSERTdbo. SCSELECT NEWID(),'001','Jack1','Mathematics', theUNIONSELECT NEWID(),'002','Jack2','language', -UNIONSELEC
Pivot and Unpivot Use a simple SQL spreadsheet-type crosstab report to display information from any relational table and store all the data in a crosstab table in a relational table. PivotAs you know, relational tables are tabular, that is, they appear in the form of column-value pairs. Suppose a table name is CUSTOMERS.Sql> desc Customers
Name
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.