我希望将机会值加在一起,并在表2中的assigned_by_id中将其分组,其中表1中的company_id与表2中的id匹配。更多细节:
表一
ID, COMPANY_ID, OPPORTUNITY, DATE_CREATE
表二
ID,ASSIGNED_BY_ID
我要做的是为每个ASSIGNED_ID添加OPPORTUNITY中的所有值。 表格一中的COMPANY_ID与表格二中的ID相同。
例如:
表一
COMPANY_ID | OPPORTUNITY | DATE_CREATE
1000 | 50 | 2013/09/19
1000 | 100 | 2013/09/18
1000 | 200 | 2013/09/18
1005 | 100 | 2013/09/18
1005 | 200 | 2013/09/18
表二
ID | ASSIGNED_BY_ID
1000 | 4
1000 | 4
1000 | 4
1005 | 2
1005 | 2
所以我想要一个能提供这些结果的SELECT语句:
ASSIGNED_BY_ID | OPPORTUNITY
4 | 350
2 | 300
我希望每个ASSIGNED_BY_ID个别选择语句。
这怎么可能?
答案 0 :(得分:2)
SELECT b.Assigned_By_ID, SUM(a.OPPORTUNITY )
FROM tbOne a
INNER JOIN
(
SELECT DISTINCT ID, Assigned_By_ID
FROM tnTwo
) b ON a.COMPANY_ID = b.ID
GROUP BY b.Assigned_By_ID
输出
╔════════════════╦═════════════╗
║ ASSIGNED_BY_ID ║ OPPORTUNITY ║
╠════════════════╬═════════════╣
║ 2 ║ 300 ║
║ 4 ║ 350 ║
╚════════════════╩═════════════╝