These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: HQL to Concatenate values of a column by grouping - stragg
PostPosted: Thu Mar 27, 2008 1:20 am 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
I was wondering if there was a way to write HQL to solve the following problem which was already posted has a solution in native SQL. In fact the alternative solution using HQL takes humongous time as I have re-loop so many times to create a solution. Is writing SQL queries the only solution?

Source: http://www.experts-exchange.com/Databas ... 87594.html

I have a table Company with columns like Phone_Num, Fax and email where they can have multiple values.
These values should be return in a single row.
The table below shed more light on the scenario
Company | Phone_Num | Fax | email
AG 0345 45 ab@rt.com
AG 0378 47 ab@rt.com
GB 0256 34 gb@rt.com
GB 0298 34 gb2@rt.com
RD 0449 67 rd@rt.com

I would the result of my query be

Company | Phone_Num | Fax | email
AG 0345,0378 45,47 ab@rt.com
GB 0256,0256 34 gb@rt.com,gb2@rt.com
RD 0449 67 rd@rt.com

I am thinking of creating a function given the name of the company, it returns a
string in the pattern the "Phone_NUm#Fax&email". Using string functions like substring and length
I would split it in the respective variables.
My problem is how can I create the oracle function.
Any help would be welcome.

Solution:
sdstuber:
You are aggregating your data, so you must use a GROUP BY or an analytic...
If you're concerned with the complexity of STRAGG, don't be.
A user defined aggregate is what you're trying to write, so you would just be reinventing that function
anyway.

Plus, Tom Kyte's site provides all the code and is ready to run "as is" so there's really no downside to it.


To use stragg as an analytic instead of an aggregate, it would look something like this...

SELECT DISTINCT company,
stragg(DISTINCT phone_num) over (partition by company) phone_num,
stragg(DISTINCT fax) over (partition by company) fax,
stragg(DISTINCT email) over (partition by company) email
FROM your_table


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.