mysql - Getting cost and hours for each order -
i having following 6 tables :
employee
pk: employeeid; fk: empbranch references branch; fk: empsupervisor references employee
customer
pk: customerid
orders
pk: ordernumber fk: customerid references customer; fk: salesperson references employee
product
pk: productcode
prodline
pk: ordernumber + prodcode fk: ordernumber references orders; fk: prodcode references product
instline
pk: ordernumber + insttype fk: ordernumber references orders; fk: insttype refesnrences installation
i making project , got stuck in between. can me how list out each order ordernumber, orderdate, employeeid of salesperson, total amount products, , total amount installation. total amount products sum of unit price times quantity of products ordered. total amount installation sum of number of hours times billing rate of installation types. trying make query since 2 hours.please help
solution above part mentioned in answer
select o.ordernumber, orderdate, salesperson, sum (price * quantity) totalproductamount, sum (hours * rate) totalinstallcost orders o join prodline pl on pl. ordernumber = o. ordernumber join product p on p.productcode = pl. productcode join instline il on il. ordernumber = o. ordernumber join installation on i.installtype = il.insttype group o. ordernumber, orderdate, salesperson;
edit : assuming total amount of order given sum of totalproductamount , totalinstallcost. revenue_generated branch sum of total amount of orders salespersons working @ branch. need figure out branch number, branch name, revenue_target, , revenue_generated each branch fails meet revenue_target.
we can assume branch table follow :
select o.ordernumber, orddate, salesperson, sum(price * quantity) totalproductamount, sum(hours * billingrate) totalinstallationamount orders o join prodline pl on pl.ordernumber = o.ordernumber join product p on p.productcode = pl.prodcode join instline il on il.ordernumber = o.ordernumber join installation on i.installationtype = il.insttype group o.ordernumber, orddate, salesperson
edit: answer second question.
select branchnumber, branchname, revenuetarget, sum((price * quantity) + (hours * billingrate)) revenueattained branch b join employee e on e.empbranch = b.branchnumber join orders o on o.salesperson = e.employeeid join prodline pl on pl.ordernumber = o.ordernumber join product p on p.productcode = pl.prodcode join instline il on il.ordernumber = o.ordernumber join installation on i.installationtype = il.insttype group branchnumber, branchname, revenuetarget
Comments
Post a Comment