android - SUM query in multiple table with group by sqlite database -
hello friends have 3 tables property_master , rent_master,expense_master , fields below
property_master --> p_id, p_name
rent_master --> r_id,p_id,r_amount
expense_master --> e_id,p_id,e_amount
i want total sum of r_amount, , e_amount single query query below
select p.p_id "product code", p.p_name "description", sum(case when ri.r_amount null 0 else ri.r_amount end) "quantity" , sum(case when d.e_amount null 0 else d.e_amount end) "dquantity" property_master p left join rent_master ri on ri.p_id = p.p_id left join expense_master d on d.p_id = p.p_id group p.p_id order sum(ri.r_amount) desc, sum(d.e_amount) desc
when run above code give right value r_amount e_amount give double value idea how can solve this?
when there 2 different rent_master
rows same p_id
values, 2 joined rows each matching expense_master
row.
you have compute sums independent subqueries:
select p_id "product code", p_name "description", (select sum(r_amount) rent_master p_id = property_master.p_id ) "quantity", (select sum(e_amount) expense_master p_id = property_master.p_id ) "dquantity" property_master order quantity desc, dquantity desc
Comments
Post a Comment