Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL server count error!!!

Posted on 2002-05-06
3
Medium Priority
?
379 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

971 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