SQL Server Pivot hidden Group

Source: Internet
Author: User

SQL Server row and column transformation hidden group

Pivot has a hidden group group, except for pivot column and value column, and other columns as groupings

Example:

IF not EXISTS (SELECT * from sys.tables where name = ' Pivot_test ')

CREATE TABLE Pivot_test

(

ID1 int,

Id2 int,

Pivot_column varchar (50),

Value char (50)

)

INSERT into pivot_test values (A, ' A ', ' A_v '), (with the ' B ', ' B_v '), (with the ' C ', ' C_v '), (with the ' D ', ' D_v ')

Row and column conversions

SELECT * from Pivot_test Pivot (MAX (value) to Pivot_column in (a,b,c,d)) TEM

Update A of the ID2 value of 2, again row and column conversion, found that two lines, proving that id2 affect the group

Update pivot_test Set id2 = 2 WHERE pivot_column = ' A '

SELECT * from Pivot_test Pivot (MAX (value) to Pivot_column in (a,b,c,d)) TEM

Update A's ID1 value to 2,id2 update to the old value 1, see the row and column conversion results again, find the result is still two lines, prove that ID1 is also in the group

Update pivot_test Set id2 = 1 WHERE pivot_column = ' A '

Update pivot_test Set id1 = 2 WHERE pivot_column = ' A '

SELECT * from Pivot_test Pivot (MAX (value) to Pivot_column in (a,b,c,d)) TEM

The above proves that pivot is grouped with all other columns except pivot column and value

SQL Server Pivot hidden Group

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.