Using SQL queries in a calculated field (report writer)

SOLVED

Hello - I am struggling with the report writer and need some clarification (a long shot, since there is almost 0 documentation on the subject). I am proficient in T-SQL, so am confident in my ability to write queries, but can't get something very simple to work in a calculated field.

It is a pretty simple ledger report that prints you out transactions in a certain account and subaccount. When a transaction has an AP invoice as the source, I would love to be able to pull the vendor name from that invoice. The way we code those transactions apparently is to have a second line in which the ledger account is always 2000 and the subaccount matches the vendor number, IE:

Line 1 - Concrete materials, account 5001 (Materials), subaccount 130 (concrete)

Line 2 - A/P Invoice, account 2000 (Trade accounts payable), subaccount 90 (vendor name)

If the report is ran for account 5001, the second line won't be in the report, thus my resorting to a query in a calculated field. This is what I'm trying to no avail:

select lgrsub.lngnme from lgrsub inner join lgtnln on lgtnln.subact = lgrsub.recnum where lgtnln.lgract = 2000 and lgrtrn.recnum = {lgrtrn.recnum}

this is what I get:

Has anyone successfully used inner joins in a calculated field? It is in the documentation so I know it's possible but... how?

  • 0

    Not sure why you need that.  The landscape version of the general ledger report 2-4-31 already includes the vendor, or job, or equipment or employee names.

    The portrait versions of GL are useless

  • 0 in reply to Char DeLange

    You are correct and that is what is currently being used by our controller... but he hates using it because it doesn't include the posting period of each transaction. He has to drill down on each transaction to confirm the period... seems like such an oversight since the period range is a parameter.

    Would love to help him save time in that respect and recreate this in the most efficient way, but my expectations are low since this isn't documented more than "inner join is a thing!"

    Edit: to clarify, this is only an issue when the report is run for a range of periods, but that is apparently a daily exercise. 

  • 0 in reply to cow is red

    Have you tried creating a new report using the General Ledger or maybe Job Costing?

  • +1
    verified answer

    Add the referenced tables to the FROM clause, [select lgrsub.lngnme from lgrsub, lgtnln, lgrtrn, lgrsub inner join lgtnln on lgtnln.subact = lgrsub.recnum where lgtnln.lgract = 2000 and lgrtrn.recnum = {lgrtrn.recnum}]

    Other Thoughts:

    1. It seems like this would be easier to create this in 13-3 Report Writer using Ledger Transaction Lines as your Primary Table formatted with a two line detail without the use of an Inner Join.
    2. Sage Help is horrible, with limited information and examples or sometimes just missing any information.
    3. Sage SQL is odd at times. The statement structure is confusing and if Sage does not understand it will sometimes create non-related results instead of giving you errors.

    See attached sample of Sage Help - Calculated Fields where Sage has an example that only details a scenario, but gives no actual sample. 

  • 0 in reply to cow is red

    If you run it for one period, then anything dated before or after that month is in the wrong period.  You can turn on a system alert to email you when users post to the wrong period.  You can also turn on the new feature called "change posting period based on transaction date".  That automates most of the posting for you except in payroll posting and AP check posting because the period is selected before you enter the check date. Those would be helpful to eliminate the issue.

    I realize I'm not answering your SQL query question, but in your screenshot you didn't include what table you started the report from.  

  • 0 in reply to Neil Macernie

    Dude, Neil! You are the man, thank you so much for clarifying my syntax troubles for me... I have no idea why they insisted on making this small change from standard T-SQL syntax and refused to document it, but it worked out.

    I had to rethink my query since lgrsub is not the table I actually wanted - it was actpay. This is the query that ended up being a home run:

    [SELECT actpay.vndnme from actpay, lgtnln, actpay inner join lgtnln on lgtnln.subact = actpay.recnum where lgtnln.recnum = {lgtnln.recnum} and lgtnln.subact = {lgtnln.subact}]

    Thanks again Neil!

  • 0 in reply to Char DeLange

    Thank you for your ideas - I really appreciate it. You would be surprised on how often there are correct postings in a different period than the transaction date in the construction industry... at least that's what I have gathered from my limited accounting knowledge as an IT guy, ha

    Also, you can tell I started the report from Ledger Transaction Lines in that screenshot just fyi