Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
MeltdownMX
Starting Member
2 Posts |
Posted - 2005-07-13 : 10:18:08
|
Hi,I saw one other post on this however what was needed to be accomplished was much too complicated for what I need to do. I have a query which returns one row of data and about 10 columns. I want to chart the data using a pie chart with Coldfusion. The problem I have is that if I want to present the data appropriately I need the Row that is returned to become a column and all of the column names to become a bunch of records under a column name. Here is my example:I currently have:RegularHours OTHours TrainingHours TrainingOT VacationHours5,000 1,000 500 25 250 I want it to look like this:Hours PayType5,000 RegularHours1,000 OTHours500 TrainingHours25 TrainingOT250 VacationHours I dont think its that hard. I mean I know how to do it in MS excel, however I am not sure how to do this with SQL and do not want to be manually exporting to excel, transposing, then reimporting into SQL all the time. How can this be done with one row or a few rows of data?Any help would be appreciated. Thanks in advance! |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-07-13 : 10:56:34
|
use union all:select 5,000 as hours, 'RegularHours' as PayType union allselect 1,000, 'OTHours' union allselect 500, 'TrainingHours' union allselect 25, 'TrainingOT' union allselect 250, 'VacationHours' union allGo with the flow & have fun! Else fight the flow |
 |
|
|
MeltdownMX
Starting Member
2 Posts |
Posted - 2005-07-13 : 14:23:59
|
Hi spirit1,Thanks for that input, I couldnt get the UNION ALL to work like you posted, it kept complaining about incorrect syntax near the FROM clause. But I finally did figure it out. I am not an DB Admin by trade, just a network admin with web development and database experience.So this is what I am using to get it to work. I will post it in case anyone else needs to do the same thing. I have been looking for a way to do this and this looked like the best resource in getting help to figure this out. SELECT TotalReg AS Hours, 'Regular Hours' AS PayTypeFROM dbo.FY05_WorkforceUNION ALLSELECT TotalOT, 'Regular OT'FROM dbo.FY05_WorkforceUNION ALLSELECT TrainReg, 'Training Regular'FROM dbo.FY05_WorkforceUNION ALLSELECT LeaveReg, 'Regular Leave'FROM dbo.FY05_WorkforceUNION ALLSELECT LeaveOT, 'Leave OT'FROM dbo.FY05_WorkforceUNION ALLSELECT TrainOT, 'Training OT'FROM dbo.FY05_WorkforceUNION ALLSELECT WBReg, 'WB Regular Hrs'FROM dbo.FY05_WorkforceUNION ALLSELECT WBOT, 'WB OT Hrs'FROM dbo.FY05_WorkforceUNION ALLSELECT TotalHrsAvail, 'Total Hours Avail'FROM dbo.FY05_WorkforceUNION ALLSELECT TotalHrsPAID, 'Total Hours Paid'FROM dbo.FY05_Workforce Thanks a LOT spirit1. You Rock!!! |
 |
|
|
tking1
Starting Member
22 Posts |
Posted - 2010-05-24 : 17:40:21
|
| Meltdown,Thanks for this post, it was exactly what I needed and saved me alot of heartache!TK |
 |
|
|
|
|
|
|
|