检查列中的条件,然后在配置单元中执行操作

时间:2018-09-19 08:54:53

标签: hive hiveql

我有一个如下所示的配置单元表:

PO_No     Line_item     Quantity      Value       PO_quantity           PO_value
101         10            100           5000           70                 5000 
101         10            -30          -2000           70                 5000 
101         20            200           7000           50                 1000
101         20            -50          -3500           50                 1000 
101         30            80            2000           100                3000
101         30            40            1250           100                3000

现在,我要先连接PO_No和Line_item(这很简单),然后检查 concat(PO_No,Line_item)的每种组合, Quantity中是否有负值列。如果是,则对“数量”值求和并与PO_quantity进行比较。最后,结果应为:

PO_No  Line_item  quantity  Value  PO_quantity  PO_value Comments
___________________________________________________________________________
101  10  70  3000  70  5000   Quantity & PO_quantity match and negative value in Quantity present
101  20  150 3500  50  1000   Quantity & PO_quantity unmatched and negative value in Quantity present
101  30  120 3250  100 3000   Quantity & PO_quantity unmatched and negative value in Quantity not present

请帮助您实现这一目标? 在此先感谢!

1 个答案:

答案 0 :(得分:0)

对大小写使用聚合:

select PO_No, Line_item, quantity, Value, PO_quantity, PO_value, 
       concat(
          case when quantity=po_quantity then 'Quantity & PO_quantity match and negative value in Quantity ' 
               else 'Quantity & PO_quantity unmatched and negative value in Quantity ' end,
          case when  min_quantity<0 then 'present' else 'not present'  end
          ) as comments                                                                
  from
      (
       select  PO_No,           Line_item ,
               sum(quantity)    quantity,
               min(quantity)    min_quantity,
               sum(value)       value,
               min(po_quantity) po_quantity,
               min(PO_value)    PO_value
       from
            (--Use your table instead of this subquery
             select stack(6, --PO_No     Line_item     Quantity      Value       PO_quantity           PO_value
                               101,         10,          100,        5000,          70 ,               5000,
                               101,         10,          -30,       -2000,          70 ,               5000,
                               101,         20,          200,        7000,          50 ,               1000,
                               101,         20,          -50,       -3500,          50 ,               1000,
                               101,         30,          80 ,        2000,          100,               3000,
                               101,         30,          40 ,        1250,          100,               3000
                          ) as (PO_No,Line_item,Quantity,Value,PO_quantity,PO_value)
            ) your_table
       group by PO_No, Line_item 
       )s
order by     PO_No, Line_item  

结果:

OK
101     10      70      3000    70      5000    Quantity & PO_quantity match and negative value in Quantity present
101     20      150     3500    50      1000    Quantity & PO_quantity unmatched and negative value in Quantity present
101     30      120     3250    100     3000    Quantity & PO_quantity unmatched and negative value in Quantity not present
Time taken: 136.559 seconds, Fetched: 3 row(s)

只需将“ your_table”子查询替换为您的表即可。