SQL - Groups rows that share a value in different columns - mysql

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

Related

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.

SQL Sum Calculation Confusion

I am new with mysql and working to change a store application to make it have two stock. I created a table to store stock quantity:
Then I plan to create a view with stock quantity, per store, per SKU. I using the following query:
SELECT
`stockList`.`sku`,
SUM(A.`stockQty`) AS 'store1',
SUM(B.`stockQty`) AS 'store2',
SUM(`stockList`.`stockQty`) AS 'total'
FROM `stockList`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=1
) AS A
ON `stockList`.`sku`=A.`sku`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=2
) AS B
ON `stockList`.`sku`=B.`sku`
GROUP BY `stockList`.`sku`
Per resulting table, calculation is not proper and I could not identify the logic:
SKU 43 should show for store1 = 9 and for store2 = 10, total = 19. This is what they show if I execute the select queries alone. Please, let me know if I misunderstood how this sum logic works.
You might to use SUM on subquery to calculate Totle price by sku
LEFT JOIN may make some fields not match causing NULL so use IFNULL to preset value 0
You can try this.
SELECT
T.sku,
SUM(T.stockQty) as totle,
IFNULL(A.`store1`,0) AS `store1`,
IFNULL(B.`store2`,0) AS `store2`
FROM `stockList` AS T
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store1`
FROM `stockList`
WHERE `idStock`=1
GROUP BY sku
) as A ON A.sku = T.sku
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store2`
FROM `stockList`
WHERE `idStock`=2
GROUP BY sku
) AS B ON T.sku =B.sku
GROUP BY T.sku
sqlfiddle
Your query is much more complicated than it needs to be. You can just do this:
SELECT
sku,
SUM(stockQty) as total,
SUM(IF(idStock=1,stockQty,0)) AS `store1`,
SUM(IF(idStock=2,stockQty,0)) AS `store2`
FROM `stockList`
GROUP BY sku
Output:
sku total store1 store2
36 10 10 0
37 3 3 0
38 4 4 0
39 3 3 0
40 10 10 0
41 12 12 0
42 12 12 0
43 19 9 10

mysql: two table join with sum

I'm attempting to join two tables and also get a SUM and flailing badly. I need to get the total commission amounts for each affiliate where affiliate.approved=1 AND order.status=3.
//affiliate table
affiliate_id | firstname | lastname | approved |
1 joe shmoe 1
2 frank dimag 0
3 bob roosky 1
here's the order table
//order
affiliate_id | order_status_id | commission
1 3 0.20
1 0 0.30
2 3 0.10
3 3 0.25
1 3 0.25
2 3 0.15
2 0 0.20
and here's what I'd like the query to return:
affiliate_id | commission
1 0.45
3 0.25
Here is my attempt that doesn't work. It outputs just one line.
SELECT order.affiliate_id, SUM(order.commission) AS total, affiliate.firstname, affiliate.lastname FROM `order`, `affiliate` WHERE order.order_status_id=3 AND affiliate.approved=1 AND order.affiliate_id = affiliate.affiliate_id ORDER BY total;
thanks for any help.
You've missed GROUP BY, try this:
SELECT
`order`.affiliate_id,
SUM(`order`.commission) AS total,
affiliate.firstname,
affiliate.lastname
FROM `order`
JOIN `affiliate`
ON `order`.order_status_id = 3 AND affiliate.approved = 1 AND `order`.affiliate_id = affiliate.affiliate_id
GROUP BY `order`.affiliate_id
ORDER BY total;
Demo Here
You can try this Query for your solution :-
SELECT order.affiliate_id, SUM(order.commission) AS total,affiliate.firstname,
affiliate.lastname
FROM `order`, `affiliate`
WHERE order.order_status_id=3
AND affiliate.approved=1
AND order.affiliate_id = affiliate.affiliate_id
GROUP BY order.affiliate_id
ORDER BY total;
Here is the solution:
select affiliate.affiliate_id,sum(`order`.commission) as total from affiliate left join `order` on affiliate.affiliate_id=`order`.affiliate_id
where affiliate.approved=1 and `order`.order_status_id=3 group by affiliate.affiliate_id
In addition,"order" is a key word of SQL , I recommend you not to use it as a table/column name.
First: Remove the implicit join syntax. It's confusing.
Second: You needed to group by affiliate_id. Using aggregate function without group by collapses your result set into a single row.
Here's the query using INNER JOIN:
SELECT
`order`.affiliate_id,
SUM(`order`.commission) AS total,
affiliate.firstname,
affiliate.lastname
FROM `order`
INNER JOIN`affiliate` ON `order`.affiliate_id = affiliate.affiliate_id
WHERE `order`.order_status_id = 3
AND affiliate.approved = 1
GROUP BY affiliate.affiliate_id
ORDER BY total;
WORKING DEMO
Caution: You have picked one of the reserved words of MySQL as table name (order). Be aware to enclose it with (`)backtick always .
Just a gentle reminder

how to mysql join for two tables

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

HAVING clause and performance

I've got a performance problem with a SQL (MySql) query. Basically I have a table similar to this:
ID PRICE ID OBJECT
-----------------------------
1 500.00 1 1
2 300.00 1 1
3 400.00 1 1
4 100.00 1 1
5 100.00 1 1
6 100.00 2 3
And I need to get the maximum amount of lines given an amount.
For example, given the amount 1000.00 the query must returns these ids (order by price asc) and the total price.
ID PRICE TOTAL_PRICE
---------------------------------
4 100 100.00
5 100 200.00
2 300 500.00
3 400 900.00
Atm I'm using a query similar to the below one:
set #total=0;
select a.id, a.price , #total:=#total + a.price as total_price , a.id_user
from shares a
where a.`id_user` != 0 and a.id_object = 1
having #total < 1000.00 order by a.price asc;
It works fine but it's not efficient. It takes around 1.5 seconds to extract the data (the table has around 1M lines).
The problem is related to the Having clause.
Do you have any suggestions?
Is there a way to perform this type of query without using the clause Having ?
I believe this (cumulative sum) is what you are looking for:
set #total = 0;
SELECT ID,
Price,
Total_Price
FROM (
SELECT a.*
,(#total := #total + price) AS total_price
FROM shares a
WHERE a.id_user != 0
AND a.id_object = 1
ORDER BY a.price
) q1
WHERE q1.total_price < 1000;
SQL Fiddle Demo