Hi,
Try this:
declare @Cust as nvarchar(20)
Declare @FromDate Datetime
Declare @ToDate Datetime
declare @Group as nvarchar(20)
set @cust = (select max(t0.cardcode) from OINV t0 where t0.cardcode = '[%0]')
Set @FromDate = (Select min(t0.Docdate) from dbo.OINV t0 where t0.Docdate >='[%1]')
Set @ToDate = (Select max(t0.Docdate) from dbo.OINV t0 where t0.Docdate <='[%2]')
set @group = (select max(t2.groupname) from OCRG t2 where t2.groupname = '[%3]')
SELECT [CustName] as CustName, [C] as Country, [S] as State, [G] as CustGroup,[1] as Jan,[2] as Feb,[3] as Mar,[4] as April,[5] as May,[6] as june,[7] as July,[8] as Aug,[9] as Sept,[10] as Oct,[11] as Nov,[12] as Dec
from
(SELECT T0.[CardName] as CustName,T1.[Country] as C, T1.[State1] as S,sum(T0.[DocTotal]) as Total, datepart(mm,T0.[DocDate]) as month, t2.groupname as G FROM OINV T0 left JOIN OCRD T1 ON T0.CardCode = T1.CardCode left JOIN OCRG T2 ON T1.GroupCode = T2.GroupCode WHERE T0.[CardCode] = @Cust and T0.[DocStatus] ='o' and T0.[DocDate] between @fromdate and @todate and t2.groupname = @group GROUP BY T0.[CardName],T0.[DocDate],t2.groupname,T1.[Country] , T1.[State1]
union all
(SELECT T0.[CardName] as CustName, T1.[Country] as C, T1.[State1] as S, -sum(T0.[DocTotal]) as Total, datepart(mm,T0.[DocDate]) as month,t2.groupname as G from ORIN T0 left JOIN OCRD T1 ON T0.CardCode = T1.CardCode left JOIN OCRG T2 ON T1.GroupCode = T2.GroupCode WHERE T0.[CardCode] = @Cust and T0.[DocStatus] ='o' and T0.[DocDate] between @fromdate and @todate and t2.groupname = @group GROUP BY T0.[CardName],T0.[DocDate],t2.groupname,T1.[Country] , T1.[State1] )) S
Pivot
(sum(S.total) For month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) P
Hope helpful.
Thanks & Regards,
Nagarajan