material 到 material 传输的计算视图

Calculation view for material to material transfer

我有一个 table Tab1 有两行

+-------+----------+--------------+----------+--------+
| DOC#  | Material | Debit/Credit | Quantity | Amount |
+-------+----------+--------------+----------+--------+
| 12345 | A1       | Credit       |        5 |     50 |
| 12345 | B1       | Debit        |        5 |     50 |
+-------+----------+--------------+----------+--------+

想要的结果

+-------+---------------+-------------+----------+--------+
| DOC#  | From Material | To Material | Quantity | Amount |
+-------+---------------+-------------+----------+--------+
| 12345 | A1            | B1          |        5 |     50 |
+-------+---------------+-------------+----------+--------+

我正在使用 SAP HANA,编写计算视图。

您可以按如下方式使用条件聚合:

Select doc#,
       Max(case when debit_credit = 'credit' then material end) as from_material, 
       Max(case when debit_credit = 'debit' then material end) as to_material, 
       Quantity, Amount
  From t
 Group by doc#, quantity, amount

聚合的替代方法是自连接。我想这对初学者来说很容易理解。您将贷方行连接到借方行,就好像它们是两个不同的表一样。

select
  doc_number, quantity, amount,
  debit.material as from_material,
  credit.material as to_material
from (select * from tab1 where credit_debit = 'Credit') as credit
join (select * from tab1 where credit_debit = 'Debit') as debit
  using (doc_number, quantity, amount)
order by doc_number, quantity, amount;

可能是HANA缺少USING子句。然后查询将变为

select
  credit.doc_number, credit.quantity, credit.amount,
  debit.material as from_material,
  credit.material as to_material
from (select * from tab1 where credit_debit = 'Credit') as credit
join (select * from tab1 where credit_debit = 'Debit') as debit
  on  credit.doc_number  =  debit.doc_number
  and credit.quantity    =  debit.quantity
  and credit.amount      =  debit.amount
order by credit.doc_number, credit.quantity, credit.amount;