tsql - Why using COUNT with SELF JOIN gives different result value -


can explain me why if use self join , count gives me different result using count command? same table controlno column. value in column not unique. query gives me total counts 15586.

    select  (select  count(controlno)                  tblquotes q1                  q1.controlno = a.controlno             ) quotedtotal        tblquotes     inner join  lstlines l on a.lineguid = l.lineguid       l.linename = 'earthquake' , year(effectivedate) = 2016 

enter image description here

but then, if run query gives me total counts of 15095.

select  count(controlno) quotedtotal    tblquotes inner join  lstlines l on a.lineguid = l.lineguid       l.linename = 'earthquake' , year(effectivedate) = 2016 

enter image description here

what changing total amount , why? , why use first scenario? , way modify first query sum of 15586 without breaking down each row? thank

it seems because field controlno not unique , there records sharing value, although not of them join against lstlines table condition. last query does:

select count(a.controlno) lstlines l inner join tblquotes on a.lineguid = l.lineguid l.linename = 'earthquake' , year(effectivedate) = 2016 

while first 1 does:

select count(b.controlno) lstlines l inner join tblquotes on a.lineguid = l.lineguid inner join tblquotes b on a.controlno = b.controlno l.linename = 'earthquake' , year(effectivedate) = 2016 

as can see, in second query not counting rows match lstlines table, rows in tblquotes have same controlno match against lstlines.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -