Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9760

Re: Eliminating duplicate entry

$
0
0

Hi Steve,

 

Previous query will work correct for below example,

 

if you have more Issue for production than "Receipt from production" then you will get correct answer.

for example you have 4 Issue and 3 Receipt from production than you will get correct answer.

 

Try the below query ,

This query will give correct answer until More Receipt from production than Issue for production.

 

But am not able to write a query for both, so based on your scenario you use any one of the query.

 

 

 

WITH receipt AS (

        SELECT distinct owor.ItemCode, oign.DocNum, ign1.BaseRef, SUM(ign1.LineTotal) AS total_receipt,

        ROW_NUMBER() OVER (partition by owor.docentry

ORDER BY owor.docentry) as row,owor.docentry

        FROM ign1 inner join OWOR on owor.DocEntry= IGN1.BaseEntry and ign1.ItemCode=owor.ItemCode

        inner join OIGN on oign.DocEntry=ign1.DocEntry and ign1.BaseType = '202'

        GROUP BY owor.ItemCode, ign1.baseref, oign.DocNum,oign.docentry,owor.DocEntry

     ),

     issued AS (

        SELECT owor.DocEntry,

        ROW_NUMBER() OVER (partition by owor.docentry

ORDER BY owor.docentry ) as iss_row,

          OIGe.DocNum, ige1.BaseRef, sum(Isnull(ige1.linetotal,0)) as total_issue

        FROM ige1 inner join OWOR on owor.DocEntry= ige1.BaseEntry and IGE1.BaseType = '202'

        inner join WOR1 on wor1.DocEntry = owor.DocEntry inner join OIGE on oige.DocEntry= ige1.DocEntry

                group by ige1.BaseRef, oige.DocNum,oige.docentry,owor.DocEntry

     )

  

select

TBL2.DocNum,TBL2.[FG-Code],TBL2.[Issued Num],Isnull(TBL2.[Issued Amt],0) [Issued Amt],TBL2.[Receipt Num],TBL2.[Receipt Amt]

  from (

Select (CASE rnum when 1 then ItemCode  end) [FG-Code],

(CASE rnum when 1 then Isnull(DocNum,0)  end) [DocNum],

(CASE ISrnum when 1 then Isnull(Issued_No,0)  end) [Issued Num],

(CASE ISrnum when 1 then Isnull(issuedamount,0)  end) [Issued Amt],

(CASE RSrnum when 1 then Isnull(Receipt_No,0)  end) [Receipt Num],

(CASE RSrnum when 1 then Isnull(receiptamount,0)  end) [Receipt Amt],

(forshort) [forshort]

 

 

from (    

SELECT owor.docentry,ROW_NUMBER() OVER (partition by owor.docentry order by owor.docentry) [rnum] ,

owor.docentry [forshort],

ROW_NUMBER() OVER (partition by issued.DocNum order by issued.DocNum) [ISrnum],

ROW_NUMBER() OVER (partition by receipt.DocNum order by receipt.DocNum) [RSrnum],

owor.ItemCode, owor.DocNum, issued.DocNum as Issued_No,

       max(Isnull(issued.total_issue,0)) AS issuedamount,

       receipt.DocNum as Receipt_No,

       max(receipt.total_receipt) AS receiptamount

FROM wor1 inner join owor on owor.DocEntry = wor1.docentry

left outer join receipt

on receipt.DocEntry = owor.DocEntry

left outer join issued

on issued.DocEntry = owor.DocEntry and Isnull(issued.iss_row,0) = Isnull(receipt.row,0)

where owor.DocNum = 154

GROUP BY owor.docentry,owor.ItemCode, owor.DocNum, issued.DocNum, receipt.DocNum,receipt.row ,issued.iss_row  ) TBL1 ) TBL2

group by

TBL2.DocNum,TBL2.[FG-Code],TBL2.[Issued Amt],TBL2.[Issued Num],TBL2.[Receipt Num],TBL2.[Receipt Amt]

 

 

order by

Max(TBL2.forshort) , TBL2.DocNum desc

 


Viewing all articles
Browse latest Browse all 9760

Trending Articles