简体   繁体   中英

Mysql Pivot Table: How to get row and column totals and add a conditional column?

I have 2 tables named tabItem and tabBin. Now data is fetched in a Pivot table format via the code as mentioned below. Now the code below works as per expectations, but I am unable to add a column and a row total field.

Now the Row total is not based on all the cells in the rows, instead I am looking to sum only the following cells in a row, column headers are 1D + 2B + BRG + BHT, I am unable to find a way of doing this, we need to name this column "Total".

Also I need to add another column which would print the following:

  1. If 1D + 2B < SO then "1 CORD"
  2. If Total < SO + ROL then "2 CSTK"

Similarly there are many other conditions something like 10~15 conditions, I am just not aware as to how to write the code since all the code mentioned has been written by searching on google but now I am unable to find answers and I am not at all versed with mysql. Any help would be highly appreciated.

select 
 `tabItem`.name as "Item Code:Link/Item:150",
 `tabItem`.description as "Description::300",
 ifnull(`tabItem`.re_order_level,0) as "ROL:Currency:50",
 ifnull(`tabBin`.reserved_qty,0) as "SO:Currency:50",
 ifnull(`tabBin`.ordered_qty,0) as "PO:Currency:50",
 ifnull(min(case when `tabBin`.warehouse="DEL20A" then `tabBin`.actual_qty end),0) as "1D:Currency:50",  
 ifnull(min(case when `tabBin`.warehouse="BGH655" then `tabBin`.actual_qty end),0) as "2B:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="RG-BGH655" then `tabBin`.actual_qty end),0) as "BRG:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="HT-BGH655" then `tabBin`.actual_qty end),0) as "BHT:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="FG-BGH655" then `tabBin`.actual_qty end),0) as "BFG:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="SLIT-DEL20A" then `tabBin`.actual_qty end),0) as "DSL:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="RG-DEL20A" then `tabBin`.actual_qty end),0) as "DRG:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="FG-DEL20A" then `tabBin`.actual_qty end),0) as "DFG:Currency:50", 
 ifnull(min(case when `tabBin`.warehouse="TEST-DEL20A" then `tabBin`.actual_qty end),0) as "DTS:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="REJ-DEL20A" then `tabBin`.actual_qty end),0) as "DRJ:Currency:50",   
 ifnull(min(case when `tabBin`.warehouse="RM-DEL20A" then `tabBin`.actual_qty end),0) as "DRM:Currency:50",    
 ifnull(min(case when `tabBin`.warehouse="RM-BGH655" then `tabBin`.actual_qty end),0) as "BRM:Currency:50"  
 from
 `tabItem`
 left join `tabBin` on
 `tabItem`.name = `tabBin`.item_code
where
 `tabBin`.item_code <>""
 and `tabBin`.item_code = `tabItem`.name    
group by `tabItem`.name
order by `tabItem`.name asc

Try doing this with a subquery. Using your query . . .

select t.*,
       ("1D:Currency:50" + "2B:Currency:50" + "BRG:Currency:50" + "BHT:Currency:50"
       ) as tot1,
       (case when "1D:Currency:50" + "2B:Currency:50" < "SO:Currency:50"
             then '1 CORD'
             when ("1D:Currency:50" + "2B:Currency:50" + "BRG:Currency:50" + "BHT:Currency:50" < "SO:Currency:50" + "ROL:Currency:50"
             then  "2 CSTK"
         end)
from (<your query>) t

This should give you an idea of how to proceed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM