Home » SQL & PL/SQL » SQL & PL/SQL » DECODE and SUM query (Oracle 9i)
icon2.gif  DECODE and SUM query [message #550616] Wed, 11 April 2012 01:36 Go to next message
ora_nov
Messages: 35
Registered: January 2012
Member
Hi,

I have table test_1 like

CREATE TABLE test_1 (
src_page varchar2(150)
, ref_page varchar2(150)
, page_desc varchar2(100)
, no_hits NUMBER
)
;

And sample data
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'http://www.google.com', null, 1);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'http://www.google.com', 'Hello', 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'http://www.google.com', null, 1);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', null, null, 5);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', null, null, 1);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'http://www.google.com', 'Hello', 3);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'abcd', null, 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'abcc', null, 6);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', null, null, 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', null, null, 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'abcd', null, 6);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'http://www.google.com', 'Empty', 3);
INSERT INTO test_1 VALUES ('http://www1.ora_test_1.com', 'http://www.google.com', null, 5);

INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'http://www.google.com', null, 1);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'http://www.google.com', 'Hello', 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'http://www.google.com', null, 1);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', null, null, 5);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', null, null, 1);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'http://www.google.com', 'Hello', 3);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'abcd', null, 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'abcc', null, 6);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', null, null, 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', null, null, 2);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'abcd', null, 6);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'http://www.google.com', 'Empty', 3);
INSERT INTO test_1 VALUES ('http://www1.ora_test_2.com', 'http://www.google.com', null, 5);



Desired output:

src_page	                ref_page	        page_desc  sum(no_hits)
http://www1.ora_test_1.com	http://www.google.com	Hello	   20
http://www1.ora_test_1.com	abcd	                null	   8
http://www1.ora_test_1.com	abcc	                null	   6
http://www1.ora_test_1.com	null	                null	   10
http://www1.ora_test_2.com	http://www.google.com	Hello	   20
http://www1.ora_test_2.com	abcd	                null	   8
http://www1.ora_test_2.com	abcc	                null	   6
http://www1.ora_test_2.com	null	                null	   10


For same src_page and ref_page, if page_desc is different it should take the value other than null and empty and update the page_desc, sum no_hits. In the above example it is Hello. So it should come as single row instead of 3 rows (If we do group by we get 3 rows).

TIA




[Updated on: Wed, 11 April 2012 01:43]

Report message to a moderator

Re: DECODE and SUM query [message #550618 is a reply to message #550616] Wed, 11 April 2012 01:42 Go to previous messageGo to next message
Michel Cadot
Messages: 54223
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Could you, PLEASE, align the columns in the result, INCLUDING the column headers.

Quote:
For same src_page, ref_page if page_desc is different it should take the value other than null and


And what if there are several page_desc not null?

Regards
Michel
Re: DECODE and SUM query [message #550621 is a reply to message #550618] Wed, 11 April 2012 01:45 Go to previous messageGo to next message
ora_nov
Messages: 35
Registered: January 2012
Member
I have aligned the result set.

For same src_page and ref_page, if page_desc is different it should take the value other than null and Empty and update the page_desc
Re: DECODE and SUM query [message #550623 is a reply to message #550621] Wed, 11 April 2012 01:53 Go to previous messageGo to next message
Michel Cadot
Messages: 54223
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
And if there are SEVERAL not null values which one it takes?

Regards
Michel
Re: DECODE and SUM query [message #550624 is a reply to message #550623] Wed, 11 April 2012 02:01 Go to previous messageGo to next message
Michel Cadot
Messages: 54223
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Assuming there can be only 1 not null value:
SQL> col src_page format a26
SQL> col ref_page format a21
SQL> col page_desc format a10
SQL> with
  2   data as (
  3     select src_page, ref_page, no_hits,
  4            last_value(nullif(page_desc,'Empty') ignore nulls) 
  5              over (partition by src_page, ref_page order by null) page_desc
  6     from test_1
  7   )
  8  select src_page, ref_page, page_desc, sum(no_hits) no_hits
  9  from data
 10  group by src_page, ref_page, page_desc
 11  order by src_page, ref_page, page_desc
 12  /
SRC_PAGE                   REF_PAGE              PAGE_DESC     NO_HITS
-------------------------- --------------------- ---------- ----------
http://www1.ora_test_1.com abcc                                      6
http://www1.ora_test_1.com abcd                                      8
http://www1.ora_test_1.com http://www.google.com Hello              15
http://www1.ora_test_1.com                                          10
http://www1.ora_test_2.com abcc                                      6
http://www1.ora_test_2.com abcd                                      8
http://www1.ora_test_2.com http://www.google.com Hello              15
http://www1.ora_test_2.com                                          10

Regards
Michel

[Updated on: Wed, 11 April 2012 02:05]

Report message to a moderator

Re: DECODE and SUM query [message #550629 is a reply to message #550624] Wed, 11 April 2012 02:35 Go to previous messageGo to next message
saipradyumn
Messages: 131
Registered: October 2011
Location: Hyderabad
Senior Member
Hi Michel ,


Following is also one of the way for the same requirement ,

SQL>  col src_page format a26
SQL> col ref_page format a21
SQL> col page_desc format a10
SQL> 
SQL> with data as
  2   (select t1.src_page,
  3           t1.ref_page,
  4           t1.page_desc,
  5           sum(t1.no_hits) over(partition by t1.src_page, t1.ref_page 
order by t1.src_page, t1.ref_page) sum,
  6           row_number() over(partition by t1.src_page, t1.ref_page 
order by decode(lower(page_desc), 'empty', null, page_desc)) row_order
  7      from test_1 t1)
  8  select t1.src_page, t1.ref_page, t1.page_desc, t1.sum
  9    from data t1
 10   where t1.row_order = 1
 11  ;
 
SRC_PAGE                   REF_PAGE              PAGE_DESC         SUM
-------------------------- --------------------- ---------- ----------
http://www1.ora_test_1.com abcc                                      6
http://www1.ora_test_1.com abcd                                      8
http://www1.ora_test_1.com http://www.google.com Hello              15
http://www1.ora_test_1.com                                          10
http://www1.ora_test_2.com abcc                                      6
http://www1.ora_test_2.com abcd                                      8
http://www1.ora_test_2.com http://www.google.com Hello              15
http://www1.ora_test_2.com                                          10
 
8 rows selected
 
SQL> 



Please let me know if i am wrong

Thanks
Sai Pradyumn

[Updated on: Wed, 11 April 2012 10:57] by Moderator

Report message to a moderator

Re: DECODE and SUM query [message #550637 is a reply to message #550629] Wed, 11 April 2012 03:17 Go to previous messageGo to next message
Michel Cadot
Messages: 54223
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
It is not exactly the same thing (it defers when there are more than 1 not null page_desc for a same src_page,ref_page) but it is valid in the same way.

Regards
Michel
Re: DECODE and SUM query [message #550692 is a reply to message #550637] Wed, 11 April 2012 10:45 Go to previous messageGo to next message
ora_nov
Messages: 35
Registered: January 2012
Member
Thanks Michel, I was not clear with req. For each combination of src_page and ref_page there should be only one row and page_desc should be other than null and 'Empty'. Always 'Empty' and null should be merged to the valid page_desc for each combination of src_page and ref_page.


So the sum for below combination should be 20:

SRC_PAGE                   REF_PAGE              PAGE_DESC         SUM
-------------------------- --------------------- ---------- ----------
http://www1.ora_test_1.com http://www.google.com Hello              20


Re: DECODE and SUM query [message #550693 is a reply to message #550692] Wed, 11 April 2012 11:01 Go to previous messageGo to next message
Michel Cadot
Messages: 54223
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
So the sum for below combination should be 20:

Why? I don't see how you get 20 from your test case. Explain.

Regards
Michel

[Updated on: Wed, 11 April 2012 11:01]

Report message to a moderator

Re: DECODE and SUM query [message #550704 is a reply to message #550616] Wed, 11 April 2012 13:43 Go to previous message
ora_nov
Messages: 35
Registered: January 2012
Member
Sorry Michel,
You are right the sum is 15 not 20 . Razz

[Updated on: Wed, 11 April 2012 13:43]

Report message to a moderator

Previous Topic: Re: I got a problem while writing a simple procedure (split from hijacked thread by bb)
Next Topic: REG:ALTERING COLUMN SIZES of nested tables (merged 4)
Goto Forum:
  


Current Time: Thu May 23 18:53:37 CDT 2013

Total time taken to generate the page: 0.78234 seconds