- You have an extra ending parenthesis, which should be removed
- Views should not have
ORDER BY
.
- The
WHERE
clause should not be in the middle of the SELECT
, you will need to use a CASE
.
- You are returning a Cartesian product instead of joining your tables together, this may provide more results than you want/expect.
The R.RID
in the THEN
block of the CASE
statements may need to be a 1
, not entirely sure what you hoped to get from those COUNT
statements you originally had.
I tried to figure out what you were trying to do, and I think this is it:
CREATE VIEW UserResults AS
SELECT E.No, E.Description
, SUM(CASE
WHEN $Username = R.Owner THEN R.RID
ELSE 0
END) AS SumOfOwner
, SUM(CASE
WHEN $Username = R.Owner AND Status = 'Active' THEN R.RID
ELSE 0
END) AS SumOfOwnerAndActive
FROM ETable AS E
INNER JOIN RTABLE as R ON E.No = R.ENo
GROUP BY E.No, E.Description