Solved

Difference in the number of rows returned

Posted on 2011-09-09
7
272 Views
Last Modified: 2013-12-19
select distinct col1 , col2 from table;

//75048 Records Returned

with temp as (select distinct col1, col2 from table)
select count(*) from temp
;

// 75833 Records Returned

I am not able to figure out why the difference despite the same query.. something elusive hidden somewhere.. requesting all the experts to help me please make me understand.. is this something basic or is this something normal confusion for everyone... Please help me understand
0
Comment
Question by:XxtremePro
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36511809
This was posted in a 10.x Zone.  Please confirm you are running 10g.

I have seen something like this in 11g with results caching.
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 334 total points
ID: 36511966
Just in case,  I'll ask the obvious....

Were 785 rows added to the table between the time you ran the queries?

assuming no...

10g has a few bugs related to WITH clauses,  you may have encountered one. Are you fully patched?

A little more obscure,  check for  equivalencies created with dbms_advanced_rewrite

select * from DBA_REWRITE_EQUIVALENCES
0
 
LVL 15

Expert Comment

by:Franck Pachot
ID: 36527616
Hi,
Can you try:
select count(*) from (select distinct col1 , col2 from table)

Open in new window

in order to see if it comes with a subquery factoring bug ?
Can you show the execution plan with predicates (set autotrace trace explain)
Regards,
Franck.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:XxtremePro
ID: 36531477
Thanks Franck

I am a newbie in the world of programming with database, your statements are way advanced to what I know in sql.

ok now looking at what I would need I would say the need is to pick up just two values from the table as per requirement

select distinct col1, col2 from table1;

col1, col2 both are primary keys. Would the above query ensure that I get only distinct col1 values and col2 values?? If not then what should be done to ensure both the col1 and associate col2 values are distinct ?
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 334 total points
ID: 36531502
yes that will make them distinct

like this...

1,1
1,2
1,3
a,b
c,d
e,f
e,g
e,h


but not necessarily ordered
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
ID: 36531532
>>col1, col2 both are primary keys

Are they the only two columns defined in the Primary Key?

>>. Would the above query ensure that I get only distinct col1 values and col2 values??

Yes.  Just set up a simple test on your development database to see what effects different queries have.



Now that we have answered that, please answer the other questions we have asked.
drop table tab1 purge;
create table tab1(col1 char(1), col2 char(1));

insert into tab1 values('a','b');
insert into tab1 values('a','c');
insert into tab1 values('a','c');
commit;

select distinct col1, col2 from tab1;

Open in new window

0
 

Author Closing Comment

by:XxtremePro
ID: 37135653
Thanks to all
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now