472,374 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

Converting Columns into Rows without affecting the data in the table

Vasuki Masilamani
Hi,
I need a query which would convert Columns into Rows without causing any damages to the original data. I am not supposed to solve this by creating temporary tables and later dropping it.

I have a table which is in the below format:

Name EmployeeID Year JanExp FebExp MarExp JanHrs FebHrs MarHrs

A 129178 2005 10 20 30 15 25 35
B 129213 2004 40 50 60 45 55 65
C 129038 2005 70 80 90 75 85 95


I need a Output which has the following format:

Month Name EmployeeID Year Expenses Hours
Jan A 129178 2005 10 15
Jan B 129213 2005 40 45
Jan C 129038 2004 70 75
Feb A 129178 2005 20 25
Feb B 129213 2005 50 55
Feb C 129038 2004 80 85
Mar A 129178 2005 30 35
Mar B 129213 2005 60 65
Mar C 129038 2004 90 95

Please help with some solution.

Thanks,
Vasuki.
Dec 7 '06 #1
6 9484
willakawill
1,646 1GB
Hi,
I need a query which would convert Columns into Rows without causing any damages to the original data. I am not supposed to solve this by creating temporary tables and later dropping it.

I have a table which is in the below format:

Name EmployeeID Year JanExp FebExp MarExp JanHrs FebHrs MarHrs

A 129178 2005 10 20 30 15 25 35
B 129213 2004 40 50 60 45 55 65
C 129038 2005 70 80 90 75 85 95


I need a Output which has the following format:

Month Name EmployeeID Year Expenses Hours
Jan A 129178 2005 10 15
Jan B 129213 2005 40 45
Jan C 129038 2004 70 75
Feb A 129178 2005 20 25
Feb B 129213 2005 50 55
Feb C 129038 2004 80 85
Mar A 129178 2005 30 35
Mar B 129213 2005 60 65
Mar C 129038 2004 90 95

Please help with some solution.

Thanks,
Vasuki.
Hi
if you create a new table and insert the data from the old table. It wil be fine.
Dec 7 '06 #2
Hi
if you create a new table and insert the data from the old table. It wil be fine.
Hi,
I got a solution by creating a table and inserting data into it. But my requirement is to do without creating any tables.

Help me with a solution.

Thanks,
Vasuki.
Dec 7 '06 #3
almaz
168 Expert 100+
Hi,
I need a query which would convert Columns into Rows without causing any damages to the original data...
This sample should give you a hint:
Expand|Select|Wrap|Line Numbers
  1. declare @months table(ID int identity (1,1))
  2.  
  3. declare @i int set @i = 0
  4. while(@i<3)
  5. begin
  6.     insert @months default values
  7.     set @i = @i + 1
  8. end
  9.  
  10. select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year], 
  11.     Expense = case 
  12.         when M.ID = 1 then JanExp
  13.         when M.ID = 2 then FebExp
  14.         when M.ID = 3 then MarExp
  15.     end,
  16.     Hours = case 
  17.         when M.ID = 1 then JanHrs
  18.         when M.ID = 2 then FebHrs
  19.         when M.ID = 3 then MarHrs
  20.     end
  21. from Employees
Dec 7 '06 #4
This sample should give you a hint:
Expand|Select|Wrap|Line Numbers
  1. declare @months table(ID int identity (1,1))
  2.  
  3. declare @i int set @i = 0
  4. while(@i<3)
  5. begin
  6.     insert @months default values
  7.     set @i = @i + 1
  8. end
  9.  
  10. select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year], 
  11.     Expense = case 
  12.         when M.ID = 1 then JanExp
  13.         when M.ID = 2 then FebExp
  14.         when M.ID = 3 then MarExp
  15.     end,
  16.     Hours = case 
  17.         when M.ID = 1 then JanHrs
  18.         when M.ID = 2 then FebHrs
  19.         when M.ID = 3 then MarHrs
  20.     end
  21. from Employees

I can understand the logic of this code. But I am facing some syntax errors in this. Can you please help me in this. Any apt solution would be helpful for me.

Thanks,
Vasuki.
Dec 7 '06 #5
almaz
168 Expert 100+
I can understand the logic of this code. But I am facing some syntax errors in this. Can you please help me in this. Any apt solution would be helpful for me.

Thanks,
Vasuki.
Ooops, sorry, I forgot to add the most important part :).
Here is complete sample:
Expand|Select|Wrap|Line Numbers
  1. create table #Employees
  2.     (
  3.      EmployeeID int,
  4.      Name nvarchar(100),
  5.      [Year] int,
  6.      JanExp float,
  7.      FebExp float,
  8.      MarExp float,
  9.      JanHrs float,
  10.      FebHrs float,
  11.      MarHrs float
  12.     )
  13. insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs) 
  14.     values ('A', 129178, 2005, 10, 20, 30, 15, 25, 35)
  15. insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs) 
  16.     values ('B', 129213, 2004, 40, 50, 60, 45, 55, 65)
  17. insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs) 
  18.     values ('C', 129038, 2005, 70, 80, 90, 75, 85, 95)
  19.  
  20. declare @months table
  21.     (
  22.      ID int identity(1, 1)
  23.     )
  24.  
  25. declare @i int
  26. set @i = 0
  27. while(@i < 3)
  28.     begin
  29.         insert  @months
  30.                 default values
  31.         set @i = @i + 1
  32.     end
  33.  
  34. select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year], 
  35.     Expense = case when M.ID = 1 then JanExp
  36.          when M.ID = 2 then FebExp
  37.          when M.ID = 3 then MarExp
  38.     end,
  39.     Hours = case when M.ID = 1 then JanHrs
  40.                  when M.ID = 2 then FebHrs
  41.                  when M.ID = 3 then MarHrs
  42.             end
  43. from #Employees Emp cross join @months M
Dec 7 '06 #6
Ooops, sorry, I forgot to add the most important part :).
Here is complete sample:
Expand|Select|Wrap|Line Numbers
  1. create table #Employees
  2.     (
  3.      EmployeeID int,
  4.      Name nvarchar(100),
  5.      [Year] int,
  6.      JanExp float,
  7.      FebExp float,
  8.      MarExp float,
  9.      JanHrs float,
  10.      FebHrs float,
  11.      MarHrs float
  12.     )
  13. insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs) 
  14.     values ('A', 129178, 2005, 10, 20, 30, 15, 25, 35)
  15. insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs) 
  16.     values ('B', 129213, 2004, 40, 50, 60, 45, 55, 65)
  17. insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs) 
  18.     values ('C', 129038, 2005, 70, 80, 90, 75, 85, 95)
  19.  
  20. declare @months table
  21.     (
  22.      ID int identity(1, 1)
  23.     )
  24.  
  25. declare @i int
  26. set @i = 0
  27. while(@i < 3)
  28.     begin
  29.         insert  @months
  30.                 default values
  31.         set @i = @i + 1
  32.     end
  33.  
  34. select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year], 
  35.     Expense = case when M.ID = 1 then JanExp
  36.          when M.ID = 2 then FebExp
  37.          when M.ID = 3 then MarExp
  38.     end,
  39.     Hours = case when M.ID = 1 then JanHrs
  40.                  when M.ID = 2 then FebHrs
  41.                  when M.ID = 3 then MarHrs
  42.             end
  43. from #Employees Emp cross join @months M

Thank You So much. I got the solution.
Dec 7 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Pooj | last post by:
have a urgent requirement. Please somebody help me. I have a table departinfo with following records begin_time end_time Name Pieces 10:00 10:15 PopCorn ...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
36
by: kjvt | last post by:
Based on a prior posting, I've written a function to convert a recordset to a dataview. The first call to the function for a given recordset works perfectly, but the second call always returns a...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I am converting Excel data into a Dataset in C#. There are around 24 columns in the Excel Sheet. First I tried to insert one row with correct values in the Excel sheet. i.e. for text columns...
13
by: ppateel | last post by:
Hi, I am new to c++ and I am converting a c program to c++. I changed malloc call to new and I am getting an exception violation. Here is the relevant piece of code. Compiler vc++ 7.0 (.Net...
2
by: Greg | last post by:
I have a gridview on my form which I have populated using a datareader. What I would like to do is to be able to remove specified records from the gridview, without affecting the source of the...
2
by: Nacho | last post by:
Hi, I'm designing a new database and I have a doubt in which surely you can help me. I'm storing in this database historical data of some measurements and the system in constantly growing, new...
2
by: truva | last post by:
Hi, I need a query which would convert Rows into Columns without causing any damages to the original data. I am not supposed to solve this by creating temporary tables and later dropping it. I...
0
by: Orbie | last post by:
Hi Guys, I need some help with pivoting or converting some rows on a Table into columns using SQL Server 2008! I have a Table which contains the same Products in 4 different Stores. I'm only...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.