I have used a less than elegant solution that may or may not fit your requirements. I simply created a View in the database for what I needed (i.e. - hopefully the view can apply all the constructs you use in the script). I then added the view as a "Data Connection" - under "Generic Data Sources" - at which point I have a custom object that I can query against. Since the view does all the heavy lifting I use the Query tool in LDSD just to select to columns I want tand to apply any filters.
This works very well for example when I need data from t business objects - where neither have all the data I want (an example was for PC's - I have 1 Object that had the CI's that had a User Assigned - and 1 that had All CI's - but I wanted to show ALL CI's and an assigned user IF one existed). So - pretty much anything you can do in SQL can get done in a view - unless you need a runtime value to construct the view (easy to use a runtime value to limit the results - just not to create the view).
The limitation is that the query result is related to my custom Object (the view) - I understand why this is, just wish somehow the result set could be linked to another object. To make sure this is clear - if I click any of the items in the returned query I will see all of the columns for my custom view for the particular row selected. It would be great for example if I could link my object to another - for example so my custome view result could link to the Configuration Item object - if this is possible I have not done so.
Perhaps this helps - perhaps not,
Terry