HI Nagarajan,
Find the Query and assist me where the case condition i need to add. And find the attachment of screenshot of report preview. you can find the duplicates in marked. because the last field "TIN" it is a UDF added in CRD1 (BP addresses). it is picking 3 address TIN no. into the report.
But i need the TIN no. only when i raised Invoice to that particular pay to address.
Check the UDF in green colour (which i added)
Query:
Set @Query1='Select DISTINCT ''Invoice'' ''Type'',PVT0.DocEntry,PVT0.NumAtCard ''Vendor Ref No'', convert(varchar(10),PVT0.TaxDate,103) TaxDate
,DocNum[Invoice No.],qty [Invoice Qty.], convert(varchar(10),DocDate,103) [Invoice Date],
PONo[Purchase Order No.],PODate[Purchase Order Date],u_esugamno[ESugam No.],
WorkType,case when Docstatus=''O'' Then ''OPEN'' else ''CLOSE'' end[DocStatus]
,CardCode,CardName,CSTNo,TANNo,TINNo,U_TIN,ShipToAddr[Ship To Address],PayToAddr[Pay To Address]
,[Total Before Discount]--,[Discount]
,(CASE WHEN FREIGHT>=0 THEN FREIGHT end) [FREIGHT],(CASE WHEN FREIGHT<0 THEN FREIGHT end) [DISCOUNT ALLOWED],RoundDif [Rounding]
,'+@cols2+','+@cols+'
,AcctName[GL Account Name],Amount1[GL Account Value], DocTotal from
(Select A.DocEntry,A.NumAtCard, convert(varchar(10),A.TaxDate,103) TaxDate,A.DocNum, convert(varchar(10),A.DocDate,103) DocDate,
(Case when (Select Top 1 X.BaseType From PCH1 X Where X.DocEntry=A.DocEntry)=''20''
then (select top 1 OPOR.DocNum from OPOR inner join POR1 on opor.DocEntry=por1.DocEntry inner join PDN1 on pdn1.BaseEntry=opor.docentry and PDN1.DocEntry =(Select Top 1 X.BaseEntry From PCH1 X Where X.DocEntry=A.DocEntry) GROup by OPOR.DocNum) when (Select Top 1 X.BaseType From PCH1 X Where X.DocEntry=A.DocEntry)=''22''
then (select top 1 OPOR.DocNum from OPOR inner join POR1 on opor.DocEntry=por1.DocEntry inner join PCH1 on pch1.BaseEntry=OPOR.DocEntry and PCH1.DocEntry=a.DocEntry) else '''' end) PONo,
(Case when (Select Top 1 X.BaseType From PCH1 X Where X.DocEntry=A.DocEntry)=''20''
then (select top 1 convert(varchar(10),OPOR.Docdate,103) from OPOR inner join POR1 on opor.DocEntry=por1.DocEntry inner join PDN1 on pdn1.BaseEntry=opor.docentry and PDN1.DocEntry =(Select Top 1 X.BaseEntry From PCH1 X Where X.DocEntry=A.DocEntry) GROup by OPOR.Docdate) when (Select Top 1 X.BaseType From PCH1 X Where X.DocEntry=A.DocEntry)=''22''
then (select top 1 convert(varchar(10),OPOR.Docdate,103) from OPOR inner join POR1 on opor.DocEntry=por1.DocEntry inner join PCH1 on pch1.BaseEntry=OPOR.DocEntry and PCH1.DocEntry=a.DocEntry) else '''' end) PODate,
A.u_esugamno,
A.U_wtype[WorkType],A.Docstatus,A.CardCode,A.CardName,C.TaxId1 CSTNo,C.TaxId6 TANNo,C.TaxID11 TINNo,T2.U_TIN,A.DocTotal,B.StaCode,B.TaxSum,A.Address2 ShipToAddr,A.Address PayToAddr
,TotalExpns [Freight],A.DiscSum [Discount],(A.DOCTOTAL-A.TOTALEXPNS-A.VATSUM+A.DISCSUM) [Total Before Discount],A.RoundDif
from OPCH A
INNER JOIN
(Select T1.DocEntry,T1.StaCode,SUM(T1.TaxSum) TaxSum from OPCH T0
INNER JOIN PCH4 T1 ON T0.DocEntry=T1.DocEntry
Where T0.DocType = ''I'' ANd T0.DocDate>='''+ @fdate + ''' and T0.DocDate<='''+ @tdate + ''' and T0.U_WType ='''+@wtype+'''
Group By T1.DocEntry,T1.StaCode) B ON A.DocEntry=B.DocEntry
Left Outer Join CRD7 C ON C.CardCode=A.CardCode and ISNULL(C.Address,'''')=''''
INNER JOIN CRD1 T2 ON A.CardCode=T2.CardCode
Where A.DocDate >='''+ @fdate + ''' and A.DocDate<='''+ @tdate + ''' and A.U_WType ='''+@wtype+'''
) P
PIVOT
(SUM(TaxSum) FOR StaCode IN ('+@cols+'))
AS PVT0'
Set @Query2='
INNER JOIN
(Select DocEntry,'+@cols1+',AcctName,Amount1,qty from
(Select DISTINCT A.DocEntry,A.NumAtCard, convert(varchar(10),A.TaxDate,103) TaxDate,A.DocNum, convert(varchar(10),A.DocDate,103) DocDate
,A.U_wtype[WorkType],A.Docstatus,A.CardCode,A.CardName,B.StaCode,B.BaseSum,bb.AcctName,bb.amount1,qty from OPCH A
inner join pch1 bbb on bbb.docentry=a.docentry
inner join
(select pch1.docentry, sum(pch1.Quantity)Qty from pch1
group by pch1.docentry)D on D.docentry=A.docentry
INNER JOIN
(Select T1.DocEntry,T1.StaCode,SUM(T1.BaseSum) BaseSum from OPCH T0
INNER JOIN PCH4 T1 ON T0.DocEntry=T1.DocEntry
Where T0.DocType = ''I'' ANd T0.DocDate>='''+ @fdate + ''' and T0.DocDate<='''+ @tdate + ''' and T0.U_WType ='''+@wtype+''' '
Set @Query3='
Group By T1.DocEntry,T1.StaCode) B ON A.DocEntry=B.DocEntry
left join (select aa.GRPOEntry ,aa.AcctName,sum(aa.Amount)Amount1 from (
select distinct ''GRPO''[Type],''''[InvNo],h.DocEntry[GRPOEntry] ,e.TransId,g.Segment_0 ,g.AcctName,f.debit[Amount] from OJDT e
join JDT1 f on e.TransId=f.TransId join OACT g on f.Account=g.AcctCode join OPDN h on h.TransId =e.TransId join PDN1 i on i.DocEntry=h.DocEntry
where g.segment_0 in (''4110101'',''4110102'',''4110103'',''4110104'',''4110105'',''4110106'',''4110107'',''4110108'',''4110109'',''4110110'',''4110112'')
and h.ObjType in (''20'') and i.TargetType=''18''
union all
select distinct ''Invoice''[Type],h.docentry[InvNo],i.baseentry[GRPOEntry],e.TransId ,g.Segment_0 ,g.AcctName,f.debit[Amount] from OJDT e
join JDT1 f on e.TransId=f.TransId join OACT g on f.Account=g.AcctCode join OPCH h on h.TransId =e.TransId join PCH1 i on i.DocEntry=h.DocEntry
where g.segment_0 in (''4110101'',''4110102'',''4110103'',''4110104'',''4110105'',''4110106'',''4110107'',''4110108'',''4110109'',''4110110'',''4110112'')
and h.ObjType in (''18'') and i.BaseType =''20'' and h.DocDate>='''+ @fdate + ''' and h.DocDate<='''+ @tdate + '''
)AA
group by aa.AcctName ,aa.GRPOEntry
) BB on bb.GRPOEntry = bbb.BaseEntry
Where A.DocDate >='''+ @fdate + ''' and A.DocDate<='''+ @tdate + ''' and A.U_WType ='''+@wtype+'''
) P
PIVOT
(SUM(BaseSum) FOR StaCode IN ('+@cols+'))
AS PVT) PVT1 ON PVT0.DocEntry=PVT1.DocEntry
Note: This query is only a bit of the STORED PROCEDURE.
Help me out in this.
Thanks & Remarks,
Ravi.