Solved

SQL server count error!!!

Posted on 2002-05-06
3
371 Views
Last Modified: 2006-11-17
I have two tables.

Table1 (master) :
fields:
ID Numeric (unique)
Type Numeric

Table2 (transactions):
fields:
ID Numeric
CrOnDate datetime

Nb: all trasaction are in the month 3 and 4 (march and april)

When I run following queries A, B1, B2. all counts from A should be equal to all counts from B1 + all counts from B2. But I get different counts!!! Do you know why?

A.
SELECT TYPE, COUNT(*) AS ROWSCOUNTS FROM TABLE1  WHERE ID IN (SELECT ID FROM TABLE2 GROUP BY ID) AND TYPE = 2 GROUP BY TYPE

B1.
SELECT TYPE, COUNT(*) AS ROWSCOUNTS FROM TABLE1  WHERE ID IN (SELECT ID FROM TABLE2 WHERE MONTH(CrOnDate) = 3 GROUP BY ID) AND TYPE = 2 GROUP BY TYPE

B2.
SELECT TYPE, COUNT(*) AS ROWSCOUNTS FROM TABLE1  WHERE ID IN (SELECT ID FROM TABLE2 WHERE MONTH(CrOnDate) = 4 GROUP BY ID) AND TYPE = 2 GROUP BY TYPE


Tx,
Hiranmaya Dash
0
Comment
Question by:hiranmaya
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6990948
I guess that some records in the details table have the same ID value, but different CrOnDate month values:

like master (1,2)
and details (1,2002/03/01)
and details (1,2002/04/01)

This gives for A: count=1
This gives for B1: count=1
This gives for B2: count=1

In short, some records of table2 are counted for both B1 and B2.

Solution: Rewrite your queries.
Suggestion:
SELECT TYPE, COUNT(*) AS ROWSCOUNTS
FROM TABLE1 JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
AND TABLE1.TYPE = 2 GROUP BY TYPE

-- B1.
SELECT TYPE, COUNT(*) AS ROWSCOUNTS
FROM TABLE1 JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
WHERE MONTH(TABLE2.CrOnDate) = 3
AND TYPE = 2 GROUP BY TYPE

-- B2.
SELECT TYPE, COUNT(*) AS ROWSCOUNTS
FROM TABLE1 JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
WHERE MONTH(TABLE2.CrOnDate) = 4
AND TYPE = 2 GROUP BY TYPE

CHeers
0
 

Expert Comment

by:CleanupPing
ID: 9280538
hiranmaya:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

773 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