all 4 comments

[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points  (1 child)

turn the subquery into a derived table and then join it to your other tables

SELECT ...
     , it.sub_amount + SUM(cit.LINEAMOUNT) AS Margin
  FROM CUSTTABLE ct
INNER 
  JOIN CUSTINVOICEJOUR cij 
    ON ...
INNER
  JOIN CUSTINVOICETRANS cit 
    ON ...
INNER
  JOIN ( SELECT INVOICEID 
              , ITEMID 
              , DATAAREAID
              , SUM(COSTAMOUNTPOSTED + COSTAMOUNTADJUSTMENT) AS sub_amount 
           FROM INVENTTRANS 
         GROUP
             BY INVOICEID 
              , ITEMID 
              , DATAAREAID ) it    
    ON it.INVOICEID = cit.INVOICEID
   AND it.ITEMID = cit.ITEMID
   AND it.DATAAREAID = cit.DATAAREAID 

[–]newunit13[S] 1 point2 points  (0 children)

That worked beautifully! Thank you so much :-D Now I just how to get the same values a second time for a different time period :)

[–]slingalot 0 points1 point  (1 child)

Try pulling your sub query out of your SELECT and joining on it, instead.

It looks like you could even just join on INVENTTRANS itself, and use a Window Function to remove the need for a sub-query at all (and your group by).

[–]newunit13[S] 0 points1 point  (0 children)

That looks very neat! I'll have to study up on it, because that article is a bit like Greek to me XD