sql - combining two queries -


This first query captures all transactions_details for a specific date range, it is difficult, however, Need the amount in the query if it does have any meaning.

  SELECT td.transaction_id, sum (IF (Sahara (ti.na, -1) = 0 and covalent (ti.special_info_type, -1) = 0 and insert (ti.item_type, ' '=' P 'and organized (ti.comp_id, 0) & lt; = 0, organized (disc_amt, 0), 0)) disc_sum, sum (IF (insert (as ti.na, -1) = 0 and organized (ti.special_info_type, -1) = 0 and organized (ti.item_type, '') = 'P' and organized (ti.comp_id, 0) & gt; 0, organized (comp_amt, 0), 0) ) CSM, as the amount (if (insert (ti.na, -1) = 0 and organized (ti.special_info_type, -1) = 0 and organized (ti.item_type, '') = 'P' and organized ( Ti.comp_id, 0)> 0, (Comp_tax, 0) inserted, 0)) ctx form transaction_details to include td left externally Ti.transaction_id = td.transaction_id on transaction_items ti td.na = 0 and td.entry_TS & gt; =? And td.entry_TS & lt; ? TD by Group Transaction_id;  

This query is executed in the loop for each transaction returned from the previous query. RefAmt, collect (sum (x.efftax) as the syLECT count (x.id) refCnt, as syndication (sum (X.item_price + x.sub_price), 0) , 0) as Rifaks (SELECT (tiP.item_price - tiP.comp_amt) as item_price, consolidate (sum (tiA.item_price), 0) as sub_price, (tiP.efftax - tiP.comp_tax) as efftax, transaction_items fresh tiP.id join left outer (tiP.id = tiA.ref_id and tiA.item_type = 'a' and Tia transaction_items Tia. na = 0) where tiP.item_type = 'p 'And tiP.na = 0 and tiP.refund = 1 #and tiP.transaction_id = tiP.transaction_id, tiP.order_id) by the group as tiP.id by x;

First of all, you query your COALESCE .

NULL values ​​do not contribute to SUM in your argument, so you can quickly filter them:

  SELECT td Transaction_id, SUM (IF (ti.comp_id & lt; 0 or ti.comp_id IS nULL, Disk_amti, 0), SM (IF (TI Coppeediaidi & gt; 0, COMP_AMT, 0), SM (IF (TI Copas_aidi & gt; 0 ), Comp_tax, 0) transaction_details td from LEFT OUTER Join transaction_items ti oN ti.transaction_id = td.transaction_id WHERE td.entry_TS & gt; =? aND td.entry_TS & lt ;? aND ti.na = 0 and ti.special_info_type = 0 and TI .item_type = 'T' by T.R. Transaction_ID;  

second, SUM lower Is a Uetative, i.e. SUM code> SUM s is a SUM contribution values.

You can use intermediate SUM 'S.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -