473,657 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2634
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****@sommarsk og.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
3870
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 records (and also show the above 3 fields) So suppose the table looks like this:
3
14119
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 (and store it in field 2). > > I would like to run a query that returns all the data in the table plus the > record number, in a similar sense to the getuser() command to get the User's > Identity, I would like a GetRecord() command.
1
2280
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 that have null and non null fields (see below). CustNo Funding ClaimAmt Prod 123 R 100 PPO 123 R 200 Null Value
10
6721
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 contains only tables. I have linked the tables for the front end to the back end database. I am trying to set the recordsource of a form to a query established by the user to narrow the scope but I don't want to display the form if there are no...
15
1778
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 (based on the primary key of the detail record). I've been trying with "TOP 3", but can't get anywhere. Using Access 2000. Something like: SELECT t1.*, TOP 3 t2.*
2
4442
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 statistical purposes. I've been using Here's the situation: I have two main tables:
1
1496
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 would be much appreiciated :-) <?php include "flowermatchdbcnx.php"; $r = $_POST; //users choice... e.g. birthday, anniversary etc.
2
9000
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 patients in a table Const strDBPath = "C:\Patients.mdb" ' external database Set dbPatients = OpenDatabase(strDBPath, False, True, ";PWD=abc")
8
2776
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. The problem is that when I use that criteria for all four fields I am not getting the expected results. I am trying to find out from this query is the date in date field one is older than 180, same thing for the other three date fields. For...
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.