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 9484
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.
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.
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: 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...
|
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...
|
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...
|
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++...
|
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...
|
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...
|
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...
|
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.
|
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', {...
| |