Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2009, 01:57 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Query Help Needed

Hello,

I am kinda newbie to the SQL so please excuse if the question is dumb, but I don't know hot to figure it out.
I am working with an Oracle database (11g).
It all comes down to this:
I have a table that is:

column1 varchar2(10)
column2 number(5,2)
column3 varchar2(5)

I have to create a query that will group results by column1 (easy), sums the column2 values for identical column1 values and (here's the tricky part) to concatenate the texts in column3.
Something like:
column1 column2 column3
a__________1_________x
b__________2_________y
c__________3_________z
a__________15________w

I need the report to be something like:
column1 column2 column3
a_______1+15(16)____xw

select column1, sum(column2), column3 from table group by column1; -> will not work due to the restriction regarding the group by clause and aggregation functions. Using a subquery doesn't seem to do it, as I cannot concatenate the values in different rows in column3.

Does anyone know how this can be achieved?


Thanks a lot.
 
Old June 19th, 2009, 07:16 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I don't use Oracle, but in SQL Server you can use COALESCE( ) in a user defined function to do this.

Try googling for "delimited list via sql" or "oracle delimited list via sql" and see if you don't get some hits. (Without "oracle" in there, you'll get back results for SQL Server, I know.)
 
Old June 21st, 2009, 04:39 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

foxbymariuss:
Normally in cases such as the one posted, I usually load the table into MS Access and manipulate the table information. However my suggestion is as follows:
Code:
DECLARE
 v_col1in col1val%TYPE;
 v_col2in col2val%TYPE;
 v_col3in col3val%TYPE;
 v_col1cnt PLS_INTEGER := 0;
 v_col2sum PLS_INTEGER :=0;
 v_col3concat VARCHAR2(20) := v_col3in;
 
CURSOR colmix_cur IS
  SELECT count(a.col1in), sum(a.col2in), concat(a.col3in)
  FROM intbl1 a
  WHERE 1=1
  GROUP BY a.col1in
  HAVING count(a.col1in) > 0
  ORDER BY a.col1in;
 
BEGIN
   DBMS_OUTPUT.ENABLE(500);
   OPEN colmix_cur;
   LOOP
        FETCH colmix_cur INTO v_col1cnt, v_col2sum, v_vol3concat;
        EXIT WHEN colmix_cur%NOTFOUND;
 
        DBMS_OUTPUT.PUT_LINE (v_col1in || ','  || v_col2sum || ',' || v_vol3concat);
  END LOOP;
  CLOSE colmix_cur;
END;
This is just a suggestion. I hope it helps.
__________________
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.

Last edited by peace95; June 21st, 2009 at 04:43 AM.. Reason: extra spacing in code...no problem though.
 
Old June 22nd, 2009, 02:30 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot

I will first try doing the processing in the vb.net, as I don't find how to do it with sql.

I am grateful for your help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed with JOIN Query icecrew SQL Server 2000 3 October 16th, 2006 11:59 AM
Nested Query Help Needed Corey Access 2 April 26th, 2006 02:17 PM
Help needed with calculation in a query Lovegroover Access 6 August 18th, 2005 10:57 AM
Query for two conditions needed doosti SQL Language 2 November 9th, 2004 06:24 AM
SQL syntax for my query - guru needed badgolfer Access 2 September 20th, 2003 01:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.