473,382 Members | 1,387 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,382 software developers and data experts.

Transposing rows into columns

Hello,

is there any quite easy solution for the problem of transposing the rows
into the columns? I have the following table with some data in it:

dealer date 09.00 10.00 11.00 12.00 13.00 14.00
-----------------------------------------------------------------
1 2004-10-01 1/1 2/3 3/3 3/4 4/5 0/3
1 2004-10-02 0/1 1/3 1/3 1/4 3/5 1/3

/and so on.../

I'd like to prepare a SELECT query in the stored procedure that will
display this data in the following form:

dealer date hour reservations
------------------------------------------------------------------
1 2004-10-01 09.00 1/1
1 2004-10-01 10.00 2/3
1 2004-10-01 11.00 3/3
1 2004-10-01 12.00 3/4
1 2004-10-01 13.00 4/5
1 2004-10-01 14.00 0/3
1 2004-10-02 09.00 0/1
1 2004-10-02 10.00 1/3
1 2004-10-02 11.00 1/3
1 2004-10-02 12.00 1/4
1 2004-10-02 13.00 3/5
1 2004-10-02 14.00 1/3

Is it possible to do it using some simple solution? I saw some possible
solutions but they are a bit confusing. Any ideas? Thanks in advance.

...:: fabio
Jul 20 '05 #1
2 4693

"Fabio" <fabio_in_wawa@to_wez_wywal.poczta.fm> wrote in message
news:MP************************@news.interia.pl...
Hello,

is there any quite easy solution for the problem of transposing the rows
into the columns? I have the following table with some data in it:

dealer date 09.00 10.00 11.00 12.00 13.00 14.00
-----------------------------------------------------------------
1 2004-10-01 1/1 2/3 3/3 3/4 4/5 0/3
1 2004-10-02 0/1 1/3 1/3 1/4 3/5 1/3

/and so on.../

I'd like to prepare a SELECT query in the stored procedure that will
display this data in the following form:

dealer date hour reservations
------------------------------------------------------------------
1 2004-10-01 09.00 1/1
1 2004-10-01 10.00 2/3
1 2004-10-01 11.00 3/3
1 2004-10-01 12.00 3/4
1 2004-10-01 13.00 4/5
1 2004-10-01 14.00 0/3
1 2004-10-02 09.00 0/1
1 2004-10-02 10.00 1/3
1 2004-10-02 11.00 1/3
1 2004-10-02 12.00 1/4
1 2004-10-02 13.00 3/5
1 2004-10-02 14.00 1/3

Is it possible to do it using some simple solution? I saw some possible
solutions but they are a bit confusing. Any ideas? Thanks in advance.

..:: fabio


Rather than write a confusing query, it would probably be better to improve
your table design - do you really have columns called 09:00 etc.? The time
should be part of the data, and by putting it in the table you can write a
much easier query - see below.

I suspect that you might also want to store reservations as integer, not
character data, because that makes it much easier to do calculations, but
that depends on what the numbers mean.

If you really can't change the table design, I suggest you post CREATE TABLE
and INSERT statements that someone else can paste into Query Analyzer to
recreate your table with some sample data - that means we don't have to
guess what data types you have, what the primary key is, etc.

Simon
create table fabio (
dealer int not null,
res_time datetime not null,
reservations char(3) not null,
constraint PK_fabio primary key (dealer, res_time)
)
go

insert into fabio
select 1, '2004-10-01T09:00:00', '1/3'
union all
select 1, '2004-10-01T10:00:00', '2/3'
union all
select 1, '2004-10-01T11:00:00', '3/3'
union all
select 1, '2004-10-01T12:00:00', '3/4'
union all
select 1, '2004-10-01T13:00:00', '4/5'
go

select
dealer,
convert(char(10), res_time, 105) as 'date',
convert(char(5), res_time, 114) as 'hour',
reservations
from
fabio
order by
dealer, 'date', 'hour'
go

drop table fabio
go
Jul 20 '05 #2
On Wed, 29 Sep 2004 19:08:37 +0200, Fabio wrote:
Hello,

is there any quite easy solution for the problem of transposing the rows
into the columns? I have the following table with some data in it:

dealer date 09.00 10.00 11.00 12.00 13.00 14.00
-----------------------------------------------------------------
1 2004-10-01 1/1 2/3 3/3 3/4 4/5 0/3
1 2004-10-02 0/1 1/3 1/3 1/4 3/5 1/3

/and so on.../

I'd like to prepare a SELECT query in the stored procedure that will
display this data in the following form:

dealer date hour reservations
------------------------------------------------------------------
1 2004-10-01 09.00 1/1
1 2004-10-01 10.00 2/3
1 2004-10-01 11.00 3/3
1 2004-10-01 12.00 3/4
1 2004-10-01 13.00 4/5
1 2004-10-01 14.00 0/3
1 2004-10-02 09.00 0/1
1 2004-10-02 10.00 1/3
1 2004-10-02 11.00 1/3
1 2004-10-02 12.00 1/4
1 2004-10-02 13.00 3/5
1 2004-10-02 14.00 1/3

Is it possible to do it using some simple solution? I saw some possible
solutions but they are a bit confusing. Any ideas? Thanks in advance.

..:: fabio


For this specific problem as you describe it, it's not too hard to
construct an appropriate SELECT, if a bit laborious:

SELECT dealer, date, '09.00' AS hour, [09.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '10.00' AS hour, [10.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '11.00' AS hour, [11.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '12.00' AS hour, [12.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '13.00' AS hour, [13.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '14.00' AS hour, [14.00] AS reservations FROM T1

For the sake of ordering, you probably ought to make that a derived table
and wrap it in another SELECT with an ORDER BY clause:

SELECT dealer, date, hour, reservations
FROM (
SELECT dealer, date, '09.00' AS hour, [09.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '10.00' AS hour, [10.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '11.00' AS hour, [11.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '12.00' AS hour, [12.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '13.00' AS hour, [13.00] AS reservations FROM T1
UNION ALL
SELECT dealer, date, '14.00' AS hour, [14.00] AS reservations FROM T1
) AS T2
ORDER BY dealer, date, hour

And if any of your hour columns are nullable, you may need to outer join
this with a table of hours to avoid missing rows.

To do this in *general*, however, i.e. to write a procedure that you can
reuse that can handle this without knowing the columns beforehand, is much
more difficult. It would probably involve dynamic SQL.

Very often, it's better to handle this at the client, not at the server,
e.g. in an Excel PivotTable, an MS-Access Crosstab Query, an OLAP cube, or
whatever is available.
Jul 20 '05 #3

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

Similar topics

8
by: Leszek Gruszka | last post by:
Hello! I need to transpose some columns into rows and rows into columns. I know, tha i can do it by cursor, but i don't know how make it... I read a lot about it, but still don't understand......
2
by: Howard William | last post by:
Help. I am a bit flummoxed by the problem of how to transpose "normalized" (in the database sense) data records into columns of for a data entry form, and then back again when the user is...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
9
by: Classic-Car-World Ltd | last post by:
Hi, I have some data in a table in columns which I need to convert into rows. This is specification data for some tools etc. The data is currently in the following format: Product No, order,...
1
by: trizub | last post by:
I have a table of populations of cities (identified by country, state, cities) on particular dates. How do I transpose the date values in my rows to a date value column that lists populations for...
1
by: TimHop12 | last post by:
Lets consider the following query: Select Col1, Col2 from Table1 where Colx = 'A5100650867' This gives result as: Col1 Col2 E-mail Address xyz@GMAIL.com...
11
by: Haydee Zimmerman | last post by:
Hoping someone can help me. I have 2 Access tables with a one to many relationship. the 1 side table holds the billing information, the many table side holds the billing id and modifier#. Now I...
2
by: owuraku | last post by:
Hi guys I am have two excel workbooks. One book has data formatted according rows. Example: ROW 1: Loan # ; Principal Amount ; Interest Rate ; Date The other book needs to be formatted so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.