A simple Group by in ORACLE needed

Madhu_A
Madhu_A used Ask the Experts™
on
Hi Experts,

Here is a table: product
Product_id Tags
1                 Football
1                 Soccer
1                 Tennisl
2                 Football
2                 Golf
3                 Rugby

Now I want to group all the tags against a product_id and tried:
1)select product_id, tags from product group by product_id --> didnt work, oracle wants all the columns in select to be in group by
2)select product_id, tags from product group by product_id -> gave me these results:
Product_id Tags
1                 Football
1                 Soccer
1                 Tennisl
2                 Football
2                 Golf
3                 Rugby

Which is basically the same table data. I think the above query woprks in SQL server 200 but we have Oracle here.

What i want is this:
Product_id Tags
1                 Football, Soccer, Tennis
2                 Football, Golf
3                 Rugby
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I think this is what you are looking for:

------------------Create a function first-----------------------------------------
CREATE OR REPLACE function myfunc1(in_id in number)
   return varchar2
  IS
 v_ret varchar2(100) := ' ';
  begin
  for rec in (select tags from product where pid = in_id)
  loop
   
  IF (length(v_ret) > 1 ) then
  v_ret := v_ret||','||rec.tags;
  ELSE
  v_ret := v_ret||rec.tags;
  END IF;
  end loop;
  return v_ret;
 end;

----------------------------------------------------------------------
then just run following statement:

select distinct pid,myfunc1(pid) tags from product;


hope this will solve your problem.

Author

Commented:
Please read my second sql as:
2)select product_id, tags from product group by product_id, tags -> gave me these results:
Product_id Tags
1                 Football
1                 Soccer
1                 Tennisl
2                 Football
2                 Golf
3                 Rugby
--------

Anand, thanks for your response but I am not the owner of the database and will not be finding myself creating a function in the DB. Is there a way to do it, with just sql queries?
Maybe instead of using function just use pl/sql block. and you should be ok i feel

something like this
declare
 v_ret varchar2(100) := ' ';
  begin
for id in (select distinct pid from product)
loop
  for rec in (select tags from product where pid = id.pid)
  loop
   
  IF (length(v_ret) > 1 ) then
  v_ret := v_ret||','||rec.tags;
  ELSE
  v_ret := v_ret||rec.tags;
  END IF;
  end loop;
select distinct pid,v_ret  tags from product where pid =id;
end loop;
 
 end;
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

SujithData Architect
Commented:
select product_id, ltrim(sys_connect_by_path(tags, ','), ',') tag_list
from (
select product_id, tags , row_number() over(partition by product_id order by tags) rn,
count(*) over(partition by product_id) cn
from tbl1 )
where rn = cn
start with rn = 1
connect by prior rn = rn -1
and prior product_id = product_id
/
Forced accept.

Computer101
EE Admin

Author

Commented:
Thanks Sujith and Anand for contributing with your answers.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial