带有分组依据的SQL查询问题

时间:2012-06-07 10:50:20

标签: sql sql-server sql-server-2000

Table 1
Id(Key) Name
1       XYZ
2       ABC

Table 2
ID(Key) Bank
1       SC
2       CP

Table 3
Id(no key) month year noofduty
1          03     12   20
2          04     12   22
1          03     12   25
2          04     12   15

必填结果

ID Name Bank TotalDuty
1   XYZ  SC    42
2   ABC  CP    40

我在多个表中与GroupBy混淆,需要帮助。使用SQL 2000 Server

1 个答案:

答案 0 :(得分:3)

这是SQL Fiddle with a Demo

SELECT t1.id
  , t1.name
  , t2.bank
  , sum(t3.noofduty) as totalduty
FROM t1
INNER JOIN t3
  on t1.id = t3.id
INNER JOIN t2
  ON t2.id = t3.id
GROUP BY t1.id, t1.name, t2.bank