Find the SQL Query behind an Ax table
Sorry, this isn't what I meant. There is a way to get an actual query string used by the form, not just this graphical representation. Unfortunately it's so many years since I used AX 2012 for the last time that I don't remember how to do it. I think it can be done somewhere in the Personalization form. If not, you can use the debugger to see that or use code (to call toString() of the root data source and put the result to infolog, for example). It will give you psedo-SQL code, not the actual T-SQL code sent to database, but it's still very useful.
Your SQL code doesn't resemble the query used by the form at all. Most importantly, you're missing all aggregation functions and grouping by dimensions. Another serious problem is that you forgot to filter the data by DataAreaId (and Partition). It's done automatically by F&O but you need to do it manually if you bypass the application layer and you write SQL queries directly against the database.
Also, F&O queries don't support the distinct keyword.