Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

distinct count

$
0
0

I want to count unique PNum from mytable between a certain date range and I did it as follows. Why am I getting two different results? Which code is correct? I think the first code is correct for the above requirement. But, I need to understand what is the second code doing.

Code1:  Count is 58061

Select Count(Distinct pNum) as PCount
From Master_ViewP
where convert(date,PDate) between '01-01-2016' and '01-31-2016'

-- count is 58061

Code2: Count is 58262

SELECT DISTINCT  COUNT (pNum) as PCount
FROM dbo.Master_ViewP
WHERE convert(date,PDate) between '01-01-2016' and '01-31-2016'
--count is 58262



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>