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");
Related
I am trying to calculate the req_qty and in_qty from table1 and table2.
I am trying to fetch the joined 3 tables data SUM(req_qty), SUM(in_qty). Issue come if req.qty not save in table2 it will not works. I am left joining the table by this sku's ids
I made here a very simple table for calculating the sum of the column. generally, the sum of the column is multiplying the total rows.
Please don't close the post and guild for other thread. for my doubt clear i made here a table which I can understand. please help.
table1
ID | sku_1 | req_qty | trans_id
----------------------------------
1 | 123 | 150 | 12345
2 | 142 | 200 | 256314
3 | 123 | 100 | 896523
table2
ID | sku_2 | in_qty | trans_key
-----------------------------------
1 | 142 | 50 | 002563
table3
ID | sku_code | sku_name
--------------------------
1 | 142 | ABC
2 | 123 | XYZ
Expected Output
ID | sku | sku_name | reqQty | inQty
------------------------------------
1 | 123 | XYZ | 250 | 0
2 | 142 | ABC | 200 | 50
Edit to select data when table1 and table2 are empty
SELECT table1.id, table3.sku_code as sku, table3.sku_name,
sum(table1.req_qty) as reqQty, sum(table2.in_qty) as inQty
FROM table3
LEFT JOIN table1 on table3.sku_code = table1.sku_1
LEFT JOIN table2 on table3.sku_code = table2.sku_2
GROUP BY table1.id, table3.sku_code, table3.sku_name
Explanation
You can see an explanation on how left join works here https://www.w3schools.com/sql/sql_join_left.asp#:~:text=The%20LEFT%20JOIN%20keyword%20returns,if%20there%20is%20no%20match.
But to explain this query quickly, we will select all data from table3, left join will find all records from left table (here table3) and mathcing records from right tables (here table2 and table 1).
I have three table.First table shows input data, second table shows output data and third is total table where we shows our product total balance.
1.input table
Date | product_name | in_qty
5/7/19 | A | 10
5/7/19 | B | 15
6/7/19 | A | 10
6/7/19 | C | 20
2.output table
Date | product_name | out_qty
7/7/19 | A | 10
8/7/19 | B | 10
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
There my problem is in input table product C is not show in the total table. Which product add in the output table only those product total shown in the total table.
$res=mysqli_query($con, "SELECT i.product_name, i.in_qty, o.product_name,o.out_qty
FROM input i, output o
WHERE i.product_name= o.product_name");
while($row=mysqli_fetch_array($res))
{
$in_name = $row['product_name'];
$inqty = $row['Input_qty'];;
$out_name = $row['product_name'];
$outqty = $row['Out_qty'];
$sql=mysqli_query($con, "INSERT INTO total (product_name,Input_qty,Out_qty)
VALUES('$in_name','$inqty','$outqty')");
$sql2=mysqli_query($con, "UPDATE total t2
INNER JOIN (
SELECT product_name, SUM(in_qty) as qty_total
FROM input
GROUP BY product_name
) t1 ON t2.product_name= t1.product_name
SET t2.in_qty = t1.qty_total");
}
My expectation is :
3.total balance table
product_name | in_qty | out_qty | total_qty
A | 20 | 10 | 10
B | 15 | 10 | 5
C | 20 | | 20
your mysql query is wrong.
use following query .
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
in your case:
SELECT input.product_name , input.in_qty , output.out_qty
FROM input
LEFT [OUTER] JOIN output
ON input.column = output.column;
I have data in a row with same value and id example below:
table_name: test
id | amount
1 | 100
1 | 100
1 | 100
2 | 150
2 | 150
2 | 150
3 | 200
3 | 200
3 | 200
4 | 250
4 | 250
4 | 250
I want to sum only one row in each idusing sql below is not working it sum all the row.
"select *, sum(amount) as total from test group by id";
my question is that possible to sum() only one row each id?
desired output?(edit)
id | amount
1 | 100
2 | 150
3 | 200
4 | 250
total : 700
my question is that possible to sum() only one row each id?
I interpret this as your wanting one value, with a single row from each group. One method is two levels of aggregation:
select sum(amount)
from (select id, max(amount) as amount
from test
group by id
) t;
Looks like you need this
select *,sum(amount) from (select distinct * from test) as t group by id
Try:
select id, sum(amount)/count(*) as total
from test
group by id
Result:
| id | total |
|----|-------|
| 1 | 100 |
| 2 | 150 |
| 3 | 200 |
| 4 | 250 |
try this using subquery-
select sum(amount) from (select distinct id,amount from company7) as tempTable
Try this
create table #test
(id int, amount int)
insert into #test values (1,100),(1,100),(1,100),(2,150),(2,150),(3,200),(3,250)
;with cte
as
(
select sum(distinct amount) as damount,id
from #test
group by id
)
select sum(damount) as total from cte
For other DBMSs than SQL Server
select sum(damount) as total from (select sum(distinct amount) as damount,id
from test
group by id) as it
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
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;