Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Source	
AccountNo	GSTNumber
A1	1231
A1	1232
A1	1233
A2	1241
A2	1242
A3	1243
A3	1244
A3	1251
A3	1252
A4	1253

output expected
AccountNo	Column1	Column2	Column3	Column4
A1	          1231	1232	1233	
A2	          1241	1242		
A3	          1243	1244	1251	1252
A4	          1253			


What I have tried:

select * from 
(select AccountNo, GSTIN from [GST Sales Report]) as s
PIVOT
(
GSTIN
 FOR GSTIN in ()
 )as pvt
Posted
Updated 2-May-18 3:07am
v2

For Dynamic columns you can try below solution
CREATE  table #source (AccountNo varchar(2), GSTNumber varchar (5) ,GSTAmount INT)
insert into #source (AccountNo, GSTNumber,GSTAmount) values
('A1',	'GST1',1231),
('A1',	'GST2',1232),
('A1',	'GST3',1233),
('A2',	'GST1',1241),
('A2',	'GST2',1242),
('A3',	'GST1',1243),
('A3',	'GST2',1244),
('A3',	'GST3',1251),
('A3',	'GST4',1252),
('A4',	'GST1',1253)





DECLARE @cols AS NVARCHAR(MAX),
        @query AS NVARCHAR(MAX)

SELECT @cols = STUFF
        (
          (
            SELECT ',' + QUOTENAME(GSTNumber)
            FROM #source
            GROUP BY GSTNumber
                     
            ORDER BY GSTNumber
            FOR XML PATH(''), TYPE
          ).value('.', 'NVARCHAR(MAX)'),
          1,1,''
        );

SET @query = 'SELECT AccountNo,' + @cols + ' 
              FROM
              (
                SELECT AccountNo,GSTNumber,GSTAmount
                FROM #source
             ) x
             PIVOT
             (
                MAX(GStAmount)
                FOR GSTNumber IN (' + @cols + ')
             ) p ';

EXECUTE(@query);
 
Share this answer
 
v2
Comments
sunil mali 2-May-18 9:07am    
Hi Rajat,
Thank you so much for the explaination,
But there was problem in my explaination of the question i have updated the question, can you please look back and get back to me.
Rajat-Indiandotnet 2-May-18 9:42am    
Please, check updated response.
sunil mali 17-May-18 5:39am    
Let me explain you the logic in detail.
1. One account can have multiple gst numbers.
2. GST number tagged against Account1 will not be there for Account2

As per your suggestion you want me to create columns based on GST Numbers, so if there are 100000 gst numbers, i have to create 100000 gst columns.
I don't want that,
WHat i want is if Acount1 has 3 gst numbers then data will come only in column1, column2 & column3
If Account2 has 2 gst numbers then data will come only in Column1 & Column2
If Account3 has only 1 gst number then data will come in only column1.
if Account4 has 10gst numbers then output table should have column1...column10
and each column should have different gst numbers.
sunil mali 17-May-18 5:41am    
If you have a look at my output table you will understand what i am trying to say.
I cannot execute logic as per your suggestion as it would end up creating lakh of columns.
sunil mali 17-May-18 5:44am    
I need help badly as i am getting this type of request again and again and i am failing badly.
You need to put the values in that FOR list and use a function e.g.
clare @source table (AccountNo varchar(2), GSTNumber varchar (5))
insert into @source (AccountNo, GSTNumber) values
('A1',	'GST1'),
('A1',	'GST2'),
('A1',	'GST3'),
('A2',	'GST1'),
('A2',	'GST2'),
('A3',	'GST1'),
('A3',	'GST2'),
('A3',	'GST3'),
('A3',	'GST4'),
('A4',	'GST1')

SELECT * FROM 
	(select AccountNo, GSTNumber from @source) src
	PIVOT
	(
		MAX(GSTNumber)
		FOR GSTNumber in (GST1, GST2, GST3, GST4)
	) as pvt
You could just have easily used MIN(GSTNumber), but you do have to have a function there
 
Share this answer
 
v2
Comments
sunil mali 30-Apr-18 6:53am    
I am having problem with the column names as my columns are not fixed like,
GST1, GST2, GST3, GST4
This could have any value,

SELECT * FROM
(select AccountNo, GSTNumber from source) src
PIVOT
(
MAX(GSTNumber)
FOR GSTNumber in (select GSTNumber from source)
) as pvt

i want to use select query inside (in) like above. but its not working.
CHill60 30-Apr-18 8:28am    
It's not working because that is not the correct syntax for Pivot! :-)
That list has to contain the column names generated by the pivot, it is not a list of values.
In my article Processing Loops in SQL Server[^] I have an example of how to generate that list dynamically

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900