473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inflated number of records returned when adding bit field criteria

When querying a bit field, I am encountering a problem with MS SQL
Server returning a larger number of records for a table than the
actual number of records that exist within that table.

For example, my customer table has 1 million unique records, so the
results of the following query are as such:

select count(customer_nbr) from customer = 1,000,000

There is bit field in the customer table that denotes whether a
customer has placed an order with us called. That flag is called
order_flag

If I run the following query:

select count(customer_nbr) from customer where order_flag = 1

The result is 3,000,000 records.

There is no logical way that this is possible, as my table only
contains 1,000,000 unique records and the number of customers with an
order should be a subset of this.

If a run the above query with a distinct before customer number, I get
the results I want:

select count(distinct customer_nbr) from customer where order_flag = 1

600,000 records.

So while I can get to the answer I want, I have no idea why I am
returning incorrect values if I don't select distinct.

Can anyone help? I checked microsoft support and message boards but
haven't seen anything.

I should note that the bit field is indexed.I am not sure if that is
the problem or not.
Jul 20 '05 #1
1 2623
Bryan Zash (bz***@hotmail.com) writes:
For example, my customer table has 1 million unique records, so the
results of the following query are as such:

select count(customer_nbr) from customer = 1,000,000

There is bit field in the customer table that denotes whether a
customer has placed an order with us called. That flag is called
order_flag

If I run the following query:

select count(customer_nbr) from customer where order_flag = 1

The result is 3,000,000 records.

There is no logical way that this is possible, as my table only
contains 1,000,000 unique records and the number of customers with an
order should be a subset of this.

If a run the above query with a distinct before customer number, I get
the results I want:

select count(distinct customer_nbr) from customer where order_flag = 1

600,000 records.

So while I can get to the answer I want, I have no idea why I am
returning incorrect values if I don't select distinct.


Something does not seem quite right here, but let's first observe a few
things:
CREATE TABLE #h (a int NULL, b bit NOT NULL)
go
INSERT #h (a, b) VALUES (12, 1)
INSERT #h (a, b) VALUES (12, 1)
INSERT #h (a, b) VALUES (112, 1)
INSERT #h (a, b) VALUES (NULL, 1)
INSERT #h (a, b) VALUES (15, 1)

INSERT #h (a, b) VALUES (12, 0)
INSERT #h (a, b) VALUES (12, 0)
INSERT #h (a, b) VALUES (112, 0)
INSERT #h (a, b) VALUES (NULL, 0)
INSERT #h (a, b) VALUES (15, 0)
go
CREATE INDEX bitix ON #h(b)
go
SELECT COUNT(*) FROM #h -- 10
SELECT COUNT(a) FROM #h -- 8
SELECT COUNT(DISTINCT a) FROM #h -- 3
SELECT COUNT(*) FROM #h WHERE b = 1 -- 5
SELECT COUNT(a) FROM #h WHERE b = 1 -- 4
SELECT COUNT(DISTINCT a) FROM #h WHERE b = 1 -- 3
go
DROP TABLE #h

COUNT(*) counts all rows. COUNT(a) only counts rows where A has a non-NULL
value. COUNT(DISTINCT a) counts the number of distinct values. I'm a
little uncertain from your post whether you are aware of this, which
is why I brought it up.

But of course, SELECT COUNT(a) should give a higher number if you
apply a filter, so it does smell like a bug here. Unfortunately, though,
there is not much I can do without a reproducible case. And that
may be difficult to achieve. I have a vague recollection of that I've
might have seen something like this before. First thing is to check
SELECT @@version and see what it says. If says something lower than
8.00.760 you don't have SP3, and you should apply that service pack.

If you already have SP3, and need to pursue the issue more in detail,
your best bet may be to open a case with Microsoft, since it does not
seem likely that we will be able to resolve over the newsgroups. Note
that if it is indeed a bug in SQL Server, any expenses you have for
the case should be refunded.

Of course, if you are able to reproduce the problem with a much smaller
data set, so that you could post a script, then I'm interested.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Shaiguy | last post by:
I have a table containing the following fields: ProjectUpdateID (PrimaryKey) ProjectID UpdateDate I would like to create a Query in Ms Access 2000 which will return me the most recent 2...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
1
by: RiesbeckP | last post by:
Hi All, I have a DB where there are customer numbers and a few other fields. I want to be able to pull all of the null records for a particular field as well as all the other customer numbers...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
15
by: Hexman | last post by:
Hello All, How do I limit the number of detail records selected in a Master-Detail set using SQL? I want to select all master records for a date, but only the first 3 records for the details...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
1
by: jasone | last post by:
Hi all, ive got a search working, as shown below... on the page i want to show how many records are returned, im sure this is pretty simple but cant really work it out. any help or advice...
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following code to get the recordset of an external database. Dim dbPatients As Database Dim rsCountPatients As Recordset ' to count number of...
8
by: Dr Al | last post by:
I have a table with four date fields, some of which may not be filled in based on our data entry needs. I have a criteria set as <date()-180 which is supposed to pull dates older than 180 days ago....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.