473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Urgent HELP on Converting columns to rows

Dear Friends,

I want to show columns into rows. For example, I have a table,
TIMESHEET, with the following structure:

CREATE TABLE TIMESHEET
(
EMPNO CHAR(3),
WEEKEND_DATE DATE,
SAT SMALLINT,
SUN SMALLINT,
MON SMALLINT,
TUE SMALLINT,
WED SMALLINT,
THU SMALLINT,
FRI SMALLINT
);
INSERT INTO TIMESHEET VALUES ('001','2007-12-07',8,8,8,8,8,8,8);
INSERT INTO TIMESHEET VALUES ('001','2007-12-14',8,8,8,8,8,8,8);
INSERT INTO TIMESHEET VALUES ('001','2007-12-21',8,8,8,8,8,8,8);
INSERT INTO TIMESHEET VALUES ('001','2007-12-28',8,8,8,8,8,8,8);

Expected output :

EMPNO, DAYLY_DATE, HOURS
====== =========== ======
001 12/1/2007 8
001 12/2/2007 8
001 12/3/2007 8

Etc ....

I have a solution using UNION or UNION ALL but would like to avoid
them.

Thank you in advance for your help.

Shiva
Dec 8 '07 #1
3 2291
On Sat, 08 Dec 2007 14:56:49 -0000, Shivakumar <cs****@gmail.comwrote:
Dear Friends,

I want to show columns into rows. For example, I have a table,
TIMESHEET, with the following structure:

CREATE TABLE TIMESHEET
(
EMPNO CHAR(3),
WEEKEND_DATE DATE,
SAT SMALLINT,
SUN SMALLINT,
MON SMALLINT,
TUE SMALLINT,
WED SMALLINT,
THU SMALLINT,
FRI SMALLINT
);
INSERT INTO TIMESHEET VALUES ('001','2007-12-07',8,8,8,8,8,8,8);
INSERT INTO TIMESHEET VALUES ('001','2007-12-14',8,8,8,8,8,8,8);
INSERT INTO TIMESHEET VALUES ('001','2007-12-21',8,8,8,8,8,8,8);
INSERT INTO TIMESHEET VALUES ('001','2007-12-28',8,8,8,8,8,8,8);

Expected output :

EMPNO, DAYLY_DATE, HOURS
====== =========== ======
001 12/1/2007 8
001 12/2/2007 8
001 12/3/2007 8

Etc ....

I have a solution using UNION or UNION ALL but would like to avoid
them.
Any particular reason you want to avoid a UNION?

Anyway, it's possible to avoid by using a little LATERAL trickery to
explode the daily values into a 7-row table for each row in the original
table. Here's an example. I've revised the original INSERT statement to
check the output works correctly:

CREATE TABLE TIMESHEET (
EMPNO CHAR(3) NOT NULL,
WEEKEND_DATE DATE NOT NULL,
SAT SMALLINT,
SUN SMALLINT,
MON SMALLINT,
TUE SMALLINT,
WED SMALLINT,
THU SMALLINT,
FRI SMALLINT,
CONSTRAINT PK PRIMARY KEY (EMPNO, WEEKEND_DATE)
);

INSERT INTO TIMESHEET VALUES
('001','2007-12-07',1,2,3,4,5,6,7),
('001','2007-12-14',2,3,4,5,6,7,8),
('002','2007-12-07',3,4,5,6,7,8,9),
('002','2007-12-14',4,5,6,7,8,9,10);

SELECT
T.EMPNO,
E.DAILY_DATE,
E.HOURS
FROM
TIMESHEET T
INNER JOIN LATERAL(VALUES
(T.WEEKEND_DATE - 6 DAYS, T.SAT),
(T.WEEKEND_DATE - 5 DAYS, T.SUN),
(T.WEEKEND_DATE - 4 DAYS, T.MON),
(T.WEEKEND_DATE - 3 DAYS, T.TUE),
(T.WEEKEND_DATE - 2 DAYS, T.WED),
(T.WEEKEND_DATE - 1 DAY, T.THU),
(T.WEEKEND_DATE, T.FRI)
) AS E (DAILY_DATE, HOURS)
ON E.DAILY_DATE BETWEEN T.WEEKEND_DATE - 6 DAYS AND T.WEEKEND_DATE;

EMPNO DAILY_DATE HOURS
----- ---------- ------
001 01/12/2007 1
001 02/12/2007 2
001 03/12/2007 3
001 04/12/2007 4
001 05/12/2007 5
001 06/12/2007 6
001 07/12/2007 7
001 08/12/2007 2
001 09/12/2007 3
001 10/12/2007 4
001 11/12/2007 5
001 12/12/2007 6
001 13/12/2007 7
001 14/12/2007 8
002 01/12/2007 3
002 02/12/2007 4
002 03/12/2007 5
002 04/12/2007 6
002 05/12/2007 7
002 06/12/2007 8
002 07/12/2007 9
002 08/12/2007 4
002 09/12/2007 5
002 10/12/2007 6
002 11/12/2007 7
002 12/12/2007 8
002 13/12/2007 9
002 14/12/2007 10

SELECT * FROM TIMESHEET;

EMPNO WEEKEND_DATE SAT SUN MON TUE WED THU FRI
----- ------------ ------ ------ ------ ------ ------ ------ ------
001 07/12/2007 1 2 3 4 5 6 7
001 14/12/2007 2 3 4 5 6 7 8
002 07/12/2007 3 4 5 6 7 8 9
002 14/12/2007 4 5 6 7 8 9 10
Cheers,

Dave.
Dec 8 '07 #2
"Dave Hughes" <da**@waveform.plus.com
wrote in message news:op***************@stewie.waveform.plus.com...
Any particular reason you want to avoid a UNION?
Maybe because the instructor specified that a UNION cannot be used in the
solution?
Dec 8 '07 #3
All,

THANK YOU !

The reason for not using UNION is due to the number of lines my
original SQL has. If I use union or union all, it would be around 7
pages of SQL.
Again, thanks for all your help.
Dec 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
1
by: sk | last post by:
I have a situation where the list of tables(database) and the columns for each table must be displayed for selection. for each member(displayed in each row) i have to select a table and a column from...
13
by: scorpion53061 | last post by:
Very urgent and I am very close but need a little help to get me over the edge........ I need to write these columns to a html file with each row containing these columns (seperated by breaks)....
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...
1
by: SDRoy | last post by:
Hi I am using asp:BoundField code for my GridView Control. Something like: <asp:GridView ID="GridView2" runat="server" AutoGenerateEditButton="True" OnRowEditing="GridView2_RowEditing"> ...
3
by: ricolee99 | last post by:
Hi everyone, I have a problem that i have been trying to solve for awhile. I'm given a code where the purpose is to create a general dataset mapper. Given any dataset, i have a class,...
1
by: settyv | last post by:
Hi, I need to merge the rows in the datagrid based on the condition.Please let me know how can i do that.Here is the scenario: Present View: Short name Filed by A P A ...
1
by: raghavshastri | last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity. Here is a sample PGM image with 10...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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...

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.