MySQL to calculate SUM with foreign key relation - mysql

I have two tables 1.material_line_item and 2.item_master there is a foreign key relation between two tables (item_master_id present in material_line_item) and there is a column called item_code in item_master. So I want a Join Query to display the item_code with my current query.

I would approach this by joining the item_master table to a subquery of material_line_item which calculates the aggregates you want for each item master id value. I am selecting all columns available though you are free to choose whichever columns you want.
SELECT t1.*, t2.*
FROM item_master t1
INNER JOIN
(
SELECT item_master_id,
SUM(received_quantity) AS Total_Received_Qty,
SUM(ordered_quantity) AS Total_Ordered_Qty
FROM material_line_item
GROUP BY item_master_id
) t2
ON t1.id = t2.item_master_id

you can simply join the two tables like
select item.item_master_id, master.item_id, Sum(received_quantity),
sum(ordered_quantity) from material_line_item item
left join item_master master on item.item_master_id = master.id
group by item.item_master_id, master.item_id

Try this,
select a.item_master_id,a.Total_received_qty,a.Total_ordered_Qty,b.item_code from (select item_master_id,
sum(received_quantity) Total_received_qty,
sum(ordered_quantity) Total_ordered_Qty
from material_line_item
group by item_master_id) a, item_master b where a.item_master_id =b.item_master_id
Hope it will help.

Related

How to Join two tables and sum their columns across in Mysql based on distinct column

I am joining two tables by the regNo column. I want to add the points of Table1.points and Table2.points where the regNo matches and just incase it doesn't match I also want it be included with its points in the list as shown in the image bellow
I have read through the existing problems but not finding the solution to this e.g How can I sum columns across multiple tables in MySQL?
(
SELECT `Ex`.regNo,(`In`.`points`+`Ex`.`points`) AS 'Points'
FROM Table1`In`
LEFT JOIN Table2`Ex` ON `In`.`regNo` = `In`.`regNo`
)
UNION
(
SELECT`Ex`.regNo,(`In`.`points`+`Ex`.`points`) AS 'Points'
FROM Table1`In`
RIGHT JOIN Table2`Ex` ON `In`.`regNo` = `In`.`regNo`
);
I want it to give the list arranged as per unique (DISTINCT) regNo
You need UNION followed by GRoUP BY:
SELECT regNo, SUM(points) AS total
FROM (
SELECT regNo, points
FROM Table1
UNION ALL
SELECT regNo, points
FROM Table2
) AS u
GROUP BY regNo
You are looking for a FULL JOIN between both tables.
SELECT
COALESCE(t1.id, t2.id) id
COALESCE(t1.regNo, t2.regNo) regNo
COALESCE(t1.points, 0) + COALESCE(t2.points 0) points
FROM
table1 t1
FULL JOIN table2 t2 on t1.regNo = t2.regNo
NB : you did not specify what you expect to be done to generate the new id so by default the above query will display the table1.id if available, else table2.id.
If you would better generate a new, auto-incremented field, then :
SET #i=0;
SELECT
#i:=#i+1 id
COALESCE(t1.regNo, t2.regNo) regNo
COALESCE(t1.points, 0) + COALESCE(t2.points 0) points
FROM
table1 t1
FULL JOIN table2 t2 on t1.regNo = t2.regNo
Please check this. You need to use full outer join and null replacement before aggregation
select
COALESCE(table1.regno, table2.regno) regno,
sum(COALESCE(table1.points,0)) + sum(COALESCE(table2.points,0)) points
from Table1
full outer join Table2
on table1.regno = table2.regno
group by
COALESCE(table1.regno, table2.regno)

How to join two tables with grouping in MySQL?

I have two tables,Table 1 and Table 2,Accid is the key to join two tables,
i want to sum revenueact and revenuutilz based on year and account, so out will look like this
in reality more data is there,when i join two tables and group by year only first account is coming,can anyone please help me on this?
You could try this:
SELECT
Accname,
YEAR,
SUM(revenueact) AS Revac,
SUM(revenuutilz) AS Revut
FROM table1 a
INNER JOIN Table2 b
ON a.Accid = b.Accid
GROUP BY Accname,Year
You cound use a join adn a group by
select t2.accname, sum(t1.revenueact), sum(t1.revenuutiliz), t1.year
from table1 t1
inner join table2 t2 on t1.accid = t2.accid
group by t2.accname, t1.year

How do I select two tables in 1 query?

I want to select two tables in one query but it doesn't seem to work. I have tried nested select but I just got sql_error:
Subquery returns more than 1 row
Here is my query:
SELECT `client`.`id` as client_id,
(SELECT `org`.`name` FROM `org`) as organization
FROM `client`
What is the better way to query two tables?
Here is my expected result:
client_id = [1,2,3,4,5]
organization = [x,y,z]
This will work but it isn't what you want i think.
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org`, `client`
This will give you a cross product of all rows in org with all rows in client. Maybe you want something like this:
SELECT `client`.`id` as client_id, `org`.`name` as organization FROM `org` JOIN `client` ON `client`.`memberOf` = `org`.`id`
The JOIN will connect rows of both tables where column memberOf in table client is equal to column id in table org
You should use join queries to join two or more tables. you can visit https://dev.mysql.com/doc/refman/5.0/en/join.html
Example of the join query:
SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;

Testing for the absence of child records in MySQL

I have two database tables with a one-to-many relationship. Is it possible to select records from the 'one' table for which no records exist in the 'many' table using a single SQL statement? If so, how?
Thanks in anticipation.
Use an OUTER JOIN:
select p.id
from parent p
left outer join child c on c.parent_id = p.id
where c.parent_id is null
select *
from table1
where not exists (select null
from table2
where MatchingColumn = Table1.MatchingColumn)
You can pull all the records from the master table that do not have any children in the "many" table like so:
SELECT T.*
FROM Table1 T
WHERE T.Id NOT IN(SELECT DISTINCT FKID FROM Table2)
FKID is the Foreign Key ID that links back to the master table.

sql left join with distinct correct?

I'm trying to do a join between tables 1 and 2 which have a 1 to many relationship.
table1 has the following fields
createdate
contact
tkey (surrogate key)
table2 has the following fields
tkey (primary key)
status
userfld1
description
I want to show all items in table2 with their corresponding items in table1 grouped by table2.userfld1
select distinct t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
from table2 as t2 left join table1 as t1
on t2.tkey = t1.tkey
group by t2.userfld1
is this correct?
No that's not correct, you can't select columns that aren't in the group by unless they are contained in an aggregate function. And I think what you are asking for doesn't even make sense. My best guess is that you mean ORDER BY, not GROUP BY:
SELECT DISTINCT t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
FROM table2 t2
LEFT JOIN table1 t1
ON t2.tkey = t1.tkey
ORDER BY t2.userfld1
Three other errors that I've fixed:
SELECT ... FROM not SELECT ... WHERE
You should join with a table, not a column.
You had no aliases after the table names, but later refer to these missing aliases.
I think what you're looking for is order by, not group by, and I also fixed your query:
select t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
where table2 t2 left join table1 t1
on t2.tkey = t1.tkey
order by t2.userfld1
Is this what you were looking for?