Databases
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Databases

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old May 30th, 2008, 12:30 PM
maddyforums
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

Can someone please help me out in writing the Sql to concatenate the
Text_desc for each code for all the seq_nos in the ascending order of seq_no
and load into the target table.

Source table : S1

Code Seq_no Text_desc

1579 0 BCN5, C, ER75
1579 1 FP5, WC6, MATW, PRH
1579 2 T
1600 0 TW, FP5, AS5, MHSA15, DME20
1600 1 ER100, UR40

Target table: T1

Text_desc (concatenate all the strings for each code in ascending order of
seq_no)

code Text_desc

1579 BCN5, C, ER75FP5, WC6, MATW, PRHT
1600 TW, FP5, AS5, MHSA15, DME20ER100, UR40


Thanks,
Maddy


Reply With Quote
  #2  
Old June 4th, 2008, 08:50 AM
--CELKO--
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

Why are you formatting data in the back end? The basic principle of a
tiered architecture is that display is done in the front end and never
in the back end. This is a more basic programming principle than just
SQL and RDBMS.

Get a reporting tool so you can do a sort for your lists and lay them
out on paper or a screen in a way that a human being can read them.
It would also help if you knew what 1NF means, too.


Reply With Quote
  #3  
Old June 5th, 2008, 05:30 PM
Sybaseguru
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

Ivory towers are great - but some of us work in the real world and have to
produce systems that do stuff.

I agree its not very good practice to denormalise - but when I was at school
the text book said that atoms couldn't be split!

I suggest you try to offer more constructive advice - or don't bother.


wrote:

Why are you formatting data in the back end? The basic principle of a
tiered architecture is that display is done in the front end and never
in the back end. This is a more basic programming principle than just
SQL and RDBMS.
>

Get a reporting tool so you can do a sort for your lists and lay them
out on paper or a screen in a way that a human being can read them.
It would also help if you knew what 1NF means, too.


Reply With Quote
  #4  
Old June 5th, 2008, 05:30 PM
Sybaseguru
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

maddyforums wrote:

This looks a good one for cursors (there are some uses for them) or use
tsql loop - set up a select sorted on Code,Seq_no and loop until the Code
changes.


Can someone please help me out in writing the Sql to concatenate the
Text_desc for each code for all the seq_nos in the ascending order of
seq_no and load into the target table.
>

Source table : S1
>

Code Seq_no Text_desc

1579 0 BCN5, C, ER75
1579 1 FP5, WC6, MATW, PRH
1579 2 T
1600 0 TW, FP5, AS5, MHSA15, DME20
1600 1 ER100, UR40
>

Target table: T1
>

Text_desc (concatenate all the strings for each code in ascending order
of seq_no)
>

code Text_desc

1579 BCN5, C, ER75FP5, WC6, MATW, PRHT
1600 TW, FP5, AS5, MHSA15, DME20ER100, UR40
>
>

Thanks,
Maddy


Reply With Quote
  #5  
Old June 6th, 2008, 06:50 AM
David Kerber
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

In article <84eea91231c20@uwe>, u43936@uwe says
Can someone please help me out in writing the Sql to concatenate the
Text_desc for each code for all the seq_nos in the ascending order of seq_no
and load into the target table.

Look in your docs to see if you have the List() function available in
whatever version you are using. In ASA 9 and 10, it will do exactly
what you want here.



--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).

Reply With Quote
  #6  
Old June 6th, 2008, 10:10 AM
--CELKO--
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

>This looks a good one for cursors (there are some uses for them) or use
tsql loop - set up a select sorted on Code,Seq_no and loop until the
Code
changes. <<

I can write convoluted, poor SQL with the best of them :) I just
choose not to do bad coding. If you were a better SQL programmer, you
would know how to do this without a cursor. First, let's get some DDL
and bring the names closer to IS rules.

CREATE TABLE Foobar
(product_code CHAR(4) NT NULL,
vague_seq INTEGER NT NULL
CHECK (vague_seq >= 0),
PRIMARY KEY (product_code, vague_seq),
product_descr VARCHAR(200) NT NULL);

Here is the answer is a single, Standard SQL statement without
cursors.

SELECT X.product_code,
REPLACE
(MAX(CASE WHEN vague_seq = 0 THEN product_descr || ',' ELSE ''
END)
|| MAX(CASE WHEN vague_seq = 1 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 2 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 3 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 4 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 5 THEN product_descr ELSE '' END)
|| '?', ',?', '')
FRM Fooobar AS X
GRUP BY X.product_code;

The REPLACE() is to trim off the last comma. If the sequences are
longer, you can either add more MAX(CASE) or re-number this table
and run it again. either way, it should be about an order of
magnitude faster than a cursor.

Perhaps you should read a text book before you post again :)

Reply With Quote
  #7  
Old June 9th, 2008, 12:50 PM
Sybaseguru
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Concatenate multiple rows into a single row - Very Urgent

And when vague_seq = 6 or 7 or 99 or 999 ????

Sorry, after writing very effective Sybase tsql for 18 years, I still think
cursors or a TSQL loop will work well - and speedily with a cluster index
on code,seq_no



wrote:

This looks a good one for cursors (there are some uses for them) or use
tsql loop - set up a select sorted on Code,Seq_no and loop until the
Code
changes. <<
>

I can write convoluted, poor SQL with the best of them :) I just
choose not to do bad coding. If you were a better SQL programmer, you
would know how to do this without a cursor. First, let's get some DDL
and bring the names closer to IS rules.
>

CREATE TABLE Foobar
(product_code CHAR(4) NT NULL,
vague_seq INTEGER NT NULL
CHECK (vague_seq >= 0),
PRIMARY KEY (product_code, vague_seq),
product_descr VARCHAR(200) NT NULL);
>

Here is the answer is a single, Standard SQL statement without
cursors.
>

SELECT X.product_code,
REPLACE
(MAX(CASE WHEN vague_seq = 0 THEN product_descr || ',' ELSE ''
END)
|| MAX(CASE WHEN vague_seq = 1 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 2 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 3 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 4 THEN product_descr || ',' ELSE '' END)
|| MAX(CASE WHEN vague_seq = 5 THEN product_descr ELSE '' END)
|| '?', ',?', '')
FRM Fooobar AS X
GRUP BY X.product_code;
>

The REPLACE() is to trim off the last comma. If the sequences are
longer, you can either add more MAX(CASE) or re-number this table
and run it again. either way, it should be about an order of
magnitude faster than a cursor.
>

Perhaps you should read a text book before you post again :)


Reply With Quote
Reply

Viewing: Web Development Archives FAQs Databases > Concatenate multiple rows into a single row - Very Urgent


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway