Home » SQL & PL/SQL » SQL & PL/SQL » Urgent - using the count fn for a query
Urgent - using the count fn for a query [message #7275] Mon, 02 June 2003 02:50 Go to next message
Bhavin Patel
Messages: 2
Registered: May 2003
Junior Member
Hi,

Write now ive got 2 tables with data called registration (10 entries) and cancellation (2 entries). What i want to do is to count the no of reg than minus the no of cancellation..to give me a total attendace...so ive written following query for it:

select count(r.reg_no) as Total_Registration,
count(r.reg_no) - count(c.cancellation_id) as Actual_Attendance
from registration r, cancellation c;

wot this give is

TOTAL_REGISTRATION ACTUAL_ATTENDANCE
------------------ -----------------
20 0

and what i want/ it shoud be

TOTAL_REGISTRATION ACTUAL_ATTENDANCE
------------------ -----------------
10 8

need help plz..........
Re: Urgent - using the count fn for a query [message #7279 is a reply to message #7275] Mon, 02 June 2003 04:23 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
You're getting a cartesian product:
10*2 = 20. Each count is returning 20, so what you get is:
select count(r.reg_no) as Total_Registration -- results in 10*2 = 20
     , count(r.reg_no) - count(c.cancellation_id) as actual_Attendance -- results in 20-20 => 0
  from registration r
     , cancellation c;



Try something like this:
SQL> select count(*) from emp;

  COUNT(*)
----------
        14

SQL> select count(*) from dept;

  COUNT(*)
----------
         8

SQL> select e.cnt cnt_emp
  2       , d.cnt cnt_dept
  3       , e.cnt-d.cnt test
  4    from ( select count(*) cnt
  5             from emp
  6         ) e
  7       , ( select count(*) cnt
  8             from dept
  9         ) d
 10  /

   CNT_EMP   CNT_DEPT       TEST
---------- ---------- ----------
        14          8          6

SQL> 
HTH,
MHE
Previous Topic: Did Oracle 7.1 supports Client-Server Architecture
Next Topic: Find thrid lowest value from the table
Goto Forum:
  


Current Time: Fri Apr 26 20:52:30 CDT 2024