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.
6 9612
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.
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.
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: - declare @months table(ID int identity (1,1))
-
-
declare @i int set @i = 0
-
while(@i<3)
-
begin
-
insert @months default values
-
set @i = @i + 1
-
end
-
-
select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year],
-
Expense = case
-
when M.ID = 1 then JanExp
-
when M.ID = 2 then FebExp
-
when M.ID = 3 then MarExp
-
end,
-
Hours = case
-
when M.ID = 1 then JanHrs
-
when M.ID = 2 then FebHrs
-
when M.ID = 3 then MarHrs
-
end
-
from Employees
This sample should give you a hint: - declare @months table(ID int identity (1,1))
-
-
declare @i int set @i = 0
-
while(@i<3)
-
begin
-
insert @months default values
-
set @i = @i + 1
-
end
-
-
select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year],
-
Expense = case
-
when M.ID = 1 then JanExp
-
when M.ID = 2 then FebExp
-
when M.ID = 3 then MarExp
-
end,
-
Hours = case
-
when M.ID = 1 then JanHrs
-
when M.ID = 2 then FebHrs
-
when M.ID = 3 then MarHrs
-
end
-
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.
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: - create table #Employees
-
(
-
EmployeeID int,
-
Name nvarchar(100),
-
[Year] int,
-
JanExp float,
-
FebExp float,
-
MarExp float,
-
JanHrs float,
-
FebHrs float,
-
MarHrs float
-
)
-
insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs)
-
values ('A', 129178, 2005, 10, 20, 30, 15, 25, 35)
-
insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs)
-
values ('B', 129213, 2004, 40, 50, 60, 45, 55, 65)
-
insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs)
-
values ('C', 129038, 2005, 70, 80, 90, 75, 85, 95)
-
-
declare @months table
-
(
-
ID int identity(1, 1)
-
)
-
-
declare @i int
-
set @i = 0
-
while(@i < 3)
-
begin
-
insert @months
-
default values
-
set @i = @i + 1
-
end
-
-
select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year],
-
Expense = case when M.ID = 1 then JanExp
-
when M.ID = 2 then FebExp
-
when M.ID = 3 then MarExp
-
end,
-
Hours = case when M.ID = 1 then JanHrs
-
when M.ID = 2 then FebHrs
-
when M.ID = 3 then MarHrs
-
end
-
from #Employees Emp cross join @months M
Ooops, sorry, I forgot to add the most important part :).
Here is complete sample: - create table #Employees
-
(
-
EmployeeID int,
-
Name nvarchar(100),
-
[Year] int,
-
JanExp float,
-
FebExp float,
-
MarExp float,
-
JanHrs float,
-
FebHrs float,
-
MarHrs float
-
)
-
insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs)
-
values ('A', 129178, 2005, 10, 20, 30, 15, 25, 35)
-
insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs)
-
values ('B', 129213, 2004, 40, 50, 60, 45, 55, 65)
-
insert #Employees (Name, EmployeeID, [Year], JanExp, FebExp, MarExp, JanHrs, FebHrs, MarHrs)
-
values ('C', 129038, 2005, 70, 80, 90, 75, 85, 95)
-
-
declare @months table
-
(
-
ID int identity(1, 1)
-
)
-
-
declare @i int
-
set @i = 0
-
while(@i < 3)
-
begin
-
insert @months
-
default values
-
set @i = @i + 1
-
end
-
-
select [Month] = M.ID, Emp.Name, Emp.EmployeeID, Emp.[Year],
-
Expense = case when M.ID = 1 then JanExp
-
when M.ID = 2 then FebExp
-
when M.ID = 3 then MarExp
-
end,
-
Hours = case when M.ID = 1 then JanHrs
-
when M.ID = 2 then FebHrs
-
when M.ID = 3 then MarHrs
-
end
-
from #Employees Emp cross join @months M
Thank You So much. I got the solution.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 ...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |