dbTalk Databases Forums  

COUNT Function

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss COUNT Function in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Magnet
 
Posts: n/a

Default COUNT Function - 05-17-2010 , 11:01 AM






Hi,

I'm tryng to use the Analytical function COUNT(*):

SELECT tag_id, tag_name, tag_count
FROM (SELECT t.tag_id, tag_name,
COUNT(*) OVER (PARTITION BY t.tag_id) tag_count,
ROW_NUMBER() OVER (PARTITION BY t.tag_id ORDER BY
t.tag_id) rnum
FROM commentary.article_tags a, commentary.tags t
WHERE t.tag_id = a.tag_id(+))
WHERE rnum = 1;

Problem I am having is that for records in the TAGS table which do not
match in the ARTICLE_TAGS table are still being returned with 1 row.
I'm looking for it to return 0, as there were no matches.

Thought it was the way the join was working, but I do not think so as
I've tried different combos.

Any ideas?

Reply With Quote
  #2  
Old   
Michel Cadot
 
Posts: n/a

Default Re: COUNT Function - 05-17-2010 , 11:09 AM






"The Magnet" <art (AT) unsu (DOT) com> a écrit dans le message de news: d34c6b28-c9b1-4bb8-b973-94aece1cc1ee...oglegroups.com...
Quote:
Hi,

I'm tryng to use the Analytical function COUNT(*):

SELECT tag_id, tag_name, tag_count
FROM (SELECT t.tag_id, tag_name,
COUNT(*) OVER (PARTITION BY t.tag_id) tag_count,
ROW_NUMBER() OVER (PARTITION BY t.tag_id ORDER BY
t.tag_id) rnum
FROM commentary.article_tags a, commentary.tags t
WHERE t.tag_id = a.tag_id(+))
WHERE rnum = 1;

Problem I am having is that for records in the TAGS table which do not
match in the ARTICLE_TAGS table are still being returned with 1 row.
I'm looking for it to return 0, as there were no matches.

Thought it was the way the join was working, but I do not think so as
I've tried different combos.

Any ideas?
Do not count after the outer join because you will have of course at least
one row, count inside the outer joined table and nvl to 0.

Regards
Michel

Reply With Quote
  #3  
Old   
The Magnet
 
Posts: n/a

Default Re: COUNT Function - 05-17-2010 , 11:19 AM



On May 17, 10:09*am, "Michel Cadot" <micadot{at}altern{dot}org> wrote:
Quote:
"The Magnet" <a... (AT) unsu (DOT) com> a écrit dans le message de news: d34c6b28-c9b1-4bb8-b973-94aece1cc... (AT) y21g2000vba (DOT) googlegroups.com...
| Hi,
|
| I'm tryng to use the Analytical function COUNT(*):
|
| SELECT tag_id, tag_name, tag_count
| FROM (SELECT t.tag_id, tag_name,
| * * * * * * COUNT(*) OVER (PARTITION BY t.tag_id) tag_count,
| * * * * * * ROW_NUMBER() OVER (PARTITION BY t.tag_id ORDER BY
| t.tag_id) rnum
| * * *FROM commentary.article_tags a, commentary.tags t
| * * *WHERE t.tag_id = a.tag_id(+))
| WHERE rnum = 1;
|
| Problem I am having is that for records in the TAGS table which do not
| match in the ARTICLE_TAGS table are still being returned with 1 row.
| I'm looking for it to return 0, as there were no matches.
|
| Thought it was the way the join was working, but I do not think so as
| I've tried different combos.
|
| Any ideas?

Do not count after the outer join because you will have of course at least
one row, count inside the outer joined table and nvl to 0.

Regards
Michel

Not sure what you mean "after" the outer join. I thought I was
already counting within.

Reply With Quote
  #4  
Old   
Carlos
 
Posts: n/a

Default Re: COUNT Function - 05-17-2010 , 11:30 AM



On May 17, 5:01*pm, The Magnet <a... (AT) unsu (DOT) com> wrote:
Quote:
Hi,

I'm tryng to use the Analytical function COUNT(*):

SELECT tag_id, tag_name, tag_count
FROM (SELECT t.tag_id, tag_name,
* * * * * * *COUNT(*) OVER (PARTITION BY t.tag_id) tag_count,
* * * * * * *ROW_NUMBER() OVER (PARTITION BY t.tag_id ORDERBY
t.tag_id) rnum
* * * FROM commentary.article_tags a, commentary.tags t
* * * WHERE t.tag_id = a.tag_id(+))
WHERE rnum = 1;

Problem I am having is that for records in the TAGS table which do not
match in the ARTICLE_TAGS table are still being returned with 1 row.
I'm looking for it to return 0, as there were no matches.

Thought it was the way the join was working, but I do not think so as
I've tried different combos.

Any ideas?
I think i don't get it.

Why not:

CARLOS (AT) XE (DOT) bequeath> select * from tags;

TAG_ID TAG_NAME
---------- ----------
1 TAG 1
2 TAG 2
3 TAG 3

CARLOS (AT) XE (DOT) bequeath> select * from article_tags;

ARTICLE_ID ARTICLE_NAME TAG_ID
---------- ------------ ----------
1 ARTICLE 11 1
1 ARTICLE 11 1
1 ARTICLE 12 1
2 ARTICLE 21 2

CARLOS (AT) XE (DOT) bequeath> SELECT t.tag_id,
2 t.tag_name,
3 COUNT(a.tag_id) tag_count
4 FROM article_tags a,
5 tags t
6 WHERE t.tag_id = a.tag_id(+)
7 group by t.tag_id, t.tag_name;

TAG_ID TAG_NAME TAG_COUNT
---------- ---------- ----------
3 TAG 3 0
1 TAG 1 3
2 TAG 2 1

HTH.

Cheers.

Carlos.

Reply With Quote
  #5  
Old   
The Magnet
 
Posts: n/a

Default Re: COUNT Function - 05-17-2010 , 11:32 AM



On May 17, 10:30*am, Carlos <miotromailcar... (AT) netscape (DOT) net> wrote:
Quote:
On May 17, 5:01*pm, The Magnet <a... (AT) unsu (DOT) com> wrote:



Hi,

I'm tryng to use the Analytical function COUNT(*):

SELECT tag_id, tag_name, tag_count
FROM (SELECT t.tag_id, tag_name,
* * * * * * *COUNT(*) OVER (PARTITION BY t.tag_id) tag_count,
* * * * * * *ROW_NUMBER() OVER (PARTITION BY t.tag_id ORDER BY
t.tag_id) rnum
* * * FROM commentary.article_tags a, commentary.tags t
* * * WHERE t.tag_id = a.tag_id(+))
WHERE rnum = 1;

Problem I am having is that for records in the TAGS table which do not
match in the ARTICLE_TAGS table are still being returned with 1 row.
I'm looking for it to return 0, as there were no matches.

Thought it was the way the join was working, but I do not think so as
I've tried different combos.

Any ideas?

I think i don't get it.

Why not:

CAR... (AT) XE (DOT) bequeath> select * from tags;

* * TAG_ID TAG_NAME
---------- ----------
* * * * *1 TAG 1
* * * * *2 TAG 2
* * * * *3 TAG 3

CAR... (AT) XE (DOT) bequeath> select * from article_tags;

ARTICLE_ID ARTICLE_NAME * * TAG_ID
---------- ------------ ----------
* * * * *1 ARTICLE 11 * * * * * *1
* * * * *1 ARTICLE 11 * * * * * *1
* * * * *1 ARTICLE 12 * * * * * *1
* * * * *2 ARTICLE 21 * * * * * *2

CAR... (AT) XE (DOT) bequeath> SELECT t.tag_id,
* 2 * * * * t.tag_name,
* 3 * * * * COUNT(a.tag_id) tag_count
* 4 * *FROM article_tags a,
* 5 * * * * tags t
* 6 * WHERE t.tag_id = a.tag_id(+)
* 7 * group by t.tag_id, t.tag_name;

* * TAG_ID TAG_NAME * *TAG_COUNT
---------- ---------- ----------
* * * * *3 TAG 3 * * * * * * * 0
* * * * *1 TAG 1 * * * * * * * 3
* * * * *2 TAG 2 * * * * * * * 1

HTH.

Cheers.

Carlos.

Ok, maybe using all those analytical functions was not necessary
here. although I love them.

Thanks.

Reply With Quote
  #6  
Old   
Michel Cadot
 
Posts: n/a

Default Re: COUNT Function - 05-17-2010 , 11:37 AM



"The Magnet" <art (AT) unsu (DOT) com> a écrit dans le message de news: cc1222a8-dd9e-440d-ab5c-c0d16795ec69...oglegroups.com...
On May 17, 10:09 am, "Michel Cadot" <micadot{at}altern{dot}org> wrote:
Quote:
"The Magnet" <a... (AT) unsu (DOT) com> a écrit dans le message de news: d34c6b28-c9b1-4bb8-b973-94aece1cc... (AT) y21g2000vba (DOT) googlegroups.com...
| Hi,
|
| I'm tryng to use the Analytical function COUNT(*):
|
| SELECT tag_id, tag_name, tag_count
| FROM (SELECT t.tag_id, tag_name,
| COUNT(*) OVER (PARTITION BY t.tag_id) tag_count,
| ROW_NUMBER() OVER (PARTITION BY t.tag_id ORDER BY
| t.tag_id) rnum
| FROM commentary.article_tags a, commentary.tags t
| WHERE t.tag_id = a.tag_id(+))
| WHERE rnum = 1;
|
| Problem I am having is that for records in the TAGS table which do not
| match in the ARTICLE_TAGS table are still being returned with 1 row.
| I'm looking for it to return 0, as there were no matches.
|
| Thought it was the way the join was working, but I do not think so as
| I've tried different combos.
|
| Any ideas?

Do not count after the outer join because you will have of course at least
one row, count inside the outer joined table and nvl to 0.

Regards
Michel

Not sure what you mean "after" the outer join. I thought I was
already counting within.

------------------------------------------------


Not:
SQL> select deptno, dname, cnt
2 from ( select d.deptno, d.dname,
3 count(*) over (partition by d.deptno) cnt,
4 row_number () over (partition by d.deptno order by e.ename) rn
5 from dept d, emp e
6 where e.deptno (+) = d.deptno
7 )
8 where rn = 1
9 /
DEPTNO DNAME CNT
---------- -------------- ----------
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
40 OPERATIONS 1

4 rows selected.

But:

SQL> select d.deptno, d.dname, nvl(e.cnt,0) cnt
2 from dept d,
3 (select deptno, count(*) cnt from emp group by deptno) e
4 where e.deptno (+) = d.deptno
5 /
DEPTNO DNAME CNT
---------- -------------- ----------
10 ACCOUNTING 3
20 RESEARCH 5
30 SALES 6
40 OPERATIONS 0

4 rows selected.

General note: using an analytic function and then restrict to 1 row
with "distinct" or "row_number" is a sign that you misuse the function
and you acutally want to use an aggregate (as I did).

Regards
Michel

Reply With Quote
  #7  
Old   
Carlos
 
Posts: n/a

Default Re: COUNT Function - 05-18-2010 , 06:00 AM



Quote:
Ok, maybe using all those analytical functions was not necessary
here. *although I love them.

Thanks.
I love my electric screwdriver, but I wouldn't use it to sink a nail
into the wall.

Cheers.

Carlos.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.