2

I am wanting to add together the opportunity values and group them by assigned_by_id in table two where the company_id in table one matches the id in table two. A little more detail:

Table One

ID, COMPANY_ID, OPPORTUNITY, DATE_CREATE

Table Two

ID,ASSIGNED_BY_ID

What I am trying to do is add all the values in OPPORTUNITY for each ASSIGNED_ID. COMPANY_ID in table one is the same as ID in table two.

So for example:

Table One

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

Table Two

ID         | ASSIGNED_BY_ID 
1000       | 4
1000       | 4
1000       | 4
1005       | 2
1005       | 2

So I want a SELECT statement that will provide these results:

ASSIGNED_BY_ID  | OPPORTUNITY
4               | 350
2               | 300

I would like individual select statements per ASSIGNED_BY_ID.

How is this possible?

4

1 回答 1

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 ║
╚════════════════╩═════════════╝
于 2013-09-19T16:30:44.380 回答