RogerCooper
Registered User.
- Local time
- Today, 01:46
- Joined
- Jul 30, 2014
- Messages
- 764
I need to run a crosstab query directly in SQL Server (the query is being executed through a website). In Access this is easy enough
	
	
	
		
In SQL Server I can do this
	
	
	
		
But I want to determine the column headings from the data, rather than my having to change the query every year. How can I do this in SQL Server?
 
		Code:
	
	
	TRANSFORM Sum(dbo_HISTORY.NET_SALES) AS SumOfNET_SALES
SELECT dbo_HISTORY.CUSTOMER_NAME
FROM dbo_HISTORY
GROUP BY dbo_HISTORY.CUSTOMER_NAME
PIVOT dbo_HISTORY.YEAR;In SQL Server I can do this
		Code:
	
	
	SELECT CUSTOMER_NAME, [2017], [2018],[2019],[2020], [2021], [2022]
FROM   
(SELECT CUSTOMER_NAME, YEAR, NET_SALES
FROM [SPEC_MIS].DBO.HISTORY) p 
PIVOT 
( 
SUM (NET_SALES) 
FOR YEAR IN 
( [2017], [2018], [2019], [2020], [2021], [2022] ) 
) AS pvt 
ORDER BY pvt.CUSTOMER_NAME;But I want to determine the column headings from the data, rather than my having to change the query every year. How can I do this in SQL Server?
 
	 
 
		 
 
		