Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2015
    Posts
    1

    Unanswered: count(*) on where exists

    Hello all,

    Two queries very similar are giving different row count. Can you please advice. TIA.

    select count(*) from FBL.CELL
    where exists
    ( select * from FBL.cell where cmpgn_cd = 'RS15001' and cell_st_dt = '2015-07-14'); Total 426308
    But
    select count(*) from FBL.cell where cmpgn_cd = 'RS15001' and cell_st_dt = '2015-07-14' Total = 9

    I was expecting both the queries to return the same result. The first query is pulling the entire table. My thinking is that count(*) does nto work on whe exists.

    Thanks,
    Shalaka

  2. #2
    Join Date
    Apr 2006
    Location
    Belgium
    Posts
    2,514
    Provided Answers: 11
    strange, but I would never use first type of query
    Best Regards, Guy Przytula
    Database Software Consultant
    Good DBAs are not formed in a week or a month. They are created little by little, day by day. Protracted and patient effort is needed to develop good DBAs.
    Spoon feeding : To treat (another) in a way that discourages independent thought or action, as by overindulgence.
    DB2 UDB LUW Certified V7-V8-V9-V9.7-V10.1-V10.5 DB Admin - Advanced DBA -Dprop..
    Information Server Datastage Certified
    http://www.infocura.be

  3. #3
    Join Date
    Jul 2013
    Location
    Moscow, Russia
    Posts
    666
    Provided Answers: 55
    Hello,

    These queries mustn't return the same result (unless all rows in the table have cmpgn_cd = 'RS15001' and cell_st_dt = '2015-07-14') since they are absolutely different.
    The outer select and the subselect of the following query are not correlated.
    Code:
    select count(*) from FBL.CELL 
    where exists 
    ( select * from FBL.cell where cmpgn_cd = 'RS15001' and cell_st_dt = '2015-07-14');
    and it simply returns the number of rows in the FBL.CELL table since the subselect always returns 9 rows for every row of the outer select.
    Last edited by mark.b; 07-15-15 at 11:52.
    Regards,
    Mark.

  4. #4
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Think of it this way. EXISTS is a boolean. It will either return True or False. True if the query returns any rows, false otherwise. The query always returns 9 rows so it will always return True. So you are counting each row where EXISTS is returning true, but this sub-query always returns True so every row will be counted.

    Andy

  5. #5
    Join Date
    Sep 2004
    Posts
    15
    select count(*) from FBL.CELL
    where exists
    ( select * from FBL.cell where cmpgn_cd = 'RS15001' and cell_st_dt = '2015-07-14'); Total 426308
    But
    select count(*) from FBL.cell where cmpgn_cd = 'RS15001' and cell_st_dt = '2015-07-14' Total = 9

    Breaking things up!

    Your EXISTS sql has no relation to your SELECT COUNT(*) portion! Since your EXISTS block is able to fetch at-least 1 row (in your case its 9 rows) the EXISTS is satisfied and your count will continue to "count" all the rows

    You need to provide a reference of your main query in the EXISTS block to bind them together, else its as good as just running the COUNT statement on its own.

    Here:
    select count(*) from FBL.CELL AS X
    where exists
    ( select * from FBL.cell AS Y where Y.cmpgn_cd = X.cmpgn_cd and Y.cell_st_dt = X.cell_st_dt
    and Y.cmpgn_cd = 'RS15001' and Y.cell_st_dt = '2015-07-14');

    Now that you have a binding of the two queries done, the count should return 9

    The benefit of using EXISTS and NOT EXISTS is to do a quick check if your record exists in another subset of data, this is done using a Unique Key or any Indexed key in your EXISTS / NOT EXISTS sub query to fetch the data faster. That is why its preferred against using NOT IN and IN clause.

    Hope that helped

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •