While running this query...
Select distinct
CR.CustomerID ,
I.Current_Identified_Impairment_Raised As OpeningStock_at_CustomerLevel--,
--,SUM(I.Closing_Stock) AS ClosingStock_at_CustomerLevel--,
--SUM(I.Movement_Amount) As Movement_Amount_at_CustomerLevel,
--SUM(I.Impairment_CurrentWriteoff) As WriteOff_at_CustomerLevel
From tblCCRS_relations CR
Inner JOIN dbo.tbl_element_credit_applications CRA
On CRA.cra_id_pkey = CR.CreditApplicationID
Inner JOIN dbo.tbl_relations R
ON R.rel_tgt_id_fkey = CRA.cra_elmt_id_fkey
Inner Join
(
Select C.cust_id_pkey As cust_id_pkey , F.Fac_id_Pkey As Fac_id_Pkey, F.fac_elmt_id_fkey
From tbl_element_customers C
Inner Join tbl_element_facilities F
on C.cust_id_pkey = F.fac_customer_id_fkey
Where F.fac_subfacility_id_fkey is null --and C.cust_id_pkey =25577
)CF
ON CR.CustomerID = CF.cust_id_pkey
And R.rel_src_id_fkey = CF.fac_elmt_id_fkey
Inner Join tblCCRS_Impairments I
On I.FacilityID = CF.fac_id_pkey
Where R.rel_src_type = 4
And R.rel_tgt_type = 3
And CRA.cra_islast = 1
And CR.CustomerID in (25577)
---Group By CR.CustomerID
OUT PUT:-
CustomerID OpeningStock_at_CustomerLevel
-------------------------------------------------------------------------
25577 64579.682662214
25577 135554.082514537
But when i do sum it will double the value like
Select distinct
CR.CustomerID ,
sum(I.Current_Identified_Impairment_Raised) As OpeningStock_at_CustomerLevel--,
--,SUM(I.Closing_Stock) AS ClosingStock_at_CustomerLevel--,
--SUM(I.Movement_Amount) As Movement_Amount_at_CustomerLevel,
--SUM(I.Impairment_CurrentWriteoff) As WriteOff_at_CustomerLevel
From tblCCRS_relations CR
Inner JOIN dbo.tbl_element_credit_applications CRA
On CRA.cra_id_pkey = CR.CreditApplicationID
Inner JOIN dbo.tbl_relations R
ON R.rel_tgt_id_fkey = CRA.cra_elmt_id_fkey
Inner Join
(
Select C.cust_id_pkey As cust_id_pkey , F.Fac_id_Pkey As Fac_id_Pkey, F.fac_elmt_id_fkey
From tbl_element_customers C
Inner Join tbl_element_facilities F
on C.cust_id_pkey = F.fac_customer_id_fkey
Where F.fac_subfacility_id_fkey is null --and C.cust_id_pkey =25577
)CF
ON CR.CustomerID = CF.cust_id_pkey
And R.rel_src_id_fkey = CF.fac_elmt_id_fkey
Inner Join tblCCRS_Impairments I
On I.FacilityID = CF.fac_id_pkey
Where R.rel_src_type = 4
And R.rel_tgt_type = 3
And CRA.cra_islast = 1
And CR.CustomerID in (25577)
Group By CR.CustomerID
OUT PUT:-
CustomerID OpeningStock_at_CustomerLevel
-------------------------------------------------------------------------
25577 400267.54
Please suggest why this happening.any idea