Concatenate rows into a single cell

Hi,

I’m creating a report in full client. I have the table in the following format:

Category  Frequency   IsTrend Sample Numbers if Trend = Yes

A1                  0.80        Y            200
                                                  300
A2                   1.50       N              -
A3                    0.70      Y             400
                                                    500

My question is : I would to display the data in this format:

A1                     0.80        Y        200, 300
A2                       1.50       N          -
A3                       0.70       Y         400,500

Is there a way to concatenate the sample numbers currently in different rows into a single cell separated with comma. The sample numbers are extracted from a variable called : “Sample Numbers if Trend is Yes”

Thanks a lot,
Stara


stara (BOB member since 2005-12-02)

Yes you can by using ‘&’

Its hard to say the exact formula from here, but you can


KhoushikTTT :us: (BOB member since 2005-02-24)

Thanks for the reply. Could you please explain briefly how the & can be used.

Thanks a lot,
Stara


stara (BOB member since 2005-12-02)

Different rows merged onto the same cell in one row… I think you’ll need to do that at database level.


jac :australia: (BOB member since 2005-05-10)

Yes, you can do this both at BO level and DB level using your case statements in the SQL. Did you try to use the CONCAT function ?

Thanks,
BO_Analyst.


BO_Analyst (BOB member since 2005-06-09)

Thanks for your reply. I’m not sure how to use the CONCAT function. Could you please help me with a sample?

Thanks a lot,
Stara


stara (BOB member since 2005-12-02)

What’s there to CONCAT? It’s the same column in different Records! Maybe you could try something with previous()?


jobjoris :netherlands: (BOB member since 2004-06-30)

I cannot use the Previous function since I’m not sure how many rows are returned for each category. It’s not possible to hard code the count.

Thanks,
S
tara


stara (BOB member since 2005-12-02)

There isn’t a function in BO to do this. The BO-only solution would be to do something ugly with multiple aliases and outer joins in Designer to concatenate values from the alias if they exist.

I’ve faced this issue before and a better way to handle is at the database level. If you are using Oracle, create a custom function to concatenate the values. Here is an example where I had to concatenate multiple disease states for grants which exist in one row per grant id - disease state combination and can have one or many disease states per grant.

create or replace 
function get_transposedDS( GID in Integer ) 
return varchar2 
is 
l_str varchar2(2000) default null; 
l_sep varchar2(1) default null; 
begin 
for x in ( select DISEASE_STATE from GRANT_DISEASE_STATE where GRANT_ID = GID ) loop 
l_str := l_str || l_sep || x.DISEASE_STATE; 
l_sep := ','; 
end loop; 
return l_str; 
end;

Next, I created a derived table in my universe called Grant_All_Disease_States as:

SELECT GRANT_ID, get_transposedDS(GRANT_ID) AS DISEASE_STATE_LIST
FROM GRANT_DISEASE_STATE
GROUP BY GRANT_ID

Finally, my universe object Disease State List defined as:

Grant_All_Disease_States.DISEASE_STATE_LIST

The end result of this is that if I queried the following data:

GRANT_ID    DISEASE_STATE
----------------------------
1                  X
1                  Y
2                  A
3                  B
3                  C
3                  D

…I would see:

GRANT_ID    DISEASE_STATE_LIST
----------------------------
1                  X,Y
2                  A
3                  B,C,D

Sandy Smyth :us: (BOB member since 2002-08-19)

@Sandy: Is their something similar that can be done at the universe level only inorder to achieve the same result :?: :idea:

I have the same requirement but dont have access to create anything in the database. We use DB2 9.5 as backend and are currently in XIR2 SP5.

Thanks in advance.
Adnan.


boe_bods :india: (BOB member since 2008-03-14)

Unfortunately there isn’t a clean way to do this straight from Designer. The database function can handle a variable number of strings to concatenate. In Designer, you can alias the table with the strings to concatenate and do a seriea of outer joins possibly, but it is complicated, ugly, and may have a performance hit.


Sandy Smyth :us: (BOB member since 2002-08-19)

Hi This Can be done by creating a simple object in the universe level.

Suppose you have company Type under which Number of companies will be there and if you want to display the company names of Company Type in a single cell. then Create a object ’ ‘+company name+’,’

Made this object as measure. Then drag company Type and Name to your report then you wll see the data like
Company type Company Name
A x,y,z
B k,l,m

But there will be some limitation on the number of characters it is going to display, If data exceeds that limit then it will through #Overflow error


divyap (BOB member since 2010-09-08)

I did what divya mentioned, but strangely for only few Company types the order in which Company Names are listed is in Descending order. As i mentioned this is not the case for all the Company types but only for few.

For example:
Expected result:


CompanyT     CompanyN
A            a,b,c
B            a,b,c
C            x,y,z

Actual Result:


CompanyT     CompanyN
A            a,b,c
B            c,b,a
C            x,y,z

Why is this happening? Any help? We are using XIR2 against DB2 database.

Thanks.


boe_bods :india: (BOB member since 2008-03-14)

In your get_transposedDS Function, have you tried applying a sort to the subselect?

That’s the piece that’s assembling the values into a concatenated list, so it should be where you look to correct the order…


digpen :us: (BOB member since 2002-08-15)

Since there is nothing that confirms that the second line actually belongs together with the first line (like a key field)

No, this is not possible.

Peter


pderop (BOB member since 2010-10-27)