Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Duplicate counts

Author  Topic 

tupacmoche
Starting Member

7 Posts

Posted - 2013-11-26 : 17:15:41
I have sql code that returns the correct number of record when run without an aggregate function like count(myfield) and group by myfield. It always returns 86 row which is correct when Select DISTINCT is used. As, expected when DISTINCT is not used I get double the number if rows or 172. But when I count(myfield) and group by myfield the count is 172 and not 86. The strangest thing about this is that when I am grouping a set of items Group 1
Group 2
Group 3
ect
The other group sum up correctly while others don't. What can explain this? Here is the code.

Select DISTINCT ws.p4Districtnumber, ws.cycle, ws.worksetid, count(msi.MeterSessionInputKey) as ASND

from fcs.dbo.WorkSet as ws

left outer join fcs.dbo.WorkAssignment as wa
on ws.WorkSetID = wa.WorkSetID

left outer join fcs.dbo.MeterSessionInput as msi
on wa.worksetkey = msi.worksetkey

left outer join fcs.dbo.MeterSessionOutput as mso
on msi.MeterSessionInputKey = mso.MeterSessionInputKey

where msi.P4UtilityTypeReadIndicator != 'F' and
wa.RetiredFlag = 0 and
wa.LoadStatusIndicator <> 0 and
wa.LoadStatusIndicator <> 4 and
ws.StartDateTime IS NOT NULL and
wa.RetiredFlag = 0 and
mso.ReadCode <> 0 and
ws.Cycle <> 75 and
ws.Cycle = 16 and
mso.BestResultIndicator = 2 and
ws.P4DistrictNumber = 45 and
ws.WorkSetID = '00016456' and
case
when
cast(wa.startdatetime as date) <= cast(ws.scheduledreaddate as date) and
wa.workfiltername not in ('DNRs','Type 2s/3s') or
((ws.worksetid %100 < 50 and
substring(ws.worksetid, len(ws.worksetid) - 3, 1) = 0))
then 1
End = 1
group by ws.P4DistrictNumber, ws.Cycle, ws.WorkSetID

order by ws.P4DistrictNumber, ws.Cycle, ws.WorkSetID



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-27 : 04:39:49
that means one of field you included in groupby is the one having duplicate values in it. My guess is it might be ws.WorkSetID
field. can you post some sample data of duplicates you're getting and then explain whats the count that you expect for them?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

tupacmoche
Starting Member

7 Posts

Posted - 2013-11-27 : 08:00:46
With respect that is clear that there is a duplicate, I pointed out the same thing. That is why when, I remove the DISTINCT key word, I double the number of records but when I add it the duplicates are gone. The problem is when I use an aggregate function COUNT(MYFIELD)with a GROUP BY the number of records are counted twice while other are not. Strange
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-27 : 10:38:54
quote:
Originally posted by tupacmoche

With respect that is clear that there is a duplicate, I pointed out the same thing. That is why when, I remove the DISTINCT key word, I double the number of records but when I add it the duplicates are gone. The problem is when I use an aggregate function COUNT(MYFIELD)with a GROUP BY the number of records are counted twice while other are not. Strange


that's the expected behavior. if you want to count correctly you need to take one out of the group and then count on that
for example like this


select P4DistrictNumber, Cycle, WorkSetID,
count(CASE WHEN Rn = 1 THEN msi.MeterSessionInputKey END) as ASND
from
(
Select ws.p4Districtnumber, ws.cycle, ws.worksetid,
msi.MeterSessionInputKey,
ROW_NUMBER() OVER (PARTITION BY ws.p4Districtnumber, ws.cycle, ws.worksetid ORDER BY (SELECT 1)) AS Rn
from fcs.dbo.WorkSet as ws

left outer join fcs.dbo.WorkAssignment as wa
on ws.WorkSetID = wa.WorkSetID

left outer join fcs.dbo.MeterSessionInput as msi
on wa.worksetkey = msi.worksetkey

left outer join fcs.dbo.MeterSessionOutput as mso
on msi.MeterSessionInputKey = mso.MeterSessionInputKey

where msi.P4UtilityTypeReadIndicator != 'F' and
wa.RetiredFlag = 0 and
wa.LoadStatusIndicator <> 0 and
wa.LoadStatusIndicator <> 4 and
ws.StartDateTime IS NOT NULL and
wa.RetiredFlag = 0 and
mso.ReadCode <> 0 and
ws.Cycle <> 75 and
ws.Cycle = 16 and
mso.BestResultIndicator = 2 and
ws.P4DistrictNumber = 45 and
ws.WorkSetID = '00016456' and
case
when
cast(wa.startdatetime as date) <= cast(ws.scheduledreaddate as date) and
wa.workfiltername not in ('DNRs','Type 2s/3s') or
((ws.worksetid %100 < 50 and
substring(ws.worksetid, len(ws.worksetid) - 3, 1) = 0))
then 1
End = 1
)t
group by P4DistrictNumber, Cycle, WorkSetID
order by P4DistrictNumber, Cycle, WorkSetID



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -