0

我有 2 个名为 tabItem 和 tabBin 的表。现在通过如下所述的代码以数据透视表格式获取数据。现在下面的代码按预期工作,但我无法添加列和行总计字段。

现在行总数不是基于行中的所有单元格,而是我希望仅对一行中的以下单元格求和,列标题为 1D + 2B + BRG + BHT,我无法找到这样做的方法,我们需要将此列命名为“总计”。

我还需要添加另一列,它将打印以下内容:

  1. 如果 1D + 2B < SO 那么“1 CORD”
  2. 如果 Total < SO + ROL 那么“2 CSTK”

同样,还有许多其他条件,例如 10~15 个条件,我只是不知道如何编写代码,因为提到的所有代码都是通过在 google 上搜索编写的,但现在我无法找到答案,我不在都精通mysql。任何帮助将不胜感激。

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
4

1 回答 1

0

尝试使用子查询来执行此操作。使用您的查询。. .

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

这应该让您了解如何进行。

于 2013-01-12T03:08:18.797 回答