473,513 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Columns into Rows without affecting the data in the table

Vasuki Masilamani
18 New Member
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 9612
willakawill
1,646 Top Contributor
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
Vasuki Masilamani
18 New Member
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 Recognized Expert New Member
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
Vasuki Masilamani
18 New Member
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 Recognized Expert New Member
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
Vasuki Masilamani
18 New Member
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
23488
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
2530
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
4415
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
2616
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
4623
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
5444
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
3497
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
1933
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
2362
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...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.