dbTalk Databases Forums  

Multiple rows combined into onerow and onecolumn separated by a special character

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Multiple rows combined into onerow and onecolumn separated by a special character in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
thetaamommy@gmail.com
 
Posts: n/a

Default Multiple rows combined into onerow and onecolumn separated by a special character - 10-19-2007 , 03:22 PM






Hi All :


CREATE TABLE TABLEA(Person Varchar(20), Country Varchar(20), Subject
Varchar(20), Type Char(1))

INSERT INTO TABLEA VALUES ('Einstein', 'Germany', 'Physics', 'P')
INSERT INTO TABLEA VALUES ('Kant', 'Germany', 'Philosophy', 'Q')
INSERT INTO TABLEA VALUES ('Kafka', 'Germany', 'Writer' , 'W')

INSERT INTO TABLEA VALUES ('Aristotle', 'Greece', 'Philosophy', 'Q')
INSERT INTO TABLEA VALUES ('Archimedes', 'Greece', 'Physics', 'P')
INSERT INTO TABLEA VALUES ('Homer', 'Greece', 'Writer' , 'W')

SELECT * FROM TABLEA

I am on SQL 2000.
I need an output where i have to have a resultset grouped on Type, but
the results in one row.

In the resultset I need

TypeP Person Type P Country, Type Q Person, Type Q Country, Type
W Person Type W Country
---------------------------------------------------------------------------------------------------------------------
Einstein:Archimedes Germany:Greece Kant:Aristotle Germany:Greece
Kafka:Homer Germany:Greece


************************************************** *************
I have written a puesdo-cursor code to do the same, but if there is a
way to do as a set operation, that would be great

Please select as a whole and past in query analyser as the resultset
is all overlaid when i paste in this box.


Thank you
RS


Reply With Quote
  #2  
Old   
David Portas
 
Posts: n/a

Default Re: Multiple rows combined into onerow and onecolumn separated by a special character - 10-19-2007 , 03:35 PM






<thetaamommy (AT) gmail (DOT) com> wrote

Quote:
Hi All :


CREATE TABLE TABLEA(Person Varchar(20), Country Varchar(20), Subject
Varchar(20), Type Char(1))

INSERT INTO TABLEA VALUES ('Einstein', 'Germany', 'Physics', 'P')
INSERT INTO TABLEA VALUES ('Kant', 'Germany', 'Philosophy', 'Q')
INSERT INTO TABLEA VALUES ('Kafka', 'Germany', 'Writer' , 'W')

INSERT INTO TABLEA VALUES ('Aristotle', 'Greece', 'Philosophy', 'Q')
INSERT INTO TABLEA VALUES ('Archimedes', 'Greece', 'Physics', 'P')
INSERT INTO TABLEA VALUES ('Homer', 'Greece', 'Writer' , 'W')

SELECT * FROM TABLEA

I am on SQL 2000.
I need an output where i have to have a resultset grouped on Type, but
the results in one row.

In the resultset I need

TypeP Person Type P Country, Type Q Person, Type Q Country, Type
W Person Type W Country
---------------------------------------------------------------------------------------------------------------------
Einstein:Archimedes Germany:Greece Kant:Aristotle Germany:Greece
Kafka:Homer Germany:Greece


************************************************** *************
I have written a puesdo-cursor code to do the same, but if there is a
way to do as a set operation, that would be great

Please select as a whole and past in query analyser as the resultset
is all overlaid when i paste in this box.


Thank you
RS


You didn't explain what determines the ordering of each pair of names.
There's nothing in the table to tell us that so I've just combined MIN and
MAX to return the ordering you say you want.

SELECT
MAX(CASE WHEN Type = 'P' THEN Person END) p1,
MIN(CASE WHEN Type = 'P' THEN Person END) p2,
MIN(CASE WHEN Type = 'P' THEN Country END) c1,
MAX(CASE WHEN Type = 'P' THEN Country END) c2,
MAX(CASE WHEN Type = 'Q' THEN Person END) p3,
MIN(CASE WHEN Type = 'Q' THEN Person END) p4,
MIN(CASE WHEN Type = 'Q' THEN Country END) c3,
MAX(CASE WHEN Type = 'Q' THEN Country END) c4,
MAX(CASE WHEN Type = 'W' THEN Person END) p5,
MIN(CASE WHEN Type = 'W' THEN Person END) p6,
MIN(CASE WHEN Type = 'W' THEN Country END) c5,
MAX(CASE WHEN Type = 'W' THEN Country END) c6
FROM TableA;


--
David Portas




Reply With Quote
  #3  
Old   
thetaamommy@gmail.com
 
Posts: n/a

Default Re: Multiple rows combined into onerow and onecolumn separated by a special character - 10-24-2007 , 11:27 AM



Hi David :

Thank you much.
This gives me 2 values, min and max.
but, I will have a total of 4 persons on philosophy and it could be
either 0 or 1 or 2 or 3 or 4 persons in philisophy.
Is it possible to do a set operation for that ?

What i came up with is 4 sql set stmts.
first one is insert stmt of a random philisopher in temp table:

second will be update stmt and in where clause excluding what is
already in temp table
third stmt is another update to 3rd column excluding the two already
in temp table
and fourth stmt

Is there a better way ?
Thank you


Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Multiple rows combined into onerow and onecolumn separated by a special character - 10-24-2007 , 04:27 PM



(thetaamommy (AT) gmail (DOT) com) writes:
Quote:
Thank you much.
This gives me 2 values, min and max.
but, I will have a total of 4 persons on philosophy and it could be
either 0 or 1 or 2 or 3 or 4 persons in philisophy.
Is it possible to do a set operation for that ?
It could certainly help if you could produce a more extensive set of
sample data, also explain the logic to get the desired result.

But if I am guessing correctly what you are trying to achieve, the
answer is that on SQL 2000, your prospects are bleak, at as long as
there is no upper limit of how many persons there can be.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.