i am using this query to get cost of the product
select
SUM(strg_op.opcost)/ SUM(strg_op.opvalue * 1000) * strg_catgs.Disrate as totalcost
from
strg_catgs
INNER JOIN strg_op on strg_op.ElemID = strg_catgs.elemID
and strg_catgs.elemID =(
select
strg_catgs.elemID
where
strg_catgs.catID = '13'
)
and strg_catgs.catID = '13'
and strg_catgs.descr = "cwe"
GROUP by
strg_catgs.elemID
i get result like this
| totalcost|
| -------- |
| 1.1 |
| 5.814681 |
i need result to sum all values in total cost column like (1.1 + 5.814681) but in one field like this
| totalcost |
| -------- |
| 6.9 |
when i trying to use this query
select
sum(
SUM(strg_op.opcost)/ SUM(strg_op.opvalue * 1000) * strg_catgs.Disrate
) as totalcost
from
strg_catgs
INNER JOIN strg_op on strg_op.ElemID = strg_catgs.elemID
and strg_catgs.elemID =(
select
strg_catgs.elemID
where
strg_catgs.catID = '13'
)
and strg_catgs.catID = '13'
and strg_catgs.descr = "cwe"
GROUP by
strg_catgs.elemID
not working
2
Answers
Since we can not see the sample data and table structure,so the solution below might not be working for you,but you can take it as a reference,there are two options can do it:
a. remove
group by
(this seems a more concise choice)b. add an outer query to sum the value:
seems like you’re trying to calculate the total cost for each strg_catgs.elemID and then sum up those individual total costs to get a grand total. However, the approach you’ve taken in your second query might not yield the desired result because it’s trying to use the SUM function twice, which can cause incorrect aggregation.
Here’s an alternative way to structure your query to achieve the desired result: