I am trying to avoid multiple queries / putting data into temp tables and so am trying to write a query that will do two different sums off of the same table in one query.
select
s.name,
sum(t1.Quantity),
sum(t2.Quantity)
from
SecurityMaster s
inner join Trades t1 on s.SecurityID = t1.SecurityID
inner join Trades t2 on s.SecurityID = t2.SecurityID
and t2.value < 100
group by
s.name
My expectation was that for each row in the SecurityMaster table there would be a one-to-one relationship to each of the Trades tables. The problem is that the items in the Trades table are getting read multiple times per loop and the sums are not correct (way too high by a factor of 16).
Any advice?
select
s.name,
sum(t1.Quantity),
sum(t2.Quantity)
from
SecurityMaster s
inner join Trades t1 on s.SecurityID = t1.SecurityID
inner join Trades t2 on s.SecurityID = t2.SecurityID
and t2.value < 100
group by
s.name
My expectation was that for each row in the SecurityMaster table there would be a one-to-one relationship to each of the Trades tables. The problem is that the items in the Trades table are getting read multiple times per loop and the sums are not correct (way too high by a factor of 16).
Any advice?