sql server - Need help optimizing a simple query -


let's have following query:

select   count(id) numrows,    sum(value) totsum  sometable foo= @bar 

simple enough. sums values column foo equals passed parameter, , counts how many rows involved in sum. now, in program have logic evaluates value if resultant count below, let's say, 4.

i want optimize query empty result set if count above 4. first idea slap condition on query (where numrows < 4), wouldn't work because numrows isn't column defined in sometable. second idea make query subquery, , filter results through subquery's table, so:

select  *  (    select      count(id) numrows,       sum(value) totsum     sometable    foo= @bar  ) subquery subquery.numrows < 4 

my main question solution whether or not subquery computed, or if main query halts numrows hits 4.

you can add original query. more easy-to-read , (not sure) better performance.

having count(id) < 4 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -