 |
|
 |
|
Next: At what ANSI/SPARC level are you, when creating n..
|
| Author |
Message |
External

Since: Jul 20, 2006 Posts: 5
|
(Msg. 1) Posted: Fri Jul 13, 2007 9:56 pm
Post subject: query to combine a column Archived from groups: comp>databases>oracle>misc (more info?)
|
|
|
I have a query that returns something like this, which is in a pl/sql
function:
id description col3
---------------------------------
123 dingo stuff123
357 anteater stuff357
357 aardvark stuff357
357 wombat stuff357
677 kangaroo stuff677
What I want is a query that will transform the results of the above query
like this:
id description col3
-------------------------------------------------------------
123 dingo stuff123
357 anteater$$aardvark$$wombat stuff357
677 kangaroo stuff677
I want to remove the duplicate case_nums and combine their descriptions. The
'$$' would be for use as a marker for splitting up the string inJava.
Any suggestions on how to do this, if it can be done?
Alternatively - This query is used to populate a cursor, which is returned
by the function.
Is there a way to make the transformation in pl/sql and still return a
cursor from the function? >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jun 05, 2007 Posts: 13
|
(Msg. 2) Posted: Sat Jul 14, 2007 12:55 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jul 13, 8:44 pm, "Tim B" wrote:
> I have a query that returns something like this, which is in a pl/sql
> function:
>
> id description col3
>
> ---------------------------------
> 123 dingo stuff123
>
> 357 anteater stuff357
>
> 357 aardvark stuff357
>
> 357 wombat stuff357
>
> 677 kangaroo stuff677
>
> What I want is a query that will transform the results of the above query
> like this:
>
> id description col3
>
> -------------------------------------------------------------
> 123 dingo stuff123
>
> 357 anteater$$aardvark$$wombat stuff357
>
> 677 kangaroo stuff677
>
> I want to remove the duplicate case_nums and combine their descriptions. The
> '$$' would be for use as a marker for splitting up the string inJava.
>
> Any suggestions on how to do this, if it can be done?
>
> Alternatively - This query is used to populate a cursor, which is returned
> by the function.
>
> Is there a way to make the transformation in pl/sql and still return a
> cursor from the function?
Is there an upper limit of the number of records with the same ID?
Are you ordering them alphabetically within the group (because it
appears you are not, despite using aardvark as an example).
If there IS an upper limit (within reason, let's say 100), then a
simple query using various ranking functions will give you the
results. Look into the LEAD function, passing in 1,2,3,4,5.....100 as
the second parameter to retrieve data in the correct order and
grouping.
Alternatively you can use the row_number() analytical function and
partition by your ID number. I'll demo how this can be done on a table
with your results, but it could easily be altered to work on a
subquery returning your dataset above:
create table T (ID number, des varchar2(20), COL3 varchar2(20));
Table created.
insert into T values (123, 'dingo', 'stuff123');
1 row created.
insert into T values (357, 'anteater', 'stuff357');
1 row created.
insert into T values (357, 'aardvark', 'stuff357');
1 row created.
insert into T values (357, 'wombat', 'stuff357');
1 row created.
insert into T values (677, 'kangaroo', 'stuff677');
1 row created.
This shows the values of the row_number() function when partitioned
properly:
select ID, des, COL3,
row_number() over(partition by ID order by des asc) my_order
from T;
ID DES COL3 MY_ORDER
---------- -------------------- -------------------- ----------
123 dingo stuff123 1
357 aardvark stuff357 1
357 anteater stuff357 2
357 wombat stuff357 3
677 kangaroo stuff677 1
5 rows selected.
And this query will give you the concatenated results desired. I am
using an upper limit of 10 possible members with any particular ID
number. You may need to change this to another alternate number, you
should be able to get away with a few hundred (just keep the SQL
length below ~ 30K)
with W as
(
select ID, des, COL3,
row_number() over(partition by ID order by des asc) my_order
from T
)
select ID,
(select min(DES) from W W2 where W1.ID = W2.ID and MY_ORDER = 1) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 2) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 3) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 4) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 5) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 6) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 7) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER =  ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 9) ||
(select decode(min(DES), NULL, NULL, '$$' || min(DES)) from W W2 where
W1.ID = W2.ID and MY_ORDER = 10)
as MY_RESULT
from W W1
group by ID;
ID MY_RESULT
---------- --------------------------
123 dingo
677 kangaroo
357 aardvark$$anteater$$wombat
3 rows selected.
If you are using ADO and you cannot use the WITH statement on the
first line, put a "select(*) from" around the query. >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jul 20, 2006 Posts: 5
|
(Msg. 3) Posted: Sun Jul 15, 2007 3:55 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"dean" wrote in message
> On Jul 13, 8:44 pm, "Tim B" wrote:
> > I have a query that returns something like this, which is in a pl/sql
> > function:
> >
> > id description col3
> >
> > ---------------------------------
> > 123 dingo stuff123
> >
> > 357 anteater stuff357
> >
> > 357 aardvark stuff357
> >
> > 357 wombat stuff357
> >
> > 677 kangaroo stuff677
> >
> > What I want is a query that will transform the results of the above
query
> > like this:
> >
> > id description col3
> >
> > -------------------------------------------------------------
> > 123 dingo stuff123
> >
> > 357 anteater$$aardvark$$wombat stuff357
> >
> > 677 kangaroo stuff677
> >
> > I want to remove the duplicate case_nums and combine their descriptions.
The
> > '$$' would be for use as a marker for splitting up the string inJava.
> >
> > Any suggestions on how to do this, if it can be done?
> >
> > Alternatively - This query is used to populate a cursor, which is
returned
> > by the function.
> >
> > Is there a way to make the transformation in pl/sql and still return a
> > cursor from the function?
>
> Is there an upper limit of the number of records with the same ID?
> Are you ordering them alphabetically within the group (because it
> appears you are not, despite using aardvark as an example).
>
> If there IS an upper limit (within reason, let's say 100), then a
> simple query using various ranking functions will give you the
> results. Look into the LEAD function, passing in 1,2,3,4,5.....100 as
> the second parameter to retrieve data in the correct order and
> grouping.
>
Thanks for the suggestions and examples, Dean. In theory, there is no upper
limit, but in reality the number would should go over 100, and even if it
did, there would be no significant issues. And you are correct, ordering
alphabetically within the group is not required. I've worked it out using
lead, partition, and ranking functions.
Tim B >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jun 08, 2007 Posts: 30
|
(Msg. 4) Posted: Sun Jul 15, 2007 10:49 pm
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jul 14, 1:44 am, "Tim B" wrote:
> I have a query that returns something like this, which is in a pl/sql
> function:
>
> id description col3
>
> ---------------------------------
> 123 dingo stuff123
>
> 357 anteater stuff357
>
> 357 aardvark stuff357
>
> 357 wombat stuff357
>
> 677 kangaroo stuff677
>
> What I want is a query that will transform the results of the above query
> like this:
>
> id description col3
>
> -------------------------------------------------------------
> 123 dingo stuff123
>
> 357 anteater$$aardvark$$wombat stuff357
>
> 677 kangaroo stuff677
>
> I want to remove the duplicate case_nums and combine their descriptions. The
> '$$' would be for use as a marker for splitting up the string inJava.
>
> Any suggestions on how to do this, if it can be done?
>
> Alternatively - This query is used to populate a cursor, which is returned
> by the function.
>
> Is there a way to make the transformation in pl/sql and still return a
> cursor from the function?
Some further suggestions here:
http://www.williamrobertson.net/documents/one_row.html >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jun 05, 2007 Posts: 13
|
(Msg. 5) Posted: Mon Jul 16, 2007 12:56 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jul 15, 2:25 am, "Tim B" wrote:
> "dean" wrote in message
>
>
>
>
>
>
>
> > On Jul 13, 8:44 pm, "Tim B" wrote:
> > > I have a query that returns something like this, which is in a pl/sql
> > > function:
>
> > > id description col3
>
> > > ---------------------------------
> > > 123 dingo stuff123
>
> > > 357 anteater stuff357
>
> > > 357 aardvark stuff357
>
> > > 357 wombat stuff357
>
> > > 677 kangaroo stuff677
>
> > > What I want is a query that will transform the results of the above
> query
> > > like this:
>
> > > id description col3
>
> > > -------------------------------------------------------------
> > > 123 dingo stuff123
>
> > > 357 anteater$$aardvark$$wombat stuff357
>
> > > 677 kangaroo stuff677
>
> > > I want to remove the duplicate case_nums and combine their descriptions.
> The
> > > '$$' would be for use as a marker for splitting up the string inJava.
>
> > > Any suggestions on how to do this, if it can be done?
>
> > > Alternatively - This query is used to populate a cursor, which is
> returned
> > > by the function.
>
> > > Is there a way to make the transformation in pl/sql and still return a
> > > cursor from the function?
>
> > Is there an upper limit of the number of records with the same ID?
> > Are you ordering them alphabetically within the group (because it
> > appears you are not, despite using aardvark as an example).
>
> > If there IS an upper limit (within reason, let's say 100), then a
> > simple query using various ranking functions will give you the
> > results. Look into the LEAD function, passing in 1,2,3,4,5.....100 as
> > the second parameter to retrieve data in the correct order and
> > grouping.
>
> Thanks for the suggestions and examples, Dean. In theory, there is no upper
> limit, but in reality the number would should go over 100, and even if it
> did, there would be no significant issues. And you are correct, ordering
> alphabetically within the group is not required. I've worked it out using
> lead, partition, and ranking functions.
>
> Tim B- Hide quoted text -
>
> - Show quoted text -
Good - can you post the query here for reference? Would like to see
another take on this. >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jul 20, 2006 Posts: 5
|
(Msg. 6) Posted: Mon Jul 16, 2007 12:56 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"dean" wrote in message
> On Jul 15, 2:25 am, "Tim B" wrote:
> > "dean" wrote in message
> >
> >
> >
> >
> >
> >
> >
> > > On Jul 13, 8:44 pm, "Tim B" wrote:
> > > > I have a query that returns something like this, which is in a
pl/sql
> > > > function:
> >
> > > > id description col3
> >
> > > > ---------------------------------
> > > > 123 dingo stuff123
> >
> > > > 357 anteater stuff357
> >
> > > > 357 aardvark stuff357
> >
> > > > 357 wombat stuff357
> >
> > > > 677 kangaroo stuff677
> >
> > > > What I want is a query that will transform the results of the above
> > query
> > > > like this:
> >
> > > > id description col3
> >
> > > > -------------------------------------------------------------
> > > > 123 dingo stuff123
> >
> > > > 357 anteater$$aardvark$$wombat stuff357
> >
> > > > 677 kangaroo stuff677
> >
> > > > I want to remove the duplicate case_nums and combine their
descriptions.
> > The
> > > > '$$' would be for use as a marker for splitting up the string
inJava.
> >
> > > > Any suggestions on how to do this, if it can be done?
> >
> > > > Alternatively - This query is used to populate a cursor, which is
> > returned
> > > > by the function.
> >
> > > > Is there a way to make the transformation in pl/sql and still return
a
> > > > cursor from the function?
> >
> > > Is there an upper limit of the number of records with the same ID?
> > > Are you ordering them alphabetically within the group (because it
> > > appears you are not, despite using aardvark as an example).
> >
> > > If there IS an upper limit (within reason, let's say 100), then a
> > > simple query using various ranking functions will give you the
> > > results. Look into the LEAD function, passing in 1,2,3,4,5.....100 as
> > > the second parameter to retrieve data in the correct order and
> > > grouping.
> >
> > Thanks for the suggestions and examples, Dean. In theory, there is no
upper
> > limit, but in reality the number would should go over 100, and even if
it
> > did, there would be no significant issues. And you are correct, ordering
> > alphabetically within the group is not required. I've worked it out
using
> > lead, partition, and ranking functions.
> >
> > Tim B- Hide quoted text -
> >
> > - Show quoted text -
>
> Good - can you post the query here for reference? Would like to see
> another take on this.
>
Here it is. I've actually used lag in this example. This works, but there
are probably a cleaner and simpler ways to do it, maybe without 2 subqueries
I'm not planning to do much refinement on this, since I've worked out a way
to achieve the same result using Java in the web app I'm working on, only
without any concern about upper limits, and no need to split up concatenated
strings, so I'll be going with the Java solution.
with T as (
select '123' id, 'dingo' description, 'stuff123' col3 from dual
union
select '357' id, 'anteater' description, 'stuff357' col3 from dual
union
select '357' id, 'aardvark' description, 'stuff357' col3 from dual
union
select '357' id, 'wombat' description, 'stuff357' col3 from dual
union
select '677' id, 'kangaroo' description, 'stuff677' col3 from dual
)
select id, descr, col3 from (
select id, descr, col3, RANK() OVER (PARTITION BY id ORDER BY length(descr)
DESC) AS myrank from (
select id,
description
|| nvl2((lag(description, 1 ) over (partition by id order by description)),
('$$$'||lag(description, 1 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 2 ) over (partition by id order by description)),
('$$$'||lag(description, 2 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 3 ) over (partition by id order by description)),
('$$$'||lag(description, 3 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 4 ) over (partition by id order by description)),
('$$$'||lag(description, 4 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 5 ) over (partition by id order by description)),
('$$$'||lag(description, 5 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 6 ) over (partition by id order by description)),
('$$$'||lag(description, 6 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 7 ) over (partition by id order by description)),
('$$$'||lag(description, 7 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 8 ) over (partition by id order by description)),
('$$$'||lag(description, 8 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 9 ) over (partition by id order by description)),
('$$$'||lag(description, 9 ) over (partition by id order by description)),
'')
|| nvl2((lag(description, 10 ) over (partition by id order by description)),
('$$$'||lag(description, 10 ) over (partition by id order by description)),
'')
descr , col3
from T) )
where myrank = 1
Tim B >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jul 20, 2006 Posts: 5
|
(Msg. 7) Posted: Tue Jul 17, 2007 12:47 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"William Robertson" wrote in message
> On Jul 14, 1:44 am, "Tim B" wrote:
> > I have a query that returns something like this, which is in a pl/sql
> > function:
> >
> > id description col3
> >
> > ---------------------------------
> > 123 dingo stuff123
> >
> > 357 anteater stuff357
> >
> > 357 aardvark stuff357
> >
> > 357 wombat stuff357
> >
> > 677 kangaroo stuff677
> >
> > What I want is a query that will transform the results of the above
query
> > like this:
> >
> > id description col3
> >
> > -------------------------------------------------------------
> > 123 dingo stuff123
> >
> > 357 anteater$$aardvark$$wombat stuff357
> >
> > 677 kangaroo stuff677
> >
> > I want to remove the duplicate case_nums and combine their descriptions.
The
> > '$$' would be for use as a marker for splitting up the string inJava.
> >
> > Any suggestions on how to do this, if it can be done?
> >
> > Alternatively - This query is used to populate a cursor, which is
returned
> > by the function.
> >
> > Is there a way to make the transformation in pl/sql and still return a
> > cursor from the function?
>
> Some further suggestions here:
> http://www.williamrobertson.net/documents/one_row.html
Thanks, there's some useful stuff there. >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jun 05, 2007 Posts: 13
|
(Msg. 8) Posted: Tue Jul 17, 2007 1:03 pm
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jul 16, 8:47 pm, "Tim B" wrote:
> "William Robertson" wrote in message
>
>
>
>
>
>
>
> > On Jul 14, 1:44 am, "Tim B" wrote:
> > > I have a query that returns something like this, which is in a pl/sql
> > > function:
>
> > > id description col3
>
> > > ---------------------------------
> > > 123 dingo stuff123
>
> > > 357 anteater stuff357
>
> > > 357 aardvark stuff357
>
> > > 357 wombat stuff357
>
> > > 677 kangaroo stuff677
>
> > > What I want is a query that will transform the results of the above
> query
> > > like this:
>
> > > id description col3
>
> > > -------------------------------------------------------------
> > > 123 dingo stuff123
>
> > > 357 anteater$$aardvark$$wombat stuff357
>
> > > 677 kangaroo stuff677
>
> > > I want to remove the duplicate case_nums and combine their descriptions.
> The
> > > '$$' would be for use as a marker for splitting up the string inJava.
>
> > > Any suggestions on how to do this, if it can be done?
>
> > > Alternatively - This query is used to populate a cursor, which is
> returned
> > > by the function.
>
> > > Is there a way to make the transformation in pl/sql and still return a
> > > cursor from the function?
>
> > Some further suggestions here:
> >http://www.williamrobertson.net/documents/one_row.html
>
> Thanks, there's some useful stuff there.- Hide quoted text -
>
> - Show quoted text -
That does look like an elegant solution for reasonably small datasets.
However, its selecting the MAX records of a group of strings - if you
run the subquery:
SELECT deptno || ' ' || SYS_CONNECT_BY_PATH(ename,',') AS
concatenated
from
( SELECT deptno
, ename
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename)
AS curr
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename)
-1 AS prev
FROM emp)
CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
START WITH curr = 1
CONCATENATED
------------------
10 ,CLARK
10 ,CLARK,KING
10 ,CLARK,KING,MILLER
20 ,ADAMS
20 ,ADAMS,FORD
20 ,ADAMS,FORD,JONES
20 ,ADAMS,FORD,JONES,SCOTT
20 ,ADAMS,FORD,JONES,SCOTT,SMITH
30 ,ALLEN
30 ,ALLEN,BLAKE
30 ,ALLEN,BLAKE,JAMES
30 ,ALLEN,BLAKE,JAMES,MARTIN
30 ,ALLEN,BLAKE,JAMES,MARTIN,TURNER
30 ,ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
14 rows selected.
.... you can see that its taking the maximum strings (based on string
length) grouped by the ID number, and that to me seems to be
generating and then selecting from a potentially very large
intermediate dataset. >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jun 05, 2007 Posts: 13
|
(Msg. 9) Posted: Tue Jul 17, 2007 1:09 pm
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jul 16, 8:47 pm, "Tim B" wrote:
> "William Robertson" wrote in message
>
>
>
>
>
>
>
> > On Jul 14, 1:44 am, "Tim B" wrote:
> > > I have a query that returns something like this, which is in a pl/sql
> > > function:
>
> > > id description col3
>
> > > ---------------------------------
> > > 123 dingo stuff123
>
> > > 357 anteater stuff357
>
> > > 357 aardvark stuff357
>
> > > 357 wombat stuff357
>
> > > 677 kangaroo stuff677
>
> > > What I want is a query that will transform the results of the above
> query
> > > like this:
>
> > > id description col3
>
> > > -------------------------------------------------------------
> > > 123 dingo stuff123
>
> > > 357 anteater$$aardvark$$wombat stuff357
>
> > > 677 kangaroo stuff677
>
> > > I want to remove the duplicate case_nums and combine their descriptions.
> The
> > > '$$' would be for use as a marker for splitting up the string inJava.
>
> > > Any suggestions on how to do this, if it can be done?
>
> > > Alternatively - This query is used to populate a cursor, which is
> returned
> > > by the function.
>
> > > Is there a way to make the transformation in pl/sql and still return a
> > > cursor from the function?
>
> > Some further suggestions here:
> >http://www.williamrobertson.net/documents/one_row.html
>
> Thanks, there's some useful stuff there.- Hide quoted text -
>
> - Show quoted text -
Apologies if this is a double-post, the first one didn't seem to make
it, so here goes:
That does look like an elegant solution for reasonably small datasets.
However, its selecting the MAX records of a group of strings - if you
run the subquery:
SELECT deptno || ' ' || SYS_CONNECT_BY_PATH(ename,',') AS
concatenated
from
( SELECT deptno
, ename
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename)
AS curr
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY ename)
-1 AS prev
FROM emp)
CONNECT BY prev = PRIOR curr AND deptno = PRIOR deptno
START WITH curr = 1
CONCATENATED
------------------
10 ,CLARK
10 ,CLARK,KING
10 ,CLARK,KING,MILLER
20 ,ADAMS
20 ,ADAMS,FORD
20 ,ADAMS,FORD,JONES
20 ,ADAMS,FORD,JONES,SCOTT
20 ,ADAMS,FORD,JONES,SCOTT,SMITH
30 ,ALLEN
30 ,ALLEN,BLAKE
30 ,ALLEN,BLAKE,JAMES
30 ,ALLEN,BLAKE,JAMES,MARTIN
30 ,ALLEN,BLAKE,JAMES,MARTIN,TURNER
30 ,ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
14 rows selected.
.... you can see that its taking the maximum strings (based on string
length) grouped by the ID number, and that to me seems to be
generating and then selecting from a potentially very large
intermediate dataset. >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jul 19, 2007 Posts: 1
|
(Msg. 10) Posted: Thu Jul 19, 2007 10:58 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jul 14, 2:44 am, "Tim B" wrote:
> I have a query that returns something like this, which is in a pl/sql
> function:
>
> id description col3
>
> ---------------------------------
> 123 dingo stuff123
>
> 357 anteater stuff357
>
> 357 aardvark stuff357
>
> 357 wombat stuff357
>
> 677 kangaroo stuff677
>
> What I want is a query that will transform the results of the above query
> like this:
>
> id description col3
>
> -------------------------------------------------------------
> 123 dingo stuff123
>
> 357 anteater$$aardvark$$wombat stuff357
>
> 677 kangaroo stuff677
>
> I want to remove the duplicate case_nums and combine their descriptions. The
> '$$' would be for use as a marker for splitting up the string inJava.
>
> Any suggestions on how to do this, if it can be done?
>
> Alternatively - This query is used to populate a cursor, which is returned
> by the function.
>
> Is there a way to make the transformation in pl/sql and still return a
> cursor from the function?
You can also create custom aggregate function
create or replace type concat_str as object
(
str_ varchar2(4000),
static function odciaggregateinitialize(ctx in out concat_str)
return number,
member function odciaggregateiterate
(
self in out concat_str
,value in varchar2
) return number,
member function odciaggregateterminate
(
self in concat_str
,returnvalue out varchar2
,flags in number
) return number,
member function odciaggregatemerge
(
self in out concat_str
,ctx in concat_str
) return number
);
/
show errors
create or replace type body concat_str is
--
static function odciaggregateinitialize(ctx in out concat_str)
return number is
begin
ctx := concat_str(null);
return odciconst.success;
end;
--
member function odciaggregateiterate
(
self in out concat_str
,value in varchar2
) return number is
begin
if self.str_ is null
then
self.str_ := value;
else
self.str_ := self.str_ || ';' || value;
end if;
return odciconst.success;
end;
--
member function odciaggregateterminate
(
self in concat_str
,returnvalue out varchar2
,flags in number
) return number is
begin
returnvalue := self.str_;
return odciconst.success;
end;
--
member function odciaggregatemerge
(
self in out concat_str
,ctx in concat_str
) return number is
begin
if ctx.str_ is null
then
null;
else
self.str_ := self.str_ || ';' || ctx.str_;
end if;
return odciconst.success;
end;
--
end;
/
show errors
create or replace function fce_concat_str(input varchar2) return
varchar2
parallel_enable
aggregate using concat_str;
/
show errors
column a format a10
column b format 999
column a_concatenated format a20
--
prompt concatenated - aggregate
select b
,fce_concat_str(a) a_concatenated
from (select 'a' || level a
,mod(level, 2) b
from dual
connect by level <= 5)
group by b;
--
prompt concatenated - analytic - no sort
select a
,b
,fce_concat_str(a) over(partition by b) a_concatenated
from (select 'a' || level a
,mod(level, 2) b
from dual
connect by level <= 5);
--
prompt concatenated - analytic - sort
prompt (default = rows between unbounded preceding and current row)
select a
,b
,fce_concat_str(a) over(partition by b order by a)
a_concatenated
from (select 'a' || level a
,mod(level, 2) b
from dual
connect by level <= 5);
--
prompt concatenated - analytic - sort
prompt (rows between unbounded preceding and unbounded following)
select a
,b
,fce_concat_str(a) over(partition by b order by a rows between
unbounded preceding and unbounded following) a_concatenated
from (select 'a' || level a
,mod(level, 2) b
from dual
connect by level <= 5);
S. >> Stay informed about: query to combine a column |
|
| Back to top |
|
 |  |
External

Since: Jul 02, 2007 Posts: 2
|
(Msg. 11) Posted: Fri Jul 20, 2007 7:56 am
Post subject: Re: query to combine a column [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
| Related Topics: | Format A Calculated Column in a Query? - Good afternoon, I am try to figure out how to format a calculated column in a query but am not finding how to do it or if I can do it? Here is my Query and Results: SQL> Select RTRIM(Members.LastName), RTRIM(Members.FirstName), Position.Position,...
SQL Column Heading - Hi guys, When we execute a select stmt in sqlplus we don't get the column headings full , it is shown according to the data in that column. So, i want to display the column headings in full as it was mentioned , while creating the table. Can tell by..
Column Name Change - Good Morning, Is it possible to change a column name of a table after the table has been created? I have a table called Members that have FirstName, LastName columns. The problem I have is the information that got put into the table had the names..
[Q] Sorting a column - Hi I have a table T with two columns NAME (VARCHAR2) and POS (NUMBER). I want to write an UPDATE query, which updates the column POS such that its values correspond to the alphabetical order of NAME. UPDATE T t1 SET pos = (SELECT ROWID FROM T t2..
Finding tables that contain a certain column value - I'm currently working in Oracle 8i and I'm attempting to write an SQL statement to provide a listing of tables where the column_name contains a certain value, but I'm not having any luck. I can obtain a listing of all the availabe tables with the column... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|