0

我有一个这样的查询:

select a,b,c
from
table

现在我有另一个表,其中有 hoursWorked。基本上我想要 anotherTable.hoursWorked 的总和,其中 anotherTable.a = a 和 anotherTable.b = b 和 anotherTable.c = c。

我需要什么样的查询才能做到这一点?

谢谢

像这样的东西:

SELECT DATE, CUSTOMER, SUM(FKEY.HOURSWORKED)
FROM TABLE1
INNER JOIN TABLE2 FKEY ON FKEY.CUSTOMER = CUSTOMER, FKEY.DATE = DATE
4

2 回答 2

1

你会写这样的东西:

SELECT SUM(at.hoursWorked)
FROM
    anotherTable at
    JOIN (SELECT a, b, c FROM thisTable) t
    ON at.a = t.a AND at.b = t.b AND at.c = t.c
于 2012-10-02T11:51:27.427 回答
0

修改您的示例 SQL 以使其正常工作:

SELECT TABLE1.DATE, TABLE1.CUSTOMER, SUM(FKEY.HOURSWORKED) AS HOURSWORKED
FROM TABLE1
INNER JOIN TABLE2 FKEY
ON FKEY.CUSTOMER = TABLE1.CUSTOMER AND FKEY.DATE = TABLE1.DATE
GROUP BY TABLE1.DATE, TABLE1.CUSTOMER

最重要的变化是我添加了一个GROUP BY子句。

于 2012-10-02T12:02:11.343 回答