Solved

SQL Reporting - Dynamic Column Names Based on Report Values

Posted on 2012-04-11
6
191 Views
Last Modified: 2012-05-21
I have a report that uses several CTEs, each CTE return several values.  One of the values returned from each CTE is actually one that I need to use as a column name in the final report.  Please can you advise if there is a way assign the value to the column name e.g.

CTE1.Value AS CTE1.ColValue

Obviously wont work but in effect what I am trying to achieve.

Thanks
0
Comment
Question by:Damozz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 37836072
Are you trying to do this in SSRS, or just name the column in the query results?

If it's SSRS, you should be able to just drop the value onto the report in the header instead of specifying the column name manually, and it should use the first value.

If you're trying to do it in SQL query, you'll need to use dynamic sql to do this, and I'd consider it dangerous, since you'll be injecting the contents of a column into the sql query itself, and a malformed value could have undesired (or damaging) effects, ala Bobby Tables. That said, you'd accomplish it like this:

DECLARE @ColumnName NVARCHAR(100),
        @SQL NVARCHAR(4000)

-- Get your results
SELECT Column1, Column2, Column3, Column4
  INTO #Results
  FROM SomeTableOrSetOfTables

-- Set your column name to the first row of whatever column
SELECT TOP 1 @ColumnName = Column4
  FROM #Results

-- return the results with a re-headered column
SET @SQL = 'SELECT Column1, Column2, Column3, Column4 AS ''' + @ColumnName + ''' FROM #Results'
exec sp_executeSQL @SQL

Open in new window


Is that what you're looking to do?
0
 

Author Comment

by:Damozz
ID: 37836282
I'll porvide a little more detail, I have a report that runs over a 12 month period and the months run April to March, but I need to append the relevant year to the month.  Due to the financial year spanning two calendar years I was looking to use the results to determine the year.
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 37836289
You're still using the term "report" a little unclearly - do you mean just a SQL query, or is the data fed into an SSRS or Crystal report for display? Where do you want to make the correction - in the source query or on the displayed report?

Will my solution work for changing your column name in the source?
0
 

Author Comment

by:Damozz
ID: 37992457
I've requested that this question be deleted for the following reason:

This is no longer a requirement.
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 37992458
I believe the solution will work for the problem as stated - though it's "no longer a requirement", has the OP had a chance to test it? I posted it almost a month ago.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question