sql server - sql is not returning correct count -
i have sql table hr_registrantcategories this:
registrantid categoryid 1 47 4 276 4 275 4 278 4 274 4 277 16276 276 16276 275 16276 278 16295 276 16295 275 16295 278 16295 274 16295 277 16292 276 16292 275 16292 278 16292 274 16292 277 16293 276 16293 275 16293 278 16293 274 16293 277 16294 276 16294 275 16294 278 16294 274 16294 277 16303 276 16303 275 16303 278 16303 274 16303 277 16302 276 16302 275 16302 278 16302 274 16302 277 16303 276 16303 275 16303 278 16303 274 16303 277 16304 276 16304 275 16304 278 16304 274 16304 277 26 276 26 275 26 278 26 274 26 277 16305 276 16305 275 16305 278 16305 274 16305 277 29 276 29 275 29 278 29 274 29 277 16306 276 16306 275 16306 278 16306 274 16306 277 16306 276 16306 275 16306 278 16306 274 16306 277 16307 276 16307 275 16307 278 16307 274 16307 277 16307 276 16307 275 16307 278 16307 274 16307 277
and table hr_categories this:
categoryid 1 2 3 4 5 6 ... .. .. 700
but returning same count in result when use following query
select count(rc.registrantid) dbo.hr_registrants r inner join dbo.hr_registrantcategories rc on r.registrantid = rc.registrantid r.deleted = 0 , rc.categoryid in (select categoryid dbo.hr_categories) select c.categoryid, ( select count(distinct rc.registrantid) dbo.hr_registrants r inner join dbo.hr_registrantcategories rc on r.registrantid = rc.registrantid r.deleted = 0 , rc.categoryid in (select categoryid dbo.hr_categories c1 c1.deleted = 0) )as registrantscount dbo.hr_categories c
result is:
categoryid registrantscount 1 13 2 13 3 13 4 13 5 13 6 13 7 13 8 13
the inner query not correlated withe outer one, returns same value each line.
select c.categoryid, ( select count(distinct rc.registrantid) dbo.hr_registrants r inner join dbo.hr_registrantcategories rc on r.registrantid = rc.registrantid r.deleted = 0 , rc.categoryid in (select categoryid dbo.hr_categories c1 c1.deleted = 0) , rc.categoryid = c.categoryid -- correlate outer query )as registrantscount dbo.hr_categories c
Comments
Post a Comment