Trying to create a query which will send me back the percentage of USER_TOT_REQS.
How do I get the result back with decimals?
SELECT u.USER_NAME
,SUM(t.REQ_AMOUNT) as 'USER_TOTAL_AMOUNT'
,COUNT(t.ID) as 'USER_TOT_REQS'
,(COUNT(t.ID)* 100 / (Select COUNT(*) from TB__TOMBSTONES t
JOIN TB__USERS u
ON u.ID = t.CURRENT_BUYER__ID
WHERE 1=1
AND t.REGION__ID = 1
AND t.OFFICE__ID = 1
AND t.STL_ASSIGNED__ID = 8)) AS 'Percentage'
FROM TB__TOMBSTONES t
JOIN TB__USERS u
ON u.ID = t.CURRENT_BUYER__ID
WHERE 1=1
AND t.REGION__ID = 1
AND t.STL_ASSIGNED__ID = 8
GROUP BY u.USER_NAME
ORDER BY USER_TOTAL_AMOUNT DESC
Basing myself on a few examples I’ve seen on stackoverflow – Thanks.
Your counts are inversed. The outer select is less selective than the nested select (which has an additional
t.OFFICE__ID = 1predicate… (unless you forgot that in the outer select?)The expression should be
For increased precision, make that
100.0, but that’s probably not the main issue here… Besides that, your nested select doesn’t join on the outer select’s user. Instead, for every group, it selects all users. So even more correctly: