如何在postgresql中连接多个表?

时间:2015-07-31 09:39:00

标签: xml postgresql view inner-join openerp-7

我的自定义项目中有表: account_move_line > | --- || - account_move - || --- |< account_invoice - || --- |<的 account_invoice_tax

以下是其中一些定义

account_move_line  account_move    account_invoice    account_invoice_tax
-----------------  ------------    ---------------    ------------------- 
   partner_id       partner_id         move_id        base_amount        
   move_id                             partner_id     tax_amount
                                                      name

当我尝试从account_move_line中提取数据时,这些是一些结果

partner_id     |   move_id   |     credit   ....
10000001            58              28.75   ....
10000001            59              78.25   ....
10000001            28              10.00   ....
10000004            11              12.25   ....
10000002            14              20.20   ....

(信用证的实际来源是表account_invoice_tax中的tax_amount)

我的问题是:如何才能获得account_move_line中partner_id仅出现一次的base_amount和tax_amount的总和?

到目前为止,我所做的是:

1.我创建第一个视图:

create or replace view ewt as (           
      select ai.id as id,ai.partner_id as partner_id,
          sum(abs(ait.tax_amount)) as tax_amount,
          sum(ait.base) as base_amount,ait.name 
      from account_invoice ai
      inner join account_invoice_tax ait 
          on ai.id = ait.invoice_id where ait.name like '%EWT%' 
          group by ai.id,ai.partner_id,ait.tax_amount,ait.base,ait.name 
          order by ai.partner_id
)

2.我创建第二个视图:

create or replace view ewt_f as (           
    select ewt.partner_id as id,
           ewt.partner_id as  partner_id,
           sum(ewt.tax_amount) as tax_amount,
           sum(ewt.base_amount) as base_amount,
           ewt.name 
    from ewt group by ewt.partner_id,ewt.name 
    order by (select res.name from customer res 
                 where res.id = ewt.partner_id 
              order by res.name asc) asc 
)

,第二个是我传递给我的xml,但我的排序不起作用。 是否可以为此创建一个视图以及如何创建?为什么我的排序不起作用?

我想要的输出实际上是这样的: (从表格视图的结果)

partner_id    |    base_amount    |   tax_amount ....
10000001              579.00          117.00(from the sum of tax_amount in table account_invoice_tax so does base_amount)      
10000004              250.00          12.25
10000002              150.00          20.20

非常感谢任何建议

修改

account_move_line (is) many2one (to) account_move
account_move (is) one2many (to) account_invoice
account_invoice (is) one2mny (to) account_invoice_tax

account_invoice_tax has these following columns:
name,
base_amount,
tax_amount,
invoice_id,

account_invoice:
name,
partner_id,
move_id,
...

account_move:
name,
partner_id
...

account_move_line:
partner_id
name
debit
credit
....

从那些表中使用sql语句,我希望显示如下内容:

来自account_move_line的

partner_id,来自account_invoice_tax的base_amount的总和(无论它是invoice_id),因为account_move_line中的信用卡或借记卡是account_invoice_tax中的tax_amount。我在如何在account_invoice_tax中拉取base_amount时遇到问题。

1 个答案:

答案 0 :(得分:0)

不确定我是否正确了解您的数据库模型。你应该尝试给出一个更好的定义。

但是,不是一个群体......拥有你正在寻找的条款吗?

select partner_id, sum (base_amount + tax_amount)
from account_invoice_tax
group by partner_id
having count(*) = 1

编辑: 带连接的sameidea会给出:

select t.partner_id, sum (t.base_amount + t.tax_amount)
from account_invoice_tax t, account_move_line l
where t.partner_id = l.partner_id
group by t.partner_id
having count(*) = 1