Skip to Content
author's profile photo Former Member
Former Member

SAP query returning duplicate records

Hi all,

I'll try to elaborate as much as possible so here goes:

I have a query that I am trying to run in SAP B1 8.8 which is not returning the true set of results I am expecting.

Essentially I am trying to filter by product based on delivery notes. I have the query as follows -

SELECT count(*), sum(T0.[DocTotal]-T0.[VatSum]) as "Order Value"
FROM ODLN T0  INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
WHERE DateName(mm,T0.DocDate) = 'November'
and DateName(yyyy,T0.DocDate) = '2011'
and (T1.[GroupCode] = '102' or T1.[GroupCode] = '107') 

This returns the correct result of 1320 row numbers and the given sum.

When I try to break this query down further by product using the query below, the values are doubled, tripled, quadrupled etc and summed up to give a number that is extremely high.

The code is as follows:

SELECT distinct count (*), sum(distinct T0.[DocTotal]-T0.[VatSum]) as "Order Value" from DLN1 T1
inner join ODLN T0 on T0.DocEntry = T1.DocEntry
Inner join OITM T2 on T2.ItemCode = T1.ItemCode 
inner join OCRD T3 on T0.CardCode = T3.CardCode 
WHERE DateName(mm,T0.DocDate) = 'November'
and DateName(yyyy,T0.DocDate) = '2011'
and (T3.[GroupCode] = '102' or T3.[GroupCode] = '107')
and (T2.[ItmsGrpCod] = '108' or T2.[ItmsGrpCod] = '112' or T2.[ItmsGrpCod] = '115')

This also returns 1819 rows where there should be less based on the product.

Please let me know if I am not explaining myself well or need further clarification.

Thanks!

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 17, 2011 at 02:00 PM

    Hi,

    Try:

    SELECT count (*) Count, sum(T1.LineTotal) as "Order Value" from DLN1 T1

    inner join ODLN T0 on T0.DocEntry = T1.DocEntry

    Inner join OITM T2 on T2.ItemCode = T1.ItemCode and T2.ItmsGrpCod in ('108','112','115')

    inner join OCRD T3 on T0.CardCode = T3.CardCode and T3.GroupCode in ('102','107')

    WHERE DateDiff(mm,T0.DocDate,GetDate()) = 0

    and DateDiff(yy,T0.DocDate,GetDate()) = 0

    Thanks,

    Gordon

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 17, 2011 at 02:30 AM

    hi,

    try to run this code from your SQL management studio

    SELECT     SUM(T0.DocTotal - T0.VatSum) AS [Order Value], T0.DocTotal - T0.VatSum AS [Order Value], T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName, T0.DocTotal, 
                          T1.ItemCode, T1.Dscription, T1.Quantity, T2.GroupCode, T3.ItmsGrpCod
    FROM         ODLN AS T0 INNER JOIN
                          DLN1 AS T1 ON T0.DocEntry = T1.DocEntry INNER JOIN
                          OCRD AS T2 ON T0.CardCode = T2.CardCode INNER JOIN
                          OITM AS T3 ON T1.ItemCode = T3.ItemCode
    GROUP BY T0.DocTotal - T0.VatSum, T0.DocNum, T0.DocDate, T0.CardCode, T0.CardName, T0.DocTotal, T1.ItemCode, T1.Dscription, T1.Quantity, T2.GroupCode, 
                          T3.ItmsGrpCod
    HAVING      (T2.GroupCode = 102 OR
                          T2.GroupCode = 107) AND (T3.ItmsGrpCod = 108 OR
                          T3.ItmsGrpCod = 112 OR
                          T3.ItmsGrpCod = 115) AND (T0.DocDate BETWEEN CONVERT(DATETIME, '2011-11-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-11-17 00:00:00', 102))

    regards,

    Fidel

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.