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
Related
I have three tables customers, orders, and payments. payments table has a foreign key order_id, which references the orders table on the id field, however, the payments table can also accept payment without any order, i.e.( advance payment received without any order being placed.) conversely it is also possible that there could be orders without any payment e.g. orders created on credit without any payment.
now my query is I want to generate a day-closing table where I want to list all orders if any or all payments if any for a particular date.
I have tried to join the orders table with the payment table on order_date and payment_date but the result is not as per my requirement. i.e. if there are no orders on a particular date but there are payments then I don't get any results.
I have created a scenario on dB fiddle for the same. dbfiddle
if anybody can give me a solution, it will be an immense help.
as per the scenario on dB fiddle
I tried the following query.
select
o.id,
o.order_date,
o.customer,
o.inv_number,
p.pymt,
p.cash,
p.bank
from
orders o
left join
(select
pymt_date,
sum(amount) pymt,
sum(cash) cash,
sum(bank) bank
from
payments
group by
pymt_date) p
on
o.order_date = p.pymt_date
where
o.order_date = '2022/10/25'
but I am not getting any results.
- link for the sample table
I have a table name as Customer. In that table, there are several attributes like customer_id, customer_name, address, country, bill_to_customer, account_start_date, etc...So I want to get the billing customer name, address, country, from that table. In here, customer_id may be equal to bill_to_customer or customer_id not equal to bill_to_customer_id. When I want to get the billing customer details, what it does is, go to bill_to_customer value and filter the data of customer_id which matches with the bill_to_customer and give the output. How to write a SQL query to do this?
you can do a self join on table Customer with your bill_to_customer column.
Something like this:
SELECT billing.name, billing.address, billing.country FROM Customer cust
INNER JOIN Customer billing ON cust.bill_to_customer = billing.customer_id
WHERE ...further conditions...
Say I have two tables. A customers table and a orders table.
The customers table has a list of anyone that has ever placed an order, while the orders table has a list of every order ever made. These tables are connected on customer.ID = orders.CustomerID. Each customer has a unique ID.
The orders table also gives the Date of the order. I would like to delete all customer rows that have not ordered given a certain date.
The query I have so far is as follows:
DELETE * FROM customers join orders on customers.id = orders.CustomerID WHERE orders.Date <= 'Date Input form Form';
However, I fear that customers who have ordered both before and after the specified date will be removed as well, and I don't want that to happen.
The logic I'm looking for is more so like this:
delete customers where the orders.date is !> than the given date
Even then, will this logic protect customers who have ordered before the given date and after the given date?
What can I do to achieve this?
Could you perhaps propose a query similar to this?
DELETE FROM customers WHERE customer.id NOT IN
(SELECT order.customer_id from orders where order.date > cutoff)
Where customers must have ordered something after cutoff to be kept.
I am trying to calculate in a SQL statement. I am trying to calculate the total amount in the invoice.total column per customer. I created the following statement:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id;
When I run this, I get the total amount in the table. I have 15 different customers in this table but I get only the name of the first customer and the total amount of all customers. What am I doing wrong?
First, you need to Group the data when you want to have aggregate results:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id
GROUP BY customers.firstname, customers.lastname, customers.status;
Second, are you sure you are joining the table by correct fields? Is invoice.id correct column? I would expect invoice.id to be primary key for the table and instead I would expect another column for foreign key, invoice.customerid for example. Please double check it is correct.
UPDATE: As it was mentioned in comments, if you have two customers with same first name, last name and status, the data will be grouped incorrectly. In that case you need to add unique field (e.g. customers.id) to SELECT and GROUP BY statements.
You have to add a group by customer (id property for example). If you want to have first and last name in select then you will have to group by them as well.
Use group by clause in your query or you can use common table expression to get the sum of records by customer and then make a inner join with customer table.
You need to add a Group By with column names in your query.
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