pivot with two columns combine ms sql -
I want to see results that display in pivot mode in table mode. Although column names for other rows are a combination of Person_Id and Author_Number, and the value for that column should be Project_Id to make it easy to understand, please see sample results below:
client_id 12345_1 12345_2 12346_1 12346_2 12346_1 Client 1 Test 1 Test 1 Client 2 Test 2 Test 2 Client 3 Test 3 Note that the combination of Person_Id and Author_Number from the second column and so on is further ahead.
Here is the sample table
CREATE TABLE [dbo]. [Names]] [Names]] [Names]] [Family name] [Navvorkar] (50) [Navarachar] (50) No tap, [Project_ID] [Navvorkar] (50) No, zero, [ Include in the values ('Client 1', 'Test 1', 12345,1), the author (customer ID, project_id, person_id, author_number, family_name, first_name), person_ID] [first_name] [navelist] (50) 'Gist', 'Fede' writers (client_id, project_id, person_id, author_number, family_name, first_name) Enter values ('Customer1', 'TEET1', 12345,2, 'Ma', 'K') in the authors (Client_ID, Project_ID, Person_ID, Author_Number, Family_Name, First_Name) VALUES ('Client2', 'TEST2 Include in values ('Client 2', 'Test 2', 12346,2, 'Davis',' 12346, 1, 'Jones',' Peter ') in the author (client_id, project_id, person_id, author_number, family_name, first_name) , 'Bob') INSERT authors (client_id, project_id, person_id, author_number, family name, first_name) mu ('Client 3', 'Test 3', 12346,3, 'Richards', 'Craig') This is the script
DECLARE @ ColsPivot AS NVARCHAR (MAX), @class AS NVARCHAR (maximum) select @colsPivot = STUFF (SELECT ',' + QUOTENAME (c.col + '_' + cast), type (APIDPP (''), type). Price ('.', 'NVHAR (Max)'), 1,1, '') set @query = 'Select client_id,' + @ colsPivot + '(select Client_id, col +' '_' '+ cast ( X1 fumes (maximum) for the colon in val (select client_id ('+ + + colspivot +'), call 'varchar' (10)) as the author, print 'quality @ exec' (@query) / Pre> I do not know what the problem with the script is that the value displayed after the client ID column is
Try this query I have to update it Is selected.
DECLARE @ colsPivot AS NVARCHAR (MAX), @ class AS select NVARCHAR (MAX) @colsPivot = STUFF ((SELECT ',' + QUOTENAME (c.col + '_ 'CD (VARCHAR (10)) as the person in the form of VERSAR (C) for XML path (' ', type)' value '('. ',' NVARCHAR () '(author,' varchar (10)) ), '1,1', '' set @query = 'select Client_ID,' + @ colsPivot + '(select Client_ID, cast (varchar (10) as personality)' '_' ' + Cast (Author_Number as Varchar (10)) From Call, Val (Select Client_id, Project_id, Person_Id, Author_Number, Family_Name, First_name from author) in Unpivot (Colonel's) A) Valid (P) (P) (printed by @Police_id)) Pvot (max (+ valcolate + @colspivot + ')) P Print' QuickEx (@ Korea) and the output of the above query is: / p>
Client_id 12345_1 12345_2 12346_1 12346_2 12346_3 ----------------------------------- ------------- ---------- Client 1 Test 1 Test 1 Nail Nail Client 2 Nail Faucet Test 2 Test 2 Faucet Client 3 Nail Nail Nell Nell Test 3 < / code>
Comments
Post a Comment