Чтобы проверить условие в столбце, а затем выполнить операцию в улье - PullRequest
0 голосов
/ 19 сентября 2018

У меня есть таблица улья, которая выглядит следующим образом:

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) , есть ли отрицательное значение в столбце Количество .Если да, то суммируйте значения количества и сравните с 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 голосов
/ 20 сентября 2018

Использовать агрегирование с регистром:

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' своей таблицей.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...