473,386 Members | 1,745 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.

Transpose rows to column

Hi

I have a table with data like this

ID Type
1 A
1 B
1 C
2 A
2 C
How can I tranfer it to this format
ID Type1 Type2 Type3
1 A B C
2 A Null C

Nov 12 '05 #1
4 39164
Almost all report writers offer cross-tabulation.

Standard SQL cannot solve the general case (unknown number of columns),
although the cube functionality will do the tabulation for you. However,
have to do the cross-tabulation layout yourself.

"Tuong Do" <tu*****@hotmail.com> wrote in message
news:cm**********@news-02.connect.com.au...
Hi

I have a table with data like this

ID Type
1 A
1 B
1 C
2 A
2 C
How can I tranfer it to this format
ID Type1 Type2 Type3
1 A B C
2 A Null C


Nov 12 '05 #2
try the Case statement...something like this

select ID,
min(case type='A' then 'A' else null) type_A
min(case type='B' then 'B' else null) type_B
group by ID

Tuong Do wrote:
Hi

I have a table with data like this

ID Type
1 A
1 B
1 C
2 A
2 C
How can I tranfer it to this format
ID Type1 Type2 Type3
1 A B C
2 A Null C


Nov 12 '05 #3
Thank for the quick reply,
I will try the create a view with the case statement

<da**********@gmail.com> wrote in message
news:10********************@c13g2000cwb.googlegrou ps.com...
try the Case statement...something like this

select ID,
min(case type='A' then 'A' else null) type_A
min(case type='B' then 'B' else null) type_B
group by ID

Tuong Do wrote:
Hi

I have a table with data like this

ID Type
1 A
1 B
1 C
2 A
2 C
How can I tranfer it to this format
ID Type1 Type2 Type3
1 A B C
2 A Null C

Nov 12 '05 #4
A little generalized way.
You do not need to know values of Type beforehand.
But, still need to set maximum number of Types.

WITH Types AS (
SELECT Type
, ROWNUMBER() OVER(ORDER BY Type) AS rn
FROM (SELECT DISTINCT Type
FROM Transpose
) AS R
)
SELECT ID
, MIN(CASE WHEN rn = 1 THEN a.type END) type1
, MIN(CASE WHEN rn = 2 THEN a.type END) type2
, MIN(CASE WHEN rn = 3 THEN a.type END) type3
, MIN(CASE WHEN rn = 4 THEN a.type END) type4
, MIN(CASE WHEN rn = 5 THEN a.type END) type5
FROM Transpose A
, Types T
WHERE a.type = t.type
GROUP BY
ID
;
------------------------------------------------------------------------------

ID TYPE1 TYPE2 TYPE3 TYPE4 TYPE5
----------- ----- ----- ----- ----- -----
1 A B C - -
2 A - C - -

2 record(s) selected.
Nov 12 '05 #5

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

Similar topics

1
by: Chris Smith | last post by:
Experience Posters, Sorry if this is not the right group to post this question. He is my issue; Is there a way without the use of 3rd party controls, to transpose the rows of a datatable to...
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
3
by: bogtom | last post by:
I have records ID Sku Name Date 2 41 Blair 01/04/03 3 45 John 03/04/03 that should look like... ID 2 3 Sku 41 45
8
by: m.wanstall | last post by:
Hi All, This is similar to a question I asked earlier however this is following a more "correct" way of doing things. I have normalised and summarised an Exchange addressbook (a few thousand...
5
by: stevepl7 | last post by:
I want to make the rows become column headings in an Access table or query. The data changes based on input to a form. It looks like this: A B N1 1 N2 2 N3 3 N4 4 Where A and...
4
by: samitasahu | last post by:
Hi, I m showing College Details in a table in jsp . I want to transpose the column data into rows in the table. How to do that in jsp coding.
1
by: lenygold via DBMonster.com | last post by:
Chris Eaton created a SP to to transpose rows to columns : Here is an example of a rowtocol stored proc that takes a SQL statement as the first paramter, a delimiter as the second parameter and the...
1
by: brionesl | last post by:
Hi, I have seen other threads with the same question but I'm not too familiar with Access and their solutions often include writing VB code or very complex SQL statements. What I want to do is...
5
by: jenniferhelen | last post by:
I have been searching threads for a while and found the instructions listed below many times, however when I get to step 6 and select to save, I always receive the following error, "The information...
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:
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?
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
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.