I'm creating a query where I can throw inside the data grid view.
Suppose I have 2 table namely table A and table B. Table A has StaffId column and while Table B has StaffId too. Table B can have multiple value of cost with foreign key of StaffId. What I'm trying to do is, get all the sum value inside the Table B with the same StaffId.
Here's the sample table definitions and contents.
// Table A
| StaffId | Name |
| 1 | Dummmy |
// Table B
| Id | StaffId | Cost |
| 1 | 1 | 10.00 |
| 2 | 1 | 10.00 |
| 3 | 1 | 10.00 |
SELECT A.Name, Sum(B.Cost) FROM B INNER JOIN A ON A.StaffId=B.StaffId
Dummy | 30.00
Try like this,
To find sum of cost for each and every StaffId's you should use this.
SELECT A.StaffId ,A.NAME ,Sum(B.Cost) as Total FROM B INNER JOIN A ON A.StaffId = B.StaffId GROUP BY A.StaffId ,A.NAME
To find all the cost for all the StaffId's you should use this.
SELECT Sum(B.Cost) FROM B INNER JOIN A ON A.StaffId = B.StaffId