Stock Quantity view in mysql inventory database -
here database design. plan on calculating quantity of components in view subtracting order_linequantity component_usedquantity. problem having surely elementary, current view doesn't work because summing order_linequantities components , likewise component_usedquantities instead of giving proper difference/quantity each individual component.
create view inventory3(componentid, quantity) select distinct(componentid) componentid, sum(order_linequantity) - sum(component_usedquantity) quantity component inner join order_line on order_linecomponentid = componentid inner join component_used on component_usedcomponentid = componentid how can proper quantity each individually different componentid?
if use sum() function sum values of table. if want sum related values, have first group values id , sum them. that's below sub query does.
here model of table1- order_line
here model of table2- component_used 
i have filled necessary details. can see there multiple values of order_linequantity given component.
and multiple values exist component_usedquantity given component.
i have linked using componentid component table.
select componentid componentid,(ordertable.sumorder-componenttable.sumcomponent) quantity component, (select order_linecomponentid id,sum(order_linequantity) sumorder order_line group order_linecomponentid)ordertable, (select component_usedcomponentid id,sum(component_usedquantity) sumcomponent component_used group component_usedcomponentid)componenttable componentid=ordertable.id , componentid=componenttable.id now when execute query, selects required details after grouping operations.



Comments
Post a Comment