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.
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 ectThe 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 ASNDfrom fcs.dbo.WorkSet as wsleft outer join fcs.dbo.WorkAssignment as waon ws.WorkSetID = wa.WorkSetIDleft outer join fcs.dbo.MeterSessionInput as msion wa.worksetkey = msi.worksetkeyleft outer join fcs.dbo.MeterSessionOutput as msoon msi.MeterSessionInputKey = mso.MeterSessionInputKeywhere 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 andmso.ReadCode <> 0 andws.Cycle <> 75 andws.Cycle = 16 andmso.BestResultIndicator = 2 andws.P4DistrictNumber = 45 andws.WorkSetID = '00016456' andcase 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 = 1group by ws.P4DistrictNumber, ws.Cycle, ws.WorkSetIDorder 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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
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 |
 |
|
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 thatfor example like thisselect P4DistrictNumber, Cycle, WorkSetID,count(CASE WHEN Rn = 1 THEN msi.MeterSessionInputKey END) as ASNDfrom(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 Rnfrom fcs.dbo.WorkSet as wsleft outer join fcs.dbo.WorkAssignment as waon ws.WorkSetID = wa.WorkSetIDleft outer join fcs.dbo.MeterSessionInput as msion wa.worksetkey = msi.worksetkeyleft outer join fcs.dbo.MeterSessionOutput as msoon msi.MeterSessionInputKey = mso.MeterSessionInputKeywhere 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 andmso.ReadCode <> 0 andws.Cycle <> 75 andws.Cycle = 16 andmso.BestResultIndicator = 2 andws.P4DistrictNumber = 45 andws.WorkSetID = '00016456' andcasewhen cast(wa.startdatetime as date) <= cast(ws.scheduledreaddate as date) andwa.workfiltername not in ('DNRs','Type 2s/3s') or ((ws.worksetid %100 < 50 andsubstring(ws.worksetid, len(ws.worksetid) - 3, 1) = 0))then 1End = 1)tgroup by P4DistrictNumber, Cycle, WorkSetIDorder by P4DistrictNumber, Cycle, WorkSetID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|