对postgresql中的查询获得的值进行排序

时间:2013-11-21 05:51:27

标签: sql postgresql sorting

select SUM (Account_Invoice.amount_untaxed),
       right (Res_Partner.vat,length(Res_Partner.vat)-2) as RFC 
from Account_Invoice inner join Res_Partner on Account_Invoice.partner_id = Res_Partner.id 
     inner join Account_Invoice_Tax on Account_Invoice.id = Account_Invoice_Tax.invoice_id 
where account_invoice.journal_id=2
      and account_invoice.date_invoice >= '2013-01-01' 
      and account_invoice.date_invoice <= '2013-02-01'
      and account_invoice.reconciled is TRUE 
      and account_invoice_tax.account_id = 3237 and account_invoice.amount_tax >= 0
      or account_invoice_tax.account_id = 3236 or account_invoice_tax.account_id = 3238

我希望将3236的免税金额分类为3237,最后是3238的金额。我明白我必须使用“分组”,但我不明白具体如何。有什么指针吗?

2 个答案:

答案 0 :(得分:1)

这对我有用,它将值从3236,然后是3237,然后是3238进行排序。

select SUM(Account_Invoice.amount_untaxed) as monto, 
right (Res_Partner.vat,length(Res_Partner.vat)-2) as RFC
from Account_Invoice 
inner join Res_Partner on Account_Invoice.partner_id = Res_Partner.id 
inner join Account_Invoice_Tax on Account_Invoice.id = Account_Invoice_Tax.invoice_id 
where account_invoice.journal_id=2 
and account_invoice.date_invoice >= '2013-01-01' 
and account_invoice.date_invoice <= '2013-02-01' 
and account_invoice.reconciled is TRUE 
and account_invoice_tax.account_id in (3237,3236,3238)
and account_invoice.amount_tax >= 0
GROUP BY vat,account_invoice_tax.account_id
ORDER BY account_invoice_tax.account_id;

感谢帮助人员。

答案 1 :(得分:0)

尝试ORDER BY SUM()

select account_invoice_tax.account_id, SUM (Account_Invoice.amount_untaxed),
   right (Res_Partner.vat,length(Res_Partner.vat)-2) as RFC 
from Account_Invoice inner join Res_Partner on Account_Invoice.partner_id = Res_Partner.id 
 inner join Account_Invoice_Tax on Account_Invoice.id = Account_Invoice_Tax.invoice_id 
where account_invoice.journal_id=2
  and account_invoice.date_invoice >= '2013-01-01' 
  and account_invoice.date_invoice <= '2013-02-01'
  and account_invoice.reconciled is TRUE 
  and account_invoice_tax.account_id IN(3236,32387,323)  
  AND account_invoice.amount_tax >= 0
GROUP BY account_invoice_tax.account_id
ORDER BY account_invoice_tax.account_id, SUM (Account_Invoice.amount_untaxed)