If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > DB2 Really Complex query...

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-12, 13:01
junaid377 junaid377 is offline
Registered User
 
Join Date: Jan 2012
Posts: 40
DB2 Really Complex query...

ok guys buckle up .. here is the scenario...

I have a table with three columns

id, Company, Country , and project_number


A company can have multiple projects in multiple country.....


Now I have to create a view that shows all the projects of a company in a particular country... for example there copmany has id 1 and in country 2 it has 8 projects (1,2,3,4,5,6,7,8) then one row should return this...

1| 2 |1;2;3;4;5;6;7;8


how can I acheive this... I have tried the following code but it is can't handle the length it creates... The max length for the 3rd column will be 36789 and my varchar can't handle that much length ... any ideas???
Reply With Quote
  #2 (permalink)  
Old 01-27-12, 13:15
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
This type of question gets asked often and has been answered each time. Start here: how to concatenate rows without using table variabls and temp tables for this case

Andy
Reply With Quote
  #3 (permalink)  
Old 01-27-12, 13:21
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
The max length for the 3rd column will be 36789 and my varchar can't handle that much length
Use two LISTAGG, CAST to CLOB, then concatenate them.
Reply With Quote
  #4 (permalink)  
Old 01-27-12, 14:32
junaid377 junaid377 is offline
Registered User
 
Join Date: Jan 2012
Posts: 40
I am trying to do this

REPLACE(XMLSERIALIZE(CONTENT XMLAGG(XMLELEMENT(Name "d", company)) AS VARCHAR (20000)), '<d>', ' ')

but getting this error

[Error Code: -433, SQL State: 22001] Value "<d>HGGHGKJGK</d><d>JGHJGHJ</d><d>GFFGFJFG</d><d>" is too long.. SQLCODE=-433, SQLSTATE=22001, DRIVER=3.57.82


I copied this code from somewhere... it is making a xml tree.... I just need ; between the numbers....
Reply With Quote
  #5 (permalink)  
Old 01-27-12, 14:38
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
How about to replace "AS VARCHAR (20000)" with "AS CLOB(1M)"?
Reply With Quote
  #6 (permalink)  
Old 01-27-12, 14:49
junaid377 junaid377 is offline
Registered User
 
Join Date: Jan 2012
Posts: 40
Next I am trying to perform the following operation to get the desired result... won't clob make that fail....

VARCHAR(REPLACE(REPLACE (REPLACE (REPLACE (REPLACE ( REPLACE(SUBSTR(company,1, LENGTH
(company)-1 ),'</d>', ';'),'<d/>', ' ' ), '>', ' '),'<d/','') ,'</d',''),' ', '' )) AS
CNTR_NUM...


any suggestions???
Reply With Quote
  #7 (permalink)  
Old 01-27-12, 15:07
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Apply XMLAGG(and REPLACE, so on) for each subset of company which are small enough(e.g. every 100 companies) to fit in VARCHAR, CAST to CLOB, then concatenate them,
like ...
Quote:
Originally Posted by tonkuma View Post
Use two LISTAGG, CAST to CLOB, then concatenate them.
Reply With Quote
  #8 (permalink)  
Old 01-27-12, 15:14
junaid377 junaid377 is offline
Registered User
 
Join Date: Jan 2012
Posts: 40
can you write me a query. or psuedo code..... I can't make sense of this.... Will be really grateful.....
Reply With Quote
  #9 (permalink)  
Old 01-27-12, 15:52
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
can you write me a query. or psuedo code.....
OK. I think that I can do.

Please publish DDL and INSERT statement to populate the table and expected result from them, to test queries.
And, what DB2 version/release and platform OS are you using?
Reply With Quote
  #10 (permalink)  
Old 01-27-12, 15:58
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Originally Posted by tonkuma View Post
Apply XMLAGG(and REPLACE, so on) for each subset of company which are small enough(e.g. every 100 companies) to fit in VARCHAR, CAST to CLOB, then concatenate them,
like ...
Another way might be to use SYSFUN.REPLACE which accepts CLOB for the arguments.

Please see Information Center
REPLACE - IBM DB2 9.7 for Linux, UNIX, and Windows
Quote:
REPLACE scalar function

...

The schema is SYSIBM. The SYSFUN version of the REPLACE function continues to be available but it is not sensitive to the database collation.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On