sql - Sum a column that corresponds a aggregate from a column of another table -
sorry title, couldn't think of better subject issue.
i've 2 tables in many-to-one relationship between them , other many-to-one relatioshop in same table. need sum column corresponds aggregate column of first table.
here image illustrate want:
have no idea how proceed this.
this should do:
select isnull(e.idparent,e.id) id, sum(i.value) [sum] expense_table e left join invoice_table on i.idexpense = e.id group isnull(e.idparent,e.id)
for updated requirement:
select isnull(e.idparent,e.id) id, e2.[description], sum(i.value) [sum] expense_table e left join invoice_table on i.idexpense = e.id inner join expense_table e2 on isnull(e.idparent,e.id) = e2.id group isnull(e.idparent,e.id), e2.[description]
Comments
Post a Comment