简体   繁体   中英

SQL running a count(*) and count(*) group by returns differnet number of rows

This is strange, lets say I have a table called dbo.internetquotes in SQL server .

I want to get the total number of quotes in a month and break it down by a column called quotetype.

I run the query:

select count(*) from dbo.internetquotes
where quotedate between '2014/03/01' and '2014/04/01';

and it returns 20k.

Now I run the query

select count(*), quotetype from dbo.internetquotes
where quotedate between '2014/03/01' and '2014/04/01'
group by quotetype;

And it returns the count per quotetype. However, when i sum up the counts from the second query the number does not equal 20k (slightly less). My thought was perhaps there are nulls in the column quotetypes but running a select * where quotetype = null returns 0 rows.

What is the reason for the discrepancies?

When anything does not match with aggregation, just think about three values - TRUE, FALSE, UNKNOWN.

You are probably dropping out the NULLS (UNKNOWN) when aggregating.

Also, please use > and < when dealing with dates.

Bad habits to kick : mis-handling date / range queries

-- 1 - Skip nulls
select 
    count(*) as Total
from 
    dbo.internetquotes
where 
    quotedate >= '20140301' and 
    quotedate < '20140401' and
    quotetype is not null

-- 2 - Skip nulls
select 
    quotetype, count(*) as total
from 
    dbo.internetquotes
where 
    quotedate >= '20140301' and 
    quotedate < '20140401' and
    quotetype is not null
group by 
    quotetype;

These two queries should have the same counts. Good luck.

Just use Adventure Works and try these queries

-- 31465
select count(*) from [Sales].[SalesOrderHeader]

-- Group by person 
select SalesPersonID, count(*) as total
into #temp
from [Sales].[SalesOrderHeader] 
group  by SalesPersonID

-- 18 rows for a total count of 31465
select sum(total) as grand from #temp

The nulls will be group by and show up as a row. I never discounted that. But the date issue could drop data. I champion that!

I'm thinking about it for a moment, I think you have duplicated rows or duplicated quotedate.

Can you run something like :

select count(distinct quotedate) from dbo.internetquotes
where quotedate between '2014/03/01' and '2014/04/01';

To compare with sum(counts) of 2nd query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM