Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

SQL server count error!!!

Posted on 2002-05-06
Medium Priority
377 Views
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
Question by:hiranmaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 143

Accepted Solution

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.

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

ID: 9280538
hiranmaya:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses
Course of the Month7 days, 8 hours left to enroll