Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two table 1)Purchase(Name- TBL_Pur_Invdet) 2)Sale(Name- TBL_Sale_Invdet)

i want to show the total Purchase Qty, sale Qty, Purchase Return, Sale Return in a single query.

I use the SQL server 2008 R2
pLEASE HELP ME......

What I have tried:

Main Query---
SQL
select   c.catnm,i.itemnm,i.opnstk,u.untnm,(i.ITMcgst*2) as StkGSTRate from tbl_item as i
INNER JOIN tblcat  AS c   ON c.catid=i.ITMcatid    
INNER JOIN tblsubcat  AS sc   ON sc.subcatid=i.ITMsubcatid 
INNER JOIN tblUnit  AS u   ON u.untid=i.ITMcatid   
INNER JOIN TBL_pur_invdet  AS pd   ON pd.ItemIDpur =i.itemid

Sub-query
select distinct ItemIDpur,sum(PDQty )As purchaseQty,p_type  FROM TBL_pur_invdet where p_type='P' group by ItemIDpur,p_type
union all
select distinct ItemIDsale,sum(sDQty )As SaleQty,S_type  FROM TBL_sale_invdet where S_type='S' group by ItemIDsale,S_type
UNION ALL
select distinct ItemIDpur,sum(PDQty ) As purchaseReturnQty,p_type  FROM TBL_pur_invdet where p_type='PR' group by ItemIDpur,p_type
UNION ALL
select distinct ItemIDsale,sum(sDQty )As SaleReturnQty,S_type  FROM TBL_sale_invdet where S_type='SR' group by ItemIDsale,S_type
Posted
Updated 27-Feb-18 0:41am
v2
Comments
Santosh kumar Pithani 27-Feb-18 4:23am    
please share Data records and expected output so its easy to give answer
Jayanta Modak 27-Feb-18 5:03am    
https://ibb.co/bYyEvx


How to add the sub query in main query
i write the sub query for checking it is work or not
yes it is work but wont to add the sub-query into main query means when i run the main query i see all field in result
Jayanta Modak 27-Feb-18 6:19am    
sorry! data record means table details ?
Suvendu Shekhar Giri 27-Feb-18 4:59am    
You haven't mentioned about the problem, this is just requirement.
Jayanta Modak 27-Feb-18 5:03am    
https://ibb.co/bYyEvx

How to add the sub query in main query
i write the sub query for checking it is work or not
yes it is work but wont to add the sub-query into main query means when i run the main query i see all field in result

1 solution

--i don't know relation but i have tried for OP 
select   c.catnm,i.itemnm,i.opnstk,u.untnm,(i.ITMcgst*2) as StkGSTRate,
(select sum(PDQty )As purchaseQty
      FROM TBL_pur_invdet T where p_type='P' AND 
             T.ItemIDpur=pd.ItemIDpur  group by ItemIDpur,p_type) AS purchasequantity,
(select sum(sDQty )As SaleQty 
     FROM TBL_sale_invdet T where S_type='S' AND 
             T.ItemIDsale=pd.ItemIDpur   group by ItemIDsale,S_type) AS salesQty,
(select sum(PDQty ) As purchaseReturnQty  
    FROM TBL_pur_invdet T  where p_type='PR' AND T.ItemIDpur=pd.ItemIDpur group by 
                                                ItemIDpur,p_type)AS PurchaseReturns,
(select sum(sDQty )As SaleReturnQty
    FROM TBL_sale_invdet T where S_type='SR' AND T.ItemIDsale=pd.ItemIDpur group by 
                                                 ItemIDsale,S_type) AS saleReturns
 from tbl_item as i
INNER JOIN tblcat  AS c   ON c.catid=i.ITMcatid    
INNER JOIN tblsubcat  AS sc   ON sc.subcatid=i.ITMsubcatid 
INNER JOIN tblUnit  AS u   ON u.untid=i.ITMcatid   
INNER JOIN TBL_pur_invdet  AS pd   ON pd.ItemIDpur =i.itemid
 
Share this answer
 
Comments
Jayanta Modak 27-Feb-18 6:54am    
thanks sir for help and reply,
here "p_type='P'" means it is purchase and i want to item wise purchase qty (for example i purchase 'abc' item five(5) time (2,4,5,7,9) then show purchase qty is 27) same as sale , purchase return and also Sale return column.
Santosh kumar Pithani 27-Feb-18 7:21am    
Execute above solution you will understood.
Jayanta Modak 27-Feb-18 10:36am    
hello sir
thanks
for reply it is work but sir why you use every where "T.ItemIDpur=pd.ItemIDpur for purchase T.ItemIDsale=pd.ItemIDpur for sale" "pd.ItemIDpur" this id is come from purchase details table you use when you calculate the sale quantity

and
i have 174 item abut after run the query 338 row
please help me .......
i don't know good English So, please forgive me and my query is for my knowledge update don't think others

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900