本文介绍了对与另一个表的某个列的聚合相对应的列求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起标题,但我想不出一个更好的主题。

Sorry about the title, but i couldn't think of a better subject for my issue.

我有两张桌子,多对一关系它们之间的关系以及同一表中的其他多对一关系商店。我需要对与第一张表的某个列的汇总相对应的列进行求和。

I've two tables in a many-to-one relationship between them and an other many-to-one relatioshop in the same table. I need to sum a column that corresponds a aggregate from a column of first table.

以下是说明我想要的图像:

Here is a image that illustrate what i want:


我不知道如何进行此操作。

I have no idea how to proceed with this.

推荐答案

应这样做:

SELECT  ISNULL(E.idParent,E.id) Id,
        SUM(I.Value) [Sum]
FROM EXPENSE_TABLE E
LEFT JOIN INVOICE_TABLE I
    ON I.idExpense = E.id
GROUP BY ISNULL(E.idParent,E.id)

更新的要求:

SELECT  ISNULL(E.idParent,E.id) Id,
        E2.[description],
        SUM(I.Value) [Sum]
FROM EXPENSE_TABLE E
LEFT JOIN INVOICE_TABLE I
    ON I.idExpense = E.id
INNER JOIN EXPENSE_TABLE E2
    ON ISNULL(E.idParent,E.id) = E2.id
GROUP BY ISNULL(E.idParent,E.id),
         E2.[description]

这篇关于对与另一个表的某个列的聚合相对应的列求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 07:27