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?

Parents
  • +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. 

Reply
  • +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. 

Children
  • 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!