0

I have following tables in mysql:

salesinvoices (with invoice number, ledgerid and date)

salesinvoiceitems (with invoice number)

payments( with invoice number, ledgerid and date)

Now i need to calculate the total amount from salesinvoiceitems table against a specific invoiceNumber for a particular ledgerid by using the calculations of taxations etc (columns included in table).

Then i have a payments table that maintains the records of all the payments made against specific invoices date wise. This table also contains invoice number and ledger id.

I need to generate a report for a specific ledger id showing the end balance. I am clueless for such query. Please shed some light.

4

1 回答 1

0

我假设您的 salesinvoiceitems 表有一个“金额”字段,以便我们可以计算每张发票的项目金额的总和。在下面的示例中,我将该列称为“item_amt”。我们可以尝试做这样的事情......

select ledgerid , sum(balance) as total_balance
from 
-- sub query to calculate balance per invoice and ledgerid
(
select distinct 
 invoices.invoice_number, 
 invoices.ledgerid, 
 tot_item_amt, 
 tot_payment_amt, 
 (tot_item_amt - tot_payment_amt) as balance
from salesinvoices as invoices
-- sub query to get the sum of all the items on an invoice
inner join (select invoice_number, sum(item_amt) as tot_item_amt from salesinvoiceitems group by invoice_number) as items on invoices.invoice_number = items.invoice_number
-- sub query to get the sum of all the payments made against an invoice
inner join (select invoice_number, sum(payment_amt) as tot_payment_amt from payments group by invoice_number) as payments on invoices.invoice_number = payments.invoice_number
) t
-- sum balance and group by ledgerid
group by ledgerid 
于 2013-05-29T18:23:55.893 回答