我有下面提到的表结构。
Master-Child Table:
Master_Job_Id Child_Job_Id
M1 C1
M1 C2
金额表(此表包含子和主要工作ID的详细信息):
Job_Id Rate_Id Amount
M1 R1 50
C1 R1 10
C2 R2 25
费率表
Rate_Id Lang_Id
R1 L1
R2 L2
这里的M1工作ID有两个孩子C1和C2。我想构建一个查询,它将为我提供以下数据:
Job_Id Lang_Id Amount
M1 L1 60(Since M1 has two L1 values - that of M1 and C1, the amounts are added so 60 should come)
M1 L2 25(Here M1 has only one L2 value so the total amount value is 25)
非常感谢任何帮助。
此致
答案 0 :(得分:1)
这应该有效:
SELECT t.master_job_id,r.Lang_Id,sum(a.amount)
FROM Master_Child t
INNER JOIN amount a
ON(a.job_id in(t.master_job_id,t.child_job_id))
INNER JOIN Rate r
ON(a.rate_id = r.rate_id)
GROUP BY t.master_job_id,r.lang_id