Solved

How to get db results to be column headings?

Posted on 2008-06-17
4
394 Views
Last Modified: 2008-10-23
I currently have a SQL statement that gives me the corrert results, but I need the results to be formatted in a different way.  For example, I grab 5 records from my database.  Within those results, I have 5 dates, all being Friday dates.  Instead of having 5 records, I need it to be 1 record and those 5 dates need to be column headings with other fields in the result set being under them.  Here is my current Stored Procedure that works, it just gives me a record for every date, instead of making those dates column headings.  Hopefully this makes a little bit of sense and somebody can push me in the right direction.  Thanks in advance.
CREATE PROCEDURE dbo.ManPower_GetProjectDataByProjectID
      (
            @ProjectID int = NULL
      )

AS
SELECT Manpower_Employees.FirstName + ' ' + Manpower_Employees.LastName AS EmployeeName,
Manpower_Employees.EmployeeNumber,
Manpower_Departments.DepartmentName,
Manpower_ProjectDateHours.Hours,
Manpower_Fridays.Fridays
FROM Manpower_Fridays
RIGHT OUTER JOIN Manpower_ProjectDateHours ON Manpower_Fridays.Fridays_ID = Manpower_ProjectDateHours.DateID
LEFT OUTER JOIN Manpower_Projects ON Manpower_ProjectDateHours.ProjectID = Manpower_Projects.Projects_ID
LEFT OUTER JOIN Manpower_Departments
RIGHT OUTER JOIN Manpower_Employees ON Manpower_Departments.Departments_ID = Manpower_Employees.DepartmentID ON
Manpower_ProjectDateHours.EmployeeID = Manpower_Employees.Employees_ID
      
      WHERE Manpower_Projects.Projects_ID = @ProjectID
      


RETURN
GO

0
Comment
Question by:geoffsweb
[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
4 Comments
 
LVL 1

Accepted Solution

by:
sqlconsumer earned 63 total points
ID: 21804119
Have you tried using PIVOT tables ???

http://technet.microsoft.com/en-us/library/ms177410.aspx
0
 
LVL 6

Assisted Solution

by:JohnDesautels
JohnDesautels earned 62 total points
ID: 22295572
I've never used a pivot table, but it doesn't appear that they have dyanamic column names.

What are you ultimately going to do with the data?

Is it imperative that the column headings be dynamically created as the value of your data?

Is there a fixed maximum number of columns that you will support, or does the crosstab need to be able to support an unlimited number of columns?

I've handled similar problems with 2 crosstabs: one supplies the data and the other returns a single record with the column headers. They can be combined in a Crystal report that has a fixed number of columns per page, but an unlimited number of pages to support an unlimited number of data columns.

John
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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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