how to mysql join for two tables - mysql

I have two mysql tables:
table-1: advance
id advance_id vamount
1 101 10000
2 101 20000
3 101 10000
table-2: purchase
id advance_id item amount
1 101 Shirt 500
2 101 Pant 700
3 101 Pen 500
4 101 Shoe 1000
What I want to do, sum of vamount against each advance_id from table-1 and purchase information from table-2 against same advance_id. While trying by the following query:
SELECT sum(a.vamount) as vamount, p.item, p.amount FROM advance as a INNER JOIN purchase as p ON a.advance_id=p.advance_id WHERE p.advance_id=101
I want to fetch the result in this way:
Total Advance Amount: 40000
item amount balance
shirt 500 39500
pant 700 38800
pen 500 38300
shoe 1000 37300
but it is returning one one row in result set.
How to do it in right way?

Try:
SELECT p.item, sum(a.vamount) as vamount, p.amount FROM advance as a INNER JOIN purchase as p ON a.advance_id=p.advance_id WHERE p.advance_id=101
group by p.item

You query should like this
SELECT sum(a.vamount) as vamount
, p.item
, p.amount
FROM advance as a
INNER JOIN purchase as p ON a.advance_id = p.advance_id
WHERE p.advance_id=101
GROUP BY p.item
, p.amount
Use GROUP BY clause and optional you can remove WHERE clause for group all items

Related

name and sum from 2 different tables

I have 2 tables.
table customer have. id , name , age
table order have . id, customer_id , order_amount , order date.
I want to show all name from customer table and sum of order amount from order table according to customer.
customer_id
Name
age
1
Alice
24
2
Bob
52
3
Carol
45
4
Dave
51
order_id
customer_id
order_amount
order_date
1
2
50
2012-4-5
2
1
27
2012-8-1
3
2
12
2013-5-20
4
4
25
2014-1-25
5
4
30
2014-5-30
6
1
20
2014-6-22
EDIT
I tried this but it gives me only bob and sum of all columns instead of separate sum of customers
SELECT customers.name, SUM(orders.order_amount) FROM `orders` INNER JOIN customers WHERE orders.customer_id = customers.customer_id;
Joining condition must be on ON clause, not in WHERE.
You must specify for what group the sum must be calculated.
SELECT customers.name, SUM(orders.order_amount)
FROM `orders`
INNER JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customers.name;

SUM, GROUP BY AND LEFT JOIN in query

I've two tables:
`orders`
order_id order_office_id order_invoice_id
1 1 1
2 2 2
3 2 2
4 2 3
5 1 4
`invoices`
inv_id inv_order_id inv_amount
1 1 500.00
2 0 320.00
3 3 740.00
4 4 160.00
With this query:
SELECT SUM(inv_amount) matrah, order_office_id
FROM `invoices`
LEFT JOIN orders ON order_invoice_id = inv_id OR inv_order_id = order_id
WHERE order_id IS NOT NULL
GROUP BY order_office_id
It is multiplying some amounts.
What I want to get sum of amounts by office:
office_id sum
1 660.00
2 1060.00
How to get proper amounts by single query?
This does what you want, I think
select sum(inv_amount) as matrah,
(select order_office_id from orders where order_invoice_id = inv_id limit 1) as office
from invoices
group by office;
I've removed the OR, because you were getting two office ids for one of the orders, so it was ambiguous. I've included a subquery to make sure that only one office is applied to each order.
This query may help you out:
SELECT SUM(i.inv_amount) matrah, o.order_office_id
FROM `invoices` AS i
LEFT JOIN `orders` AS o
ON o.order_invoice_id = i.inv_id
GROUP BY order_office_id
OR statement in your ON condition caused the problem.

How to get right sum from inner join in mysql

I have two tables one is orders and second is order_product in which I have to find out orders count, product count, totalamount in corresponding to store using store id from which I have successfully find out the orders count and product count but my totalamount is not coming correct.
orders:
...........................
order_id or_total_amt
...........................
1 10
2 10
3 10
order_product
.................................
op_id op_order_id st_id
.................................
1 1 1
2 2 2
3 3 1
4 3 1
I want below output but my totalamount value is coming wrong it is coming 30,but the correct value is 20 which i have mentioned in the right output below.
output which i want:
.........................................
st_id orders product totalmount
.........................................
1 2 3 20
2 1 1 10
I have tried the below query which is giving 30 value of totalamount which is wrong.
SELECT `op_st_id`,count(distinct orders.`order_id`)as orders,count(order_product.op_pr_id) as product
,sum(orders.or_total_amt) as totalamount from orders
inner JOIN order_product on orders.order_id=order_product.op_order_id
group by `op_st_id`
SELECT
`st_id`,
count(DISTINCT orders.`order_id`) AS orders,
count(order_product.op_id) AS product,
count(DISTINCT orders.`order_id`)*(sum(orders.or_total_amt)/count(order_product.op_id)) AS totalamount
FROM
orders
INNER JOIN order_product ON orders.order_id = order_product.op_order_id
GROUP BY
`st_id`

SQL - Groups rows that share a value in different columns

I have a table that has the following fields
SalesOrderID RelatedOrderID Amount
1 0 5.00
2 1 1.00
3 0 3.00
4 0 20.00
5 4 10.00
I'm looking to write a query that will return the sales order total made up of the original total and the RelatedOrderID total.
SalesOrderID NewAmount
1 6.00
4 30.00
Hope that makes sense...please ask any questions. I'm aware it's a confusing situation!
Thanks,
Mike
You could use union all and then aggregate::
select SalesOrderID, sum(amount) as amount
from ((select SalesOrderID, amount from t) union all
(select RelatedSalesOrderID, amount from t where RelatedSalesOrderID > 0)
) tt
group by SalesOrderID;
For that effect you will have to use something like this
SELECT
least(o.SalesOrderId,r.SalesOrderId) AS SalesOrderId,
o.Amount + r.Amount AS Amount
FROM
orders o
JOIN orders r ON o.SalesOrderId = r.RelatedOrderID;
Thanks both, I've actually worked it out since posting this:
SELECT so1.SalesOrderID,
cast(SUM(so1.Amount+so2.Amount) as money) as NewAmount
FROM SalesOrder so1
JOIN SalesOrder so2
ON so1.SalesOrderID = so2.RelatedOrderID
GROUP BY so1.sorder_code

How to join 3 table with this condition?

I have 3 tables: product, customer and transaction.
Product:
id_product price
1 1000
2 2000
Customer:
id_customer name
1 Tom
2 Jack
Transaction:
id_transaction id_product id_customer qty date
1 1 1 10 2013-02-21
2 2 1 50 2013-02-21
3 1 2 15 2013-02-21
I want to achieve this result:
id_customer name total_transaction purchase_qty subtotal
1 Tom 2 60 110000
2 Jack 1 15 15000
How I can get that result using a query in MySQL?
SELECT t.id_customer, c.name,
COUNT(t.id_customer) AS total_transaction,
SUM(t.qty) as purchase_qty
FROM transaction t
INNER JOIN customer c
ON t.id_customer = c.id_customer
GROUP BY t.id_customer,c.name
SELECT cs.id_customer, cs.name, COUNT(tr.transaction) AS total_transaction, SUM(tr.qty) as purchase_qty, SUM(tr.qty*pr.prize)
FROM customer AS cs
LEFT JOIN transaction AS tr ON tr.id_customer = cs.id_customer
LEFT JOIN product AS pr ON pr.id_product = tr.id_product
GROUP BY cs.id_customer
I suppose you are a total beginner, so I did this for you. Next time, if you have ANY own ideas, give to us, so we don't have to write the whole query for you. Show some effort.
SELECT c.id_customer, c.name, count(t.id_transaction) AS total_transaction
FORM Customer c INNER JOIN Transaction T
ON C.id_customer = T.id_customer
GROUP BY c.id_customer
You need a group by statement since you want to aggregate results for a given customer :
SELECT id_customer, name, count(id_transaction) AS total_transaction
FORM Customer, Transaction
WHERE Transaction.id_customer = Customer.id_customer
GROUP BY id_customer
This does not solve your whole problem, but you've got the idea.