I have a query that returns data somewhat like this:
REF01 10 50 1
REF01 10 50 1
REF01 20 40 1
REF01 20 40 1
REF01 30 30 2
REF02 40 20 1
REF02 50 10 2
REF02 50 10 2
And I need it to return it like this:
REF01 60 120
REF02 90 30
To do so first I add a DISTINCT (GROUP BY with all the columns also seems to do the same) to remove the duplicates which returns it like this:
REF01 10 50 1
REF01 20 40 1
REF01 30 30 2
REF02 40 20 1
REF02 50 10 2
And then I need to add a SUM() of that result without the removed duplicated ones being counted.
I have tried solutions like this one but I get an error with the FROM (SELECT ...) and this one which does work except that one of the columns inside the subquery is the result of an operation (column_1-column_2-column_3) which causes the error ORA-00972: identifier is too long for exceeding the 30 characters maximum limit and trying to apply an alias with AS in the subquery (inside the WITH SUBQUERY AS ( ... ) isn't working in MS Query from what I have tried.
Support for most SQL functions and such isn't the best on Microsoft Query.
Edit - This is the solution that worked:
The issue seems to be related to the auto-generated column names, that end up being too long for oracle. To avoid this problem, the query can explicitly name the CTE columns, as shown below:
Solution #2 (that didn't work in the tool):
If the tool you are using does not have support for subqueries, you can still trick it by creating a view instead. For example:
Then, just run a query that uses it:
Result:
Solution #3 (that didn't work in the tool either):
Data used for testing:
For reference, the data script I used is: