Solved

SQL Reporting - Dynamic Column Names Based on Report Values

Posted on 2012-04-11
6
189 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Creating a summarized report 19 57
migrate a SQL 2008 to 2016, 2 44
TSQL - How to declare table name 26 53
SQL Syntax: How to force case sensitive query? 2 56
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

763 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