Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Data as follows

CompanyName  Year   Sales  TradedSales  TotalSales	Taxes	NetSales  RowMaterial
       TCS	2017      201	   1245	       65650	  23	  3232     323
       TCS	2018      122	   121	       2121	  121	  21231	   2121
    Tata Moters 2015      14	    54	        54	   545	   456	   4548
   Tata Moters	2017      2121	    12	        21	   156	   565	   5656	
   Tata Moters	2018      7512	   5454	        545	   5454    787	    78
     SMERA	2017      5544	   2121	        545	   9522	    878	    121
     SMERA	2017      784	    787	        998	    854	    545	     545

I want Output as

CompanyName    TCS   TCS   [Tata Moters]  [Tata Moters]  [Tata Moters]  SMERA   SMERA
Year           2017   2018     2015          2017           2018         2017    2017    
Sales           201   122      14            2121           7512         5544    784
TradedSales    1245   121      54             12            5454         2121    784
TotalSales     65650  21       21             54            21           545     998
Taxes           23    21       545            156           5454          9522    854
NetSales        3232  21231    456            565	    787           878    121
RowMaterial     323   2121     4548           5656	    78            121    545


also there are many companies data for many years so Want solution dynamic

What I have tried:

I dont have Idea About Pivot so Please  provide solution for problem
thanks in advance
Posted
Updated 15-Feb-18 10:14am

The worst part is that you haven't even bothered to search. You have all the necessary search words in your question.
The very first result here[^] is what you need
   
Comments
paul_vin 16-Feb-18 1:56am
   
I have written code but it is not giving Output as required above

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(CompanyName)
from tblCompanyData
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')

set @query = 'SELECT Year,' + @cols + '
from
(
select [CompanyName]
,[Year]
,[Sales]
,[TradedSales]
,[TotalSales]
,[Taxes]
,[NetSales]
,[RowMaterial]
,[Power]
,[Consumables]
,[Salaries]
,[ManufacturingCost]
,[OtherExpenses]
,[TotalCost]
,[OperatingProfit]
,[Depreciation]
,[Interest]
,[Profitpostinterest]
,[IncomeTax]
,[NetProfit]
,[ID]
FROM tblCompanyData
) x
pivot
(
max(ID)
for CompanyName in (' + @cols + ')
) p '

execute(@query)
That isn't how it works: we are not a "code to order" service.
If you want work done for you - and from your previous questions that is exactly what you do want - then you will have to pay. I suggest you go to freelancer.com and ask there.

Alternatively, read this: Using PIVOT and UNPIVOT[^] and start learning, because at the moment, you are coming over as a Help Vampire and that is not good.
   
Comments
paul_vin 16-Feb-18 1:59am
   
ok thanks for Advice

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