I ran two queries as shown bellow;
query 1: queried with JobRequiredProducts table
and Product table to list the jobId 1 required all product and unit prices
SELECT j.jobId,
j.productId,
p.productUnitPrice
FROM JobRequiredProducts J
JOIN Product p
ON p.productId = j.productId
WHERE j.jobId = 1
ORDER BY j.jobId
query 2: queried with JobRequiredLabours table
and Labour table to list the jobId 1 required labours and worked hours
SELECT j.jobId,
j.labourId,
j.hoursOfWork
FROM JobRequiredLabours J
JOIN Labour L
ON l.labourId = j.labourId
WHERE j.jobId = 1
ORDER BY j.jobId
And I got bellowing results:
Query result:
jobId | productId | productUnitPrice
__________________________
1 | 4 | 1500
1 | 5 | 800
1 | 6 | 1200
jobId | labourId | hoursOfWork
______________________________
1 | 1 | 6
1 | 2 | 8
Now I want to calculate the total job cost by total (productUnitPrices) + (hoursOfWork * 30). And display the total cost separately. Please help me to do it. Note: 30 added to multiply because of hourly rate is 30
Best Answer
What you are primarily looking for is a
SUM
command run on your queries where you willGROUP BY jobId
. That will allow you to get theSUM
of your hours worked as well as the cost of the parts.I can see two options that you can utilize. Now there may be some variables I am missing but this should give you a framework to work with.
Basically what you need to do is get the
SUM
separated out which you should and then the math is fairly easy after that. You are able to getSUM
value by using theGROUP BY
clause in aSELECT
query.Sub Queries As "Tables"
With this approach you create a sort of "table" which has your
SUM
pre calculated which is then added in the mainSELECT
. (I am assuming you have some sort of aJOB
table which is where thejobId
column comes from).CTE's (Common Table Expression)
An alternative that also works is using a
CTE
(a Common Table Expression) which is almost like aVIEW
that exists only for this query. It allows you to pre-define a query and utilize it later in a different command. In this particular instance we are using it to pre-generate theSUM
so we can then use it later in a mainSELECT
. The benefit of this is the logic of how the CTE's are created is not in the mainSELECT
which helps with readability.These queries will get slightly more complicated, if a job requires more than one instance of a single part AND if that is held through some kind of
QUANTITY
COLUMN
or if the Hourly Rate for a given person is not 30 every time. But hopefully this will give you something to work with as a start.