select count(*) and select * return incoherent result

select count(*) and select * return incoherent result

Post by Eric Malotau » Sat, 17 Jun 2000 04:00:00



My problem can be reproduced by the following SQL statements:

CREATE TABLE  TEST
(
     FLT FLOAT
)

INSERT INTO TEST (FLT) VALUES (0.300000011920929)

SELECT * FROM TEST WHERE FLT = 0.300000011920929

SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

DROP TABLE  TEST

which returns:

(1 row(s) affected)

FLT                                                  
-----------------------------------------------------
0.30000001192092896

(1 row(s) affected)

-----------
0

(1 row(s) affected)

The select * returns a row but the select count(*) returns 0 instead of 1?!

Can somebody tell me if this is really a bug and if there is a workaround to it (besides reducing the number of digits to avoid discretization problem) ?

Thanks,

PS: I have made this test on MSDE 1.0 with service pack 2.

 
 
 

select count(*) and select * return incoherent result

Post by Tibor Karasz » Sat, 17 Jun 2000 04:00:00


Eric,

The float (and real) datatypes are, per definition not precise datatypes. This means
that they span a wide range , but SQL Server can not represent all values within that
range. You can easily see from the SELECT * that the values stored is _not_ the same
as you entered.

Use some other datatype (DECIMAL, for instance) if you need exact numeric
representation.

--
Tibor Karaszi, SQL Server MVP
Please reply to the newsgroup only, not by email.


> My problem can be reproduced by the following SQL statements:

> CREATE TABLE  TEST

>      FLT FLOAT
> )

> INSERT INTO TEST (FLT) VALUES (0.300000011920929)

> SELECT * FROM TEST WHERE FLT = 0.300000011920929

> SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

> DROP TABLE  TEST

> which returns:

> (1 row(s) affected)

> FLT
> -----------------------------------------------------
> 0.30000001192092896

> (1 row(s) affected)

> -----------
> 0

> (1 row(s) affected)

> The select * returns a row but the select count(*) returns 0 instead of 1?!

> Can somebody tell me if this is really a bug and if there is a workaround to it

(besides reducing the number of digits to avoid discretization problem) ?

- Show quoted text -

Quote:

> Thanks,

> PS: I have made this test on MSDE 1.0 with service pack 2.


 
 
 

select count(*) and select * return incoherent result

Post by Eric Malotau » Sat, 17 Jun 2000 04:00:00


The discretization is not really a problem for me but well the inconsistancy
between the result of the 2 selects.
Why in one case the select the discretization seems to be redone (OK for me)
and not in the other case?

The data I have to store is a C "double" which matches the range of a SQL
"float" (ranging from - 1.79E + 308 through 1.79E + 308) while "decimal" and
"numeric" types range from  - 10^38 -1 through 10^38 - 1.


> Eric,

> The float (and real) datatypes are, per definition not precise datatypes.
This means
> that they span a wide range , but SQL Server can not represent all values
within that
> range. You can easily see from the SELECT * that the values stored is
_not_ the same
> as you entered.

> Use some other datatype (DECIMAL, for instance) if you need exact numeric
> representation.

> --
> Tibor Karaszi, SQL Server MVP
> Please reply to the newsgroup only, not by email.




- Show quoted text -

Quote:> > My problem can be reproduced by the following SQL statements:

> > CREATE TABLE  TEST

> >      FLT FLOAT
> > )

> > INSERT INTO TEST (FLT) VALUES (0.300000011920929)

> > SELECT * FROM TEST WHERE FLT = 0.300000011920929

> > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

> > DROP TABLE  TEST

> > which returns:

> > (1 row(s) affected)

> > FLT
> > -----------------------------------------------------
> > 0.30000001192092896

> > (1 row(s) affected)

> > -----------
> > 0

> > (1 row(s) affected)

> > The select * returns a row but the select count(*) returns 0 instead of
1?!

> > Can somebody tell me if this is really a bug and if there is a
workaround to it
> (besides reducing the number of digits to avoid discretization problem) ?

> > Thanks,

> > PS: I have made this test on MSDE 1.0 with service pack 2.

 
 
 

select count(*) and select * return incoherent result

Post by Tibor Karasz » Sat, 17 Jun 2000 04:00:00


I'm sorry, I was a bit quick. I understand (now) what you are saying.

I don't get the same result as you:

I execute below from QA:
SET NOCOUNT ON
CREATE TABLE TEST(FLT FLOAT)
GO
INSERT INTO TEST (FLT) VALUES (0.300000011920929)
SELECT * FROM TEST WHERE FLT = 0.300000011920929
SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929
DROP TABLE  TEST

And get below result:
FLT
---------------------------
0.30000001192092901
-----------
1


Microsoft SQL Server  7.00 - 7.00.842 (Intel X86)
 Mar  2 2000 06:49:37
 Copyright (c) 1988-1998 Microsoft Corporation
 Desktop Edition on Windows NT 5.0 (Build 2195: )

--
Tibor Karaszi, SQL Server MVP
Please reply to the newsgroup only, not by email.


> The discretization is not really a problem for me but well the inconsistancy
> between the result of the 2 selects.
> Why in one case the select the discretization seems to be redone (OK for me)
> and not in the other case?

> The data I have to store is a C "double" which matches the range of a SQL
> "float" (ranging from - 1.79E + 308 through 1.79E + 308) while "decimal" and
> "numeric" types range from  - 10^38 -1 through 10^38 - 1.



> > Eric,

> > The float (and real) datatypes are, per definition not precise datatypes.
> This means
> > that they span a wide range , but SQL Server can not represent all values
> within that
> > range. You can easily see from the SELECT * that the values stored is
> _not_ the same
> > as you entered.

> > Use some other datatype (DECIMAL, for instance) if you need exact numeric
> > representation.

> > --
> > Tibor Karaszi, SQL Server MVP
> > Please reply to the newsgroup only, not by email.



> > > My problem can be reproduced by the following SQL statements:

> > > CREATE TABLE  TEST

> > >      FLT FLOAT
> > > )

> > > INSERT INTO TEST (FLT) VALUES (0.300000011920929)

> > > SELECT * FROM TEST WHERE FLT = 0.300000011920929

> > > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

> > > DROP TABLE  TEST

> > > which returns:

> > > (1 row(s) affected)

> > > FLT
> > > -----------------------------------------------------
> > > 0.30000001192092896

> > > (1 row(s) affected)

> > > -----------
> > > 0

> > > (1 row(s) affected)

> > > The select * returns a row but the select count(*) returns 0 instead of
> 1?!

> > > Can somebody tell me if this is really a bug and if there is a
> workaround to it
> > (besides reducing the number of digits to avoid discretization problem) ?

> > > Thanks,

> > > PS: I have made this test on MSDE 1.0 with service pack 2.

 
 
 

select count(*) and select * return incoherent result

Post by Eric Malotau » Sat, 17 Jun 2000 04:00:00


No problem with the "bit quick", I am always clear and my English is maybe
approximative.

I have retried your statements on my PC and one of my collegues, and it
failed in both cases.

The tested versions of SQL are

Microsoft SQL Server  7.00 - 7.00.699 (Intel X86)
 May 21 1999 14:08:18
 Copyright (c) 1988-1998 Microsoft Corporation
 MSDE on Windows NT 5.0 (Build 2195: )

and

Microsoft SQL Server  7.00 - 7.00.699 (Intel X86)
 May 21 1999 14:08:18
 Copyright (c) 1988-1998 Microsoft Corporation
 MSDE on Windows NT 4.0 (Build 1381: Service Pack 5)

I have no other version of MS SQL where to test it further for the moment.

As the PC of my collegue runs the same OS as yours, should we conclude that
wer are we in front of a bug specific to MSDE?


> I'm sorry, I was a bit quick. I understand (now) what you are saying.

> I don't get the same result as you:

> I execute below from QA:
> SET NOCOUNT ON
> CREATE TABLE TEST(FLT FLOAT)
> GO
> INSERT INTO TEST (FLT) VALUES (0.300000011920929)
> SELECT * FROM TEST WHERE FLT = 0.300000011920929
> SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929
> DROP TABLE  TEST

> And get below result:
> FLT
> ---------------------------
> 0.30000001192092901
> -----------
> 1


> Microsoft SQL Server  7.00 - 7.00.842 (Intel X86)
>  Mar  2 2000 06:49:37
>  Copyright (c) 1988-1998 Microsoft Corporation
>  Desktop Edition on Windows NT 5.0 (Build 2195: )

> --
> Tibor Karaszi, SQL Server MVP
> Please reply to the newsgroup only, not by email.




- Show quoted text -

> > The discretization is not really a problem for me but well the
inconsistancy
> > between the result of the 2 selects.
> > Why in one case the select the discretization seems to be redone (OK for
me)
> > and not in the other case?

> > The data I have to store is a C "double" which matches the range of a
SQL
> > "float" (ranging from - 1.79E + 308 through 1.79E + 308) while "decimal"
and
> > "numeric" types range from  - 10^38 -1 through 10^38 - 1.



> > > Eric,

> > > The float (and real) datatypes are, per definition not precise
datatypes.
> > This means
> > > that they span a wide range , but SQL Server can not represent all
values
> > within that
> > > range. You can easily see from the SELECT * that the values stored is
> > _not_ the same
> > > as you entered.

> > > Use some other datatype (DECIMAL, for instance) if you need exact
numeric
> > > representation.

> > > --
> > > Tibor Karaszi, SQL Server MVP
> > > Please reply to the newsgroup only, not by email.



> > > > My problem can be reproduced by the following SQL statements:

> > > > CREATE TABLE  TEST

> > > >      FLT FLOAT
> > > > )

> > > > INSERT INTO TEST (FLT) VALUES (0.300000011920929)

> > > > SELECT * FROM TEST WHERE FLT = 0.300000011920929

> > > > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

> > > > DROP TABLE  TEST

> > > > which returns:

> > > > (1 row(s) affected)

> > > > FLT
> > > > -----------------------------------------------------
> > > > 0.30000001192092896

> > > > (1 row(s) affected)

> > > > -----------
> > > > 0

> > > > (1 row(s) affected)

> > > > The select * returns a row but the select count(*) returns 0 instead
of
> > 1?!

> > > > Can somebody tell me if this is really a bug and if there is a
> > workaround to it
> > > (besides reducing the number of digits to avoid discretization
problem) ?

> > > > Thanks,

> > > > PS: I have made this test on MSDE 1.0 with service pack 2.

 
 
 

select count(*) and select * return incoherent result

Post by Tibor Karasz » Sat, 17 Jun 2000 04:00:00


Aha!
Seems to be a service pack issue.
I was running it on sp2, and I have now tried it on an sp1 machine and get the same
result as you do.

So, consider sp2 (or possible go back to RTM?).

I can't say if it is only a coincidence that is works on sp2 (i.e. there is something
inherent which can make this "fail"), but it seems to be a clear case to me (bug)...
--
Tibor Karaszi, SQL Server MVP
Please reply to the newsgroup only, not by email.


> No problem with the "bit quick", I am always clear and my English is maybe
> approximative.

> I have retried your statements on my PC and one of my collegues, and it
> failed in both cases.

> The tested versions of SQL are

> Microsoft SQL Server  7.00 - 7.00.699 (Intel X86)
>  May 21 1999 14:08:18
>  Copyright (c) 1988-1998 Microsoft Corporation
>  MSDE on Windows NT 5.0 (Build 2195: )

> and

> Microsoft SQL Server  7.00 - 7.00.699 (Intel X86)
>  May 21 1999 14:08:18
>  Copyright (c) 1988-1998 Microsoft Corporation
>  MSDE on Windows NT 4.0 (Build 1381: Service Pack 5)

> I have no other version of MS SQL where to test it further for the moment.

> As the PC of my collegue runs the same OS as yours, should we conclude that
> wer are we in front of a bug specific to MSDE?



> > I'm sorry, I was a bit quick. I understand (now) what you are saying.

> > I don't get the same result as you:

> > I execute below from QA:
> > SET NOCOUNT ON
> > CREATE TABLE TEST(FLT FLOAT)
> > GO
> > INSERT INTO TEST (FLT) VALUES (0.300000011920929)
> > SELECT * FROM TEST WHERE FLT = 0.300000011920929
> > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929
> > DROP TABLE  TEST

> > And get below result:
> > FLT
> > ---------------------------
> > 0.30000001192092901
> > -----------
> > 1


> > Microsoft SQL Server  7.00 - 7.00.842 (Intel X86)
> >  Mar  2 2000 06:49:37
> >  Copyright (c) 1988-1998 Microsoft Corporation
> >  Desktop Edition on Windows NT 5.0 (Build 2195: )

> > --
> > Tibor Karaszi, SQL Server MVP
> > Please reply to the newsgroup only, not by email.



> > > The discretization is not really a problem for me but well the
> inconsistancy
> > > between the result of the 2 selects.
> > > Why in one case the select the discretization seems to be redone (OK for
> me)
> > > and not in the other case?

> > > The data I have to store is a C "double" which matches the range of a
> SQL
> > > "float" (ranging from - 1.79E + 308 through 1.79E + 308) while "decimal"
> and
> > > "numeric" types range from  - 10^38 -1 through 10^38 - 1.



> > > > Eric,

> > > > The float (and real) datatypes are, per definition not precise
> datatypes.
> > > This means
> > > > that they span a wide range , but SQL Server can not represent all
> values
> > > within that
> > > > range. You can easily see from the SELECT * that the values stored is
> > > _not_ the same
> > > > as you entered.

> > > > Use some other datatype (DECIMAL, for instance) if you need exact
> numeric
> > > > representation.

> > > > --
> > > > Tibor Karaszi, SQL Server MVP
> > > > Please reply to the newsgroup only, not by email.



> > > > > My problem can be reproduced by the following SQL statements:

> > > > > CREATE TABLE  TEST

> > > > >      FLT FLOAT
> > > > > )

> > > > > INSERT INTO TEST (FLT) VALUES (0.300000011920929)

> > > > > SELECT * FROM TEST WHERE FLT = 0.300000011920929

> > > > > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

> > > > > DROP TABLE  TEST

> > > > > which returns:

> > > > > (1 row(s) affected)

> > > > > FLT
> > > > > -----------------------------------------------------
> > > > > 0.30000001192092896

> > > > > (1 row(s) affected)

> > > > > -----------
> > > > > 0

> > > > > (1 row(s) affected)

> > > > > The select * returns a row but the select count(*) returns 0 instead
> of
> > > 1?!

> > > > > Can somebody tell me if this is really a bug and if there is a
> > > workaround to it
> > > > (besides reducing the number of digits to avoid discretization
> problem) ?

> > > > > Thanks,

> > > > > PS: I have made this test on MSDE 1.0 with service pack 2.

 
 
 

select count(*) and select * return incoherent result

Post by Eric Malotau » Sat, 17 Jun 2000 04:00:00


Your are totally right: after having installed the SP2 (which I wrongly
thought I had already done), I got the same result as you.

Thanks a lot for your prompt support and sorry to have not checked SP2 was
well installed before.

Eric Malotaux

ACTLEM sa
Av de l'Artisanat 10A
B-1420 Braine L'Alleud
Belgium
Tel       +32 2 389.11.34
Fax      +32 2 384.47.16


> Aha!
> Seems to be a service pack issue.
> I was running it on sp2, and I have now tried it on an sp1 machine and get
the same
> result as you do.

> So, consider sp2 (or possible go back to RTM?).

> I can't say if it is only a coincidence that is works on sp2 (i.e. there
is something
> inherent which can make this "fail"), but it seems to be a clear case to
me (bug)...
> --
> Tibor Karaszi, SQL Server MVP
> Please reply to the newsgroup only, not by email.




- Show quoted text -

> > No problem with the "bit quick", I am always clear and my English is
maybe
> > approximative.

> > I have retried your statements on my PC and one of my collegues, and it
> > failed in both cases.

> > The tested versions of SQL are

> > Microsoft SQL Server  7.00 - 7.00.699 (Intel X86)
> >  May 21 1999 14:08:18
> >  Copyright (c) 1988-1998 Microsoft Corporation
> >  MSDE on Windows NT 5.0 (Build 2195: )

> > and

> > Microsoft SQL Server  7.00 - 7.00.699 (Intel X86)
> >  May 21 1999 14:08:18
> >  Copyright (c) 1988-1998 Microsoft Corporation
> >  MSDE on Windows NT 4.0 (Build 1381: Service Pack 5)

> > I have no other version of MS SQL where to test it further for the
moment.

> > As the PC of my collegue runs the same OS as yours, should we conclude
that
> > wer are we in front of a bug specific to MSDE?



> > > I'm sorry, I was a bit quick. I understand (now) what you are saying.

> > > I don't get the same result as you:

> > > I execute below from QA:
> > > SET NOCOUNT ON
> > > CREATE TABLE TEST(FLT FLOAT)
> > > GO
> > > INSERT INTO TEST (FLT) VALUES (0.300000011920929)
> > > SELECT * FROM TEST WHERE FLT = 0.300000011920929
> > > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929
> > > DROP TABLE  TEST

> > > And get below result:
> > > FLT
> > > ---------------------------
> > > 0.30000001192092901
> > > -----------
> > > 1


> > > Microsoft SQL Server  7.00 - 7.00.842 (Intel X86)
> > >  Mar  2 2000 06:49:37
> > >  Copyright (c) 1988-1998 Microsoft Corporation
> > >  Desktop Edition on Windows NT 5.0 (Build 2195: )

> > > --
> > > Tibor Karaszi, SQL Server MVP
> > > Please reply to the newsgroup only, not by email.



> > > > The discretization is not really a problem for me but well the
> > inconsistancy
> > > > between the result of the 2 selects.
> > > > Why in one case the select the discretization seems to be redone (OK
for
> > me)
> > > > and not in the other case?

> > > > The data I have to store is a C "double" which matches the range of
a
> > SQL
> > > > "float" (ranging from - 1.79E + 308 through 1.79E + 308) while
"decimal"
> > and
> > > > "numeric" types range from  - 10^38 -1 through 10^38 - 1.



> > > > > Eric,

> > > > > The float (and real) datatypes are, per definition not precise
> > datatypes.
> > > > This means
> > > > > that they span a wide range , but SQL Server can not represent all
> > values
> > > > within that
> > > > > range. You can easily see from the SELECT * that the values stored
is
> > > > _not_ the same
> > > > > as you entered.

> > > > > Use some other datatype (DECIMAL, for instance) if you need exact
> > numeric
> > > > > representation.

> > > > > --
> > > > > Tibor Karaszi, SQL Server MVP
> > > > > Please reply to the newsgroup only, not by email.



> > > > > > My problem can be reproduced by the following SQL statements:

> > > > > > CREATE TABLE  TEST

> > > > > >      FLT FLOAT
> > > > > > )

> > > > > > INSERT INTO TEST (FLT) VALUES (0.300000011920929)

> > > > > > SELECT * FROM TEST WHERE FLT = 0.300000011920929

> > > > > > SELECT COUNT(*) FROM TEST WHERE FLT = 0.300000011920929

> > > > > > DROP TABLE  TEST

> > > > > > which returns:

> > > > > > (1 row(s) affected)

> > > > > > FLT
> > > > > > -----------------------------------------------------
> > > > > > 0.30000001192092896

> > > > > > (1 row(s) affected)

> > > > > > -----------
> > > > > > 0

> > > > > > (1 row(s) affected)

> > > > > > The select * returns a row but the select count(*) returns 0
instead
> > of
> > > > 1?!

> > > > > > Can somebody tell me if this is really a bug and if there is a
> > > > workaround to it
> > > > > (besides reducing the number of digits to avoid discretization
> > problem) ?

> > > > > > Thanks,

> > > > > > PS: I have made this test on MSDE 1.0 with service pack 2.

 
 
 

1. select count(1) returns emtpy result set

Hi,

I encountered this strange phenomenon with the count aggregate function in a
query as the one below:

select count(1) from mytable where myfield1=1 group by myfield2

This works when

select * from mytable where myfield1=1

produces a non-empty result set, however, when no records meet the condition
myfield1=1, thus producing an empty result set, counting the records using
the first query above does not produce a single row with 0 in it, but
instead produces an empty result set.

This seems odd to me.  I found out that doing a

select count(1) from (select * from mytable where myfield1=1) subq

does produce a single row with a 0 in it, but when I add the grouping
clause, it fails.
Why does the first query not work ?

Actually, the complete query I'm executing is something like

select max(c) from (select count(1) as c from mytable where myfield1=1 group
by myfield2) subq

To get a correct result, I modified this to

select isnull((select max(c) from (select count(1) as c from mytable where
myfield1=1 group by myfield2) subq),0)

which seems a whole lot of work to me, so I'm wondering if there's a shorter
way, producing a correct result.

Thanks in advance,

Joris Spriet

PS :Below a simple script to build the table used in the queries above

create table mytable (myfield1 int, myfield2 int)
go
insert into mytable (myfield1, myfield2) values (0,1)
go
insert into mytable (myfield1, myfield2) values (0,2)
go
insert into mytable (myfield1, myfield2) values (0,2)
go
insert into mytable (myfield1, myfield2) values (2,1)
go
insert into mytable (myfield1, myfield2) values (2,3)
go

2. Looking for a component...

3. SELECT COUNT(*) or SELECT COUNT(id)

4. BUG with String addition in SQL SErver 7.0

5. select count(1) vs select count(*)

6. Need RealTime reporting tool(s) with graphs/charts

7. Re select count(1) vs select count(*)

8. Optimising Memory/Overhead of EXE's

9. select count(x) returning 1 instead of total count

10. How can i make select that will return only 20 first results instead of all results

11. Add a record count column to a SELECT result

12. SELECT COUNT(*) resulting in table scan

13. select count(*) from (select ...)