snippetsqlMinor
How to use JOIN and SUM function to Calculate the total of query returned column?
Viewed 0 times
totalreturnedthehowcolumnqueryfunctionjoincalculatesum
Problem
I ran bellowing query;
Query:
And got bellowing result;
Query result:
Now, I want to calculate all unitPrice to get the total. the result I expect is;
Expecting result:
Therefor bellowing is the query I tried;
Query I tried:
But it didn't give me the result I expected, where I went wrong and what I gotta do to get the result I expect?
Query:
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.jobIdAnd got bellowing result;
Query result:
jobId | productId | unitPrice
_____________________________
1 | 4 | 175.99
1 | 5 | 100.00
1 | 6 | 125.00Now, I want to calculate all unitPrice to get the total. the result I expect is;
Expecting result:
jobId | productId | Total
____________________________
1 | 4 | 175.99
1 | 5 | 100.00
1 | 6 | 125.00
null | null | 400.99Therefor bellowing is the query I tried;
Query I tried:
SELECT j.jobId,
j.productId,
SUM(p.productUnitPrice) AS 'Total'
FROM JobRequiredProducts J
JOIN Product p
ON p.productId = j.productId
WHERE j.jobId = 1
GROUP BY j.jobId,j.productId WITH ROLLUPBut it didn't give me the result I expected, where I went wrong and what I gotta do to get the result I expect?
Solution
You can use grouping set also
with grouping sets
output:
dbfiddle here
declare @JobRequiredProducts table (jobId int, productId int);
declare @Product table(productId int, productUnitPrice decimal(8,2))
insert into @JobRequiredProducts(jobId,productId)
values(1,4),(1,5),(1,6);
insert into @Product(productId, productUnitPrice)
values(4,175.99),(5 ,100.00),( 6 , 125.00)
SELECT j.jobId,
j.productId,
SUM(p.productUnitPrice) AS 'Total'
FROM @JobRequiredProducts J
JOIN @Product p
ON p.productId = j.productId
WHERE j.jobId = 1
GROUP BY ROLLUP ((j.jobId, j.productId));with grouping sets
SELECT j.jobId,
j.productId,
SUM(p.productUnitPrice) AS 'Total'
FROM @JobRequiredProducts J
JOIN @Product p
ON p.productId = j.productId
WHERE j.jobId = 1
GROUP BY GROUPING SETS( (j.jobId, j.productId),());output:
jobId productId Total
1 4 175.99
1 5 100.00
1 6 125.00
NULL NULL 400.99dbfiddle here
Code Snippets
declare @JobRequiredProducts table (jobId int, productId int);
declare @Product table(productId int, productUnitPrice decimal(8,2))
insert into @JobRequiredProducts(jobId,productId)
values(1,4),(1,5),(1,6);
insert into @Product(productId, productUnitPrice)
values(4,175.99),(5 ,100.00),( 6 , 125.00)
SELECT j.jobId,
j.productId,
SUM(p.productUnitPrice) AS 'Total'
FROM @JobRequiredProducts J
JOIN @Product p
ON p.productId = j.productId
WHERE j.jobId = 1
GROUP BY ROLLUP ((j.jobId, j.productId));SELECT j.jobId,
j.productId,
SUM(p.productUnitPrice) AS 'Total'
FROM @JobRequiredProducts J
JOIN @Product p
ON p.productId = j.productId
WHERE j.jobId = 1
GROUP BY GROUPING SETS( (j.jobId, j.productId),());jobId productId Total
1 4 175.99
1 5 100.00
1 6 125.00
NULL NULL 400.99Context
StackExchange Database Administrators Q#241256, answer score: 9
Revisions (0)
No revisions yet.