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

Transposing

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...
Can someone help me?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
8 2848

"Leszek Gruszka" <le************@kana.com.pl> wrote in message
news:41**********************@news.newsgroups.ws.. .
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...
Can someone help me?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Crosstabs or pivoting may be easier to do in the client application than on
the server side (especially if you don't know the number of rows/columns in
advance), but of course there are ways to do it on the server. These
articles include some code samples:

http://www.aspfaq.com/show.asp?id=2462

If you've read about it already, then perhaps you can give a simple example,
and explain what you don't understand? The best idea is to post CREATE TABLE
and INSERT statements to make a table with some sample data, and also give
an example of the output you want - other people can then just cut and paste
into Query Analyzer to test your example.

Simon
Jul 20 '05 #2
All pivot examples use agregate functions. I have a lot of records, that
can't be sum or anything else. It must simple leave the values. What i
must put instead of sum?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

"Leszek Gruszka" <le************@kana.com.pl> wrote in message
news:41**********************@news.newsgroups.ws.. .
All pivot examples use agregate functions. I have a lot of records, that
can't be sum or anything else. It must simple leave the values. What i
must put instead of sum?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


In a crosstab, the aggregate functions are often not really aggregates -
they're there to remove all non-NULL values (since aggregate functions
ignore NULLs). But as I said before, you really need to post a simple
example, or we can only guess what you mean.

Simon
Jul 20 '05 #4
Ok. That's simple example:

Data | Station | AuditBase | NoCDROM
---------------------------------------------------
21/01/1999| Station1 | 1 | 1
21/01/1999| Station2 | 0 | 0
21/01/1999| Station3 | 1 | 1
22/02/2002| Station1 | 1 | 0
22/02/2002| Station2 | 1 | 0
22/02/2002| Station3 | 1 | 0
23/03/2003| Station1 | 0 | 1
23/03/2003| Station2 | 1 | 1
23/03/2003| Station3 | 0 | 1

I want to transpose to:

Data | Parameter |Station1 | Station2 | Station3
--------------------------------------------------------
21/01/1999| AuditBase | 1 | 0 | 1
21/01/1999| NoCDROM | 1 | 0 | 1
22/02/2002| AuditBase | 1 | 1 | 1
22/02/2002| NoCDROM | 0 | 0 | 0
23/03/2003| AuditBase | 0 | 1 | 0
23/03/2003| NoCDROM | 1 | 1 | 1

Station names will changed in position with parameters.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
One more thing - Data (only last data for each station) will be as
parametr too.

My query for it is:

Select * FROM KanaSecRep AS jeden
WHERE data = (SELECT DISTINCT MAX(data) FROM KanaSecRep AS dwa
WHERE jeden.station = dwa.station)
Order by Data DESC

So my first table after this SELECT will looks something like that:

Data | Station | AuditBase | NoCDROM
---------------------------------------------------
23/03/2003| Station1 | 0 | 1
23/03/2003| Station2 | 1 | 1
23/03/2003| Station3 | 0 | 1

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #6

"Leszek Gruszka" <le************@kana.com.pl> wrote in message
news:41**********************@news.newsgroups.ws.. .
One more thing - Data (only last data for each station) will be as
parametr too.

My query for it is:

Select * FROM KanaSecRep AS jeden
WHERE data = (SELECT DISTINCT MAX(data) FROM KanaSecRep AS dwa
WHERE jeden.station = dwa.station)
Order by Data DESC

So my first table after this SELECT will looks something like that:

Data | Station | AuditBase | NoCDROM
---------------------------------------------------
23/03/2003| Station1 | 0 | 1
23/03/2003| Station2 | 1 | 1
23/03/2003| Station3 | 0 | 1

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


I had a look at your data, but it look like a case where TSQL is not the
best solution (see code below). You need to do rows to columns (stations)
and also columns to rows (AuditBase, NoCDROM) in the same result set, and
the solution must be dyamic because you don't know the number of stations in
advance.

This is possible, using cursors and temp tables, but personally I wouldn't
try it - any TSQL code will be awkward, and also very difficult to maintain.
If you need to change the way the output is displayed, or add more columns,
then it will take more work to do that.

Since the real question is formatting the data, I would use a reporting tool
if you have it (Crystal Reports, Business Objects etc.). If not, then using
VB, C#, Perl etc. will be a lot clearer and simpler to maintain - those
languages support looping, which makes it relatively easy to iterate over
something like an ADO RecordSet object.

Finally, please do not post table data in the format above - this means that
someone else has to write their own CREATE TABLE and INSERT statements:

http://www.aspfaq.com/etiquette.asp?id=5006

Simon

create table leszek (
data datetime not null,
station int not null,
auditbase int not null,
nocdrom int not null
)
go

insert into leszek select '19990121', 1, 1, 1
insert into leszek select '19990121', 2, 0, 0
insert into leszek select '19990121', 3, 1, 1
insert into leszek select '20020222', 1, 1, 0
insert into leszek select '20020222', 2, 1, 0
insert into leszek select '20020222', 3, 1, 0
insert into leszek select '20030323', 1, 0, 1
insert into leszek select '20030323', 2, 1, 1
insert into leszek select '20030323', 3, 0, 1
go

/* The following query would need to be completely dynamic in order to
** provide a solution to your question. Building this dynamically in TSQL
** would be awkward, and difficult to extend. It would be better to return
** a simpler result set, then format it in a client application.
*/

select * from (
select
data as 'Data',
'AuditBase' as 'Parameter',
max(case when station = 1 then auditbase else null end) as 'station1',
max(case when station = 2 then auditbase else null end) as 'station2',
max(case when station = 3 then auditbase else null end) as 'station3'
from leszek
group by data
union all
select
data,
'NoCDROM',
max(case when station = 1 then nocdrom else null end) as 'station1',
max(case when station = 2 then nocdrom else null end) as 'station2',
max(case when station = 3 then nocdrom else null end) as 'station3'
from leszek
group by data
) dt
order by 1, 2
Jul 20 '05 #7
You're a man :)

Is there any chance to replace case values (station = 1, 2, 3 etc),
because i don't know what values it can gain? Maybe some parameters?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #8
I forgot....
In my table i have 2 columns, that contains string values.
Column is LegalNoticeCaption and contains string like "Hello - welcome
into our network". I can't case it :(

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #9

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

Similar topics

2
by: Fabio | last post by:
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 ...
0
by: Christoph Lutz | last post by:
Hi, I would like to transpose a table in mysql. I have looked on the web for a while but nothing seems to fit the bill, so I just want to clarify if I have to write some php or if I can use a...
1
by: nipper1999 | last post by:
I have a set of data coming in from a text file that looks like: Date ID Value 01/01/2005 1 Value1 01/01/2005 2 Value2 01/01/2005 3 Value3 01/01/2005 4 ...
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...
5
by: SakyMoto | last post by:
I hope someone can help me with my problem. I have searched the internet for days for a solution, but nothing i found seemed to work. The following is what i have now: appartmentid code 100306...
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: Caspian | last post by:
Dear All, I'm attempting to create a query that will transpose repeated fields into a single table structure. Can anyone think of how this can be done as I'm stumped at the minute? I'd like to...
4
by: m.wanstall | last post by:
Hi there, I have a large table based around a CustomerNumber with various details about that customer (originally imported and augmented from an Exchange Server)...now, a single customer can...
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: 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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.