A simple Group by in ORACLE needed

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
Madhu_AAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anandmahajanCommented:
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.
0
Madhu_AAuthor 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?
0
anandmahajanCommented:
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;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

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

SujithData ArchitectCommented:
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
/
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
Madhu_AAuthor Commented:
Thanks Sujith and Anand for contributing with your answers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.