Solved

SQL Reporting - Dynamic Column Names Based on Report Values

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Conditional WHERE clause 3 49
Many to one in one row 2 48
store vs query adhoc - no show rows 4 37
Creating a View from a CTE 15 50
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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