sum two different table and the subtract its total - php

i have this problem with me on how to sum total value from two different table and then after getting its total i want to subtract it. for example i have table "rsales" and "sales" and i have these ff vlue below.
data from "rsales"
id | total | pcode |
1 | 100 | 2143 |
2 | 100 | 2143 |
3 | 50 | 2222 |
4 | 50 | 2222 |
data from "sales"
id | total | pcode |
7 | 100 | 2143 |
8 | 50 | 2222 |
my problem is this. i want to sum all "total" values from sales and sum "total"value from rsales group by pcode.and then after getting its sum i want to subtract it. my page must be something like this.
total pcode
| 100 | 2143 |
| 50 | 2222 |
i have this ff code but it doesnt wor for me
sql "select sum(rsales.total)- sum(sales.total) as t1 where pcode = rsales.pcode"

Use:
SELECT
SUM(r.total)-(
SELECT SUM(s.total)
FROM sales AS s WHERE r.pcode=s.pcode
) as total,
r.pcode
FROM rsales AS r
GROUP BY r.pcode;
Output:
+--+--+--+--+--+-
| total | pcode |
+--+--+--+--+--+-
| 100 | 2143 |
| 50 | 2222 |
+--+--+--+--+--+-
2 rows in set

Have you tried something like this?
SELECT
SUM(L.total) as lTotal, SUM(R.total) as rTotal
FROM
sales L
INNER JOIN rsales R
ON
R.pcode = L.pcode
GROUP BY L.pcode
If you get expected values from both tables you can easily add Additions and Subtruction in FROM clause.

There's no joins needed to do this. This solution works if some pcodes are only in one table:
select SUM(total), pcode from (
select sum(total) as total, pcode from rsales group by pcode
union all
select SUM(-total) as total, pcode from sales group by pcode) salesTables
group by pcode

Related

Merge multiple row then left join-php mysql

I have two tables: invoices and sales. These are simplified version of them:
Invoices:
InvoiceNo | ProductNo | QtyIn
-----------------------------------
F01 | 00001 | 20
F01 | 00002 | 50
F01 | 00003 | 15
F02 | 00002 | 10
Sales:
Date | ProductNo | QtyOut
---------------------------------
3/2/17 | 00002 | 12
3/3/17 | 00002 | 8
3/4/17 | 00003 | 10
What I'm trying to do is to make a stock summary table, which looks like this:
ProductNo | QtyIn | QtyOut | Stock
-------------------------------------------
00001 | 20 | 0 | 20
00002 | 60 | 20 | 40
00003 | 15 | 10 | 5
To make that stock table, as far as I can think is:
Make another database for invoice where same product will be merge
and it's quantity will be sum up.
Make something like the first one for sales.
Then make a join table for both of them.
By this way, I will end up having 5 tables. Is there any simpler way to do this? Thanks.
If you apply a filer on Invoices and Sales (e.g. for a date range) either filtered result could have products not referenced in the other. Hence a single outer join does not cover all possible conditions. As there is no "full outer join" in MySQL a simple method to overcome this is to union all source rows then group that:
select ProductNo, sum(QtyIn) QtyIn, sum(QtyOut) QtyOut, sum(QtyIn) - sum(QtyOut) Stock
from (
select ProductNo, QtyIn, 0 as QtyOut
from invoices
# where datecol >= `2017-01-01`
union all
select ProductNo, 0 as QtyIn, QtyOut
from Sales
# where datecol >= `2017-01-01`
) u
group by ProductNo
Mysql Query is
Select a.ProductNo , a.QtyIn, b.QtyOut, a.QtyIn-b.QtyOut as "Stock" from Invoices a join Sales b on a.productNo=q.productNo

Select distinct and random rows from one table that match a value from another table

This topic has been much discussed but I was unable to find a solution that I can modify and make it work for my case. So maybe a more advanced expert will be able to help out.
I have a table called keywords which contains about 3000 rows with distinct keywords. Against each keyword there is a matching product_id, which are NOT unique, i.e. some of them are repeated. Table looks something like this:
+---------+------------+
| keyword | product_id |
+---------+------------+
| apple1 | 15 |
| apple2 | 15 |
| pear | 205 |
| cherry | 307 |
| melon | 5023 |
+---------+------------+
I have a second table called inventory that contains about 500K of products each with it's own product ID and other product data.
Now I need to get one random product row from inventory table that matches each product_id from keywords table and insert those rows into another table.
Resulting table should be something like this:
+---------+------------+---------+---------+---------+
| keyword | product_id | product | data1 | data2 |
+---------+------------+---------+---------+---------+
| apple1 | 15 | app5 | d1 | d2 |
| apple2 | 15 | app1 | d1 | d2 |
| pear | 205 | pear53 | d1 | d2 |
| cherry | 307 | cher74 | d1 | d2 |
| melon | 5023 | melo2 | d1 | d2 |
+---------+------------+---------+---------+---------+
This is my query at the moment and the problem is how to get a random product from inventory that matches a product_id:
SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords LEFT OUTER JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND();
If you want it to only return rows when there is a match between the tables, then you want a regular (i.e. inner) join not a left outer join. You can also add the word distinct.
SELECT DISTINCT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND();
And if you only want 1 row returned, add limit 1 at the end.
SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND() LIMIT 1;
Is this what you want?
SELECT *
FROM (
SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND()
) tmp
GROUP BY tmp.keyword;
I also test it at http://sqlfiddle.com/#!2/e559a9/2/0. Just run some times, the result will be randomize.

update all values from different table that has same id

I have two tables named sales and rsales
I have this ff value for table sales
id | pcode | total | discount |
2 | 33 | 100 | 20 |
3 | 33 | 100 | 20 |
and i have this ff value for table rsales
id | pcode | total | discount | sales_id |
4 | 33 | 100 | 20 | 1 |
5 | 33 | 100 | 20 | 2 |
6 | 33 | 100 | 20 | 3 |
My problem is that when I update all values from table sales, the table rsales must be update either if sales.id is equal to sales_id.
so for example if I have updated table sales with id = 2 and 3, the sales_id 2 and 3 from rsales must be updated either.
take note : only 2 and 3 because only that ids is found in table sales ids.
I have this ff codes so far to update table rsales. but the output is shown not as what I meant. it update all values.
mysql_query("UPDATE sales AS t1, rsales AS t2
SET t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE t1.pcode = '$pcode'");
You should add to the WHERE class the common column between the tables.
UPDATE sales AS t1, rsales AS t2
SET
t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE
t1.id = t2.sales_id AND
t1.pcode = '$pcode'
Note: I haven't tested it.

subtract from two different table

i have 3 tables tables "productlist, sales, return" so let say for example i have 3 sales and 2 return as given below.
this is the ff data from productlist
id | pcode | pname | pdesc |
1 | 222 | 33uf | 10v |
this is the ff data from sales
id | pcode | total | profit
1 | 222 | 200 | 10
2 | 222 | 100 | 10
3 | 222 | 200 | 10
this is the ff data from return
id | pcode | total | lose
3 | 222 | 200 | 10
4 | 222 | 100 | 10
My problem is this. I want to select data from productlist and sum the "total" and "profit" Value from sales and sum the "total" and "lose" value from return. And then subtracting my two table to get the result. The expected result must be something like this.
id | pcode | pname | pdesc | total | profit |
1 | 222 | 33uf | 10v | 200 | 10 |
I have this ff code but I can't subtract "total" from sales to "total" from return and "profit" from sales and "lose" from return.
$result = mysql_query("SELECT
productlist.*,
SUM(sales.total)-SUM(return.total) as total,
SUM(sales.profit)-SUM(return.lose) as profit
FROM productlist
LEFT JOIN sales ON sales.pcode = productlist.pcode AND return ON return.pcode = productlist.pcode
GROUP BY pcode
ORDER BY total ASC");
You seem to be trying to join two tables with AND, that's not quite right ;)
Try this:
...
LEFT JOIN `sales` USING (`pcode`)
LEFT JOIN `return` USING (`pcode`)
...
I'm not completely certain this'll work, it may complain of column `pcode` is ambiguous. If this happens, try this instead:
...
LEFT JOIN `sales` ON `sales`.`pcode` = `productlist`.`pcode`
LEFT JOIN `return` ON `return`.`pcode` = `productlist`.`pcode`
...
The structure of your query is not going to return the right results. Not matter how you fix the syntax, you will still be getting a cartesian product between the sales and returns for a given product.
One fix is to do aggregations before the joins:
SELECT pl.*,
(coalesce(s.total, 0) - coalesce(r.total, 0)) as total,
(coalesce(s.profit, 0) - coalesce(r.lose, 0)) as profit
FROM productlist pl left join
(select pcode, sum(total) as total, sum(profit) as profit
from sales
group by pcode
)
on s.pcode = pl.pcode left join
(select pcode, sum(total) as total
from return
group by pcode
) r
on r.pcode = pl.pcode
ORDER BY total ASC;

Selecting 3 Different Tables

I have 3 table named rsales, rreturn, productlist. Table rsales and rreturn has total column. My aim is, I want to sum all total values from rsales and total value from rreturn table and select specific value only from productlist table.
Let say, for example, I have this data from productlist table:
id | pcode | pname | pdesc |
1 | 222 | 33uf | 10v |
data from rsales table:
id | total | pcode |
1 | 200 | 222 |
2 | 200 | 222 |
data from rreturn table:
id | total | pcode |
1 | 50 | 222 |
2 | 20 | 222 |
the output must be something like this:
id | pcode | pname | pdesc | total
1 | 222 | 33uf | 10v | 470
My question is this: I want to sum all total values from rsales and rreturn table and select all values from productlist. I have this following code below and runs very well. But it can only sum total value from rsales table, or rather, it can only sum value from one single table.
$result = mysql_query("SELECT
productlist.*,
SUM(rsales.total) as total,
SUM(rsales.vatable_sales) as vatable_sales,
SUM(rsales.vats) as vats,
SUM(rsales.discount) as discount
FROM productlist
LEFT JOIN rsales ON rsales.pcode = productlist.pcode
GROUP BY pcode
ORDER BY total ASC");
You are not very explicit about what doesn't work for you. Are you getting results, but the numbers are not what you are expecting? Well, say you have three sales and two returns, then you get six records by joining all tables. Thus you double your sales and triple your returns.
You can join aggregations instead, for one solution:
$result = mysql_query("SELECT
productlist.*,
sumsales.sum_total + sumreturns.sum_total as total,
sumsales.sum_vatable_sales as vatable_sales,
sumsales.sum_vats as vats,
sumsales.sum_discount as discount
FROM productlist
LEFT JOIN
(
SELECT pcode, SUM(total) as sum_total, SUM(vatable_sales) as sum_vatable_sales, SUM(vats) as sum_vats, SUM(discount) as sum_discount
FROM rsales
GROUP BY pcode
) AS sumsales ON sumsales.pcode = productlist.pcode
LEFT JOIN
(
SELECT pcode, SUM(total) as sum_total
FROM rreturn
GROUP BY pcode
) AS sumreturns ON sumreturns.pcode = productlist.pcode
ORDER BY total ASC");

Categories