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.
I assume your salesinvoiceitems table has an "amount" field so that we can calculate the sum of the item amounts per invoice. In the example below I call that column "item_amt". We could try doing something like this....
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
Related
So I have two tables: Customer and Transactions. I am task with doing the following:
"List the customers with their current balance greater than £5000 (at least 1 customer
should have their current balance greater than £5000). The current balance can be calculated by
summing the opening balance of the account, all the incoming transactions of the account, and
deducting the outgoing transactions of the account."
Here are my tables:
Customer:
https://i.imgur.com/o1JWbbh.png
Transactions:
https://i.imgur.com/QoqVlMc.png
In the first step, you could calculate the balance based only on transactions grouped by customer:
SELECT CustomerID,
sum(IncomingTransactions - OutgoingTransactions) AS balance
FROM Transactions
GROUP BY CustomerID;
The next step is to join the results of this query with the Customer table. It might happen, that there's a customer without transactions, therefore you should use an outer join.
SELECT Customer.*,
OpeningBalance - COALESCE(balance, 0) AS balance
FROM Customer LEFT JOIN
(SELECT CustomerID,
sum(IncomingTransactions - OutgoingTransactions) AS balance
FROM Transactions
GROUP BY CustomerID) balances
ON Customer.CustomerID = balances.CustomerID;
The LEFT JOIN ensures that you'll find each Customer in the result. If there's no transaction for a Customer, the columns of the joined table (Transactions) will be NULL. The COALESCE in the SELECT clause turns NULL-values INTO 0.
I have three tables:
Table1: Salary
Fields: ID,Account_Num,Amount
Table2: Other_Income
Fields: ID,Account_Num,Amount
Table3: Expenses
Fields: ID,Account_Num,Amount
From the above tables,how to write a query which return a result, list all account from these table, and shows the balance of every account in these tables.
Result should show Account_num,Salary,Other_Income,Expenses,
balance(balance=Salary.amount+Other_Income.amount-expenses.amount)
These three table may contains some different account number.
I have been thinking of this so long, tried union and join but still cant make it.
Somebody show/guide me?
I've had some luck with the following query. Essentially, I combine all the transactions in the Salary, Other_Income and Expenses tables into a single temp table named combined, and then use group by to sum all the transactions!
select Account_Num, sum(Amount)
from (
select Account_Num, Amount from Salary union
select Account_Num, Amount from Other_Income union
select Account_Num, -Amount from Expenses
) as Combined
group by Account_Num;
Following query might help , provided that every account has salary against it.
select Account_Num,(total-d.Amount) as final_amount ( select Account_Num, ifnull(a.Amount,0)+ifnull(b.Amount,0) as total from Salary a
left join Other_Income b on a.Account_Num=b.Account_Num ) c
join Expenses d on c.Account_Num=d.Account_Num
SELECT Sal.`Account_Num`, sal.`Amount` AS Salary, ot_in.`Amount` AS ExtraIncome , exp.`Amount` AS expenses, (sal.`Amount`+ot_in.`Amount`-exp.`Amount`) As Balance FROM Salary AS Sal INNER JOIN Other_income AS ot_in ON Sal.`Account_Num` = ot_in.`Acount_num` INNER JOIN Expenses AS exp ON Sal.`Account_Num` = ex.`Account_Num`
Here joining three of the table on a condition where you get the common account number from all three and extracting the correponding amounts from each , calculating the rest balance with a formulae given subtracting expenses from income.. i hop this will help.
I've got two tables, transactions and listings.
transactions list all of my transactions that occur each day. I have a column in transactions that lists the price of each transacted item. Several transactions occur each day. I would like to take the median or mean of all transactions in each day, and populate a new column in listings with this information.
So my end result would have a column in listings called daily_price_average, that takes the average price of individual transaction information from transactions.
Any thoughts on how to do this?
Or how could I do this using a view?
You can do this in a view as:
create v_listings as
select l.*,
(select avg(price)
from transactions t
where date(t.transactiondate) = l.date
) as daily_price_average
from listings l;
To do the update, you would first be sure daily_price_average is a column in listings:
update listings join
(select date(t.transactiondate) as tdate, avg(price) as avgprice
from transactions
group by date(t.transactiondate)
) td
on listings.date = td.tdate
set daily_price_average = td.avgprice;
Both of these assume that listings has a column called date for the average.
Use INSERT... SELECT
For average
INSERT INTO averages (day, average)
SELECT date, AVG(price)
GROUP BY date
Getting the median is... complicated.
Ok so I have a users table and a payments table. Each payment has a date_paid and amount_paid. I would like to group the payments by the sum of the amount paid, but I would also like to keep a field for a last date_paid. Is this possible? Or will I have to store a last_date_paid column within the user table?
SELECT id, SUM(amount_paid), MAX(date_paid)
FROM payments
GROUP BY id
I'm trying to create a query, that will calculate sum of products on invoice. I have 3 tables :
Product (with product's price)
Invoice (with invoice id)
Products on invoice (with invoice id, product id and number of particular products)
So in my query I take invoice_id (from invoice), price (from product),number of products sold and invoice_id (from products on invoice) and calculate their product in fourth column. I know I should use 'Totals' but how to achieve that ?
The following added to your SELECT should do it.
[Product].[price] * [Products on invoice].[number of products on invoice] AS Total
If you include the fields and table names, I can give you a much more accurate statement.
Edit:
SELECT
invoice.invoice_id,
product.price,
products_on_invoice.amount,
product.price * products_on_invoice.amount AS Total
FROM
invoice INNER JOIN
(products_on_invoice INNER JOIN
product
ON products_on_invoice.product_id = product.product_id)
ON invoice.invoice_id = products_on_invoice.invoice_id