473,785 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2866

"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 LegalNoticeCapt ion 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
4724
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 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
0
1999
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 SQL query (preferred). What I would like to do is to use the values in a row as column headings during output, so that I can enter different sets of data into the same table and display it in a readable format afterwards. Let me explain: I have say...
1
1285
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 Value4 01/01/2005 5 Value5 01/01/2005 6 Value6 01/01/2005 7 Value7
2
6265
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 finished. My previous solution to this was to filter by query or SQL for the observations for a particular set of criteria, then go through the resulting records and assign them to a non-normalized work table containing ONE RECORD with the rows x...
5
3910
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 Wip 100306 Zandbak 100306 Glijbaan 100306 Klimrek 100306 Schommel
9
10668
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, description, value 1000, 1, model No, Ak272 1000, 2, Size, 10mm 1000, 3, Length, 20mm 1001, 1, Model No, Ak273
1
3138
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 do this without having to create a cursor due to the overheads and performance issues associated with cursors. The table may also include additional fields which I'm not interested in. Serial Data is like this.............
4
1708
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 have many email addresses and thus will have multiple entries in this table but this isn't what I want. Is there a way to relatively simply take any duplicate CustomerNumber records, take their EmailAddress field and append it in a new column...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
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 we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.