web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics GP (Archived)

adding some logic to an SQL join of history

(0) ShareShare
ReportReport
Posted on by 955

HI I have a union SQL that join the 101-102 and 302-303 SOP tables. This works fine except there are 2 quirks that have to be accounted for. I would like to account for this in the SQL rather than the reporting level.

1. If an item appears 2 times on the same SOP document it won't get counted
onto the report.

2. Averages are made if  items are purchased multiple times at differing
prices.

The SQ:

SELECT
IV00101.ITMGEDSC,
IV00101.ITEMDESC,
RM00101.CUSTNAME,
IV40600.UserCatLongDescr,
CATS.UserCatLongDescr,
SOP10200.ITEMNMBR,
SOP10200.SOPNUMBE,
SOP10200.QUANTITY,
SOP10200.OXTNDPRC,
SOP10200.SOPTYPE,
SOP10100.DOCDATE,
'Current' as source

FROM ((((PBS.dbo.SOP10200
SOP10200 INNER JOIN PBS.dbo.IV00101 IV00101 ON
SOP10200.ITEMNMBR=IV00101.ITEMNMBR)

INNER JOIN
PBS.dbo.SOP10100 SOP10100 ON
(SOP10200.SOPTYPE=SOP10100.SOPTYPE) AND
(SOP10200.SOPNUMBE=SOP10100.SOPNUMBE))

INNER JOIN
PBS.dbo.IV40600 IV40600 ON
IV00101.ITMGEDSC=IV40600.USCATVAL)

INNER JOIN
PBS.dbo.IV40600 CATS ON
IV00101.USCATVLS_2=CATS.USCATVAL)

INNER JOIN
PBS.dbo.RM00101 RM00101 ON
SOP10100.CUSTNMBR=RM00101.CUSTNMBR

UNION ALL

SELECT
IV00101.ITMGEDSC,
IV00101.ITEMDESC,
RM00101.CUSTNAME,
IV40600.UserCatLongDescr,
CATS.UserCatLongDescr,
SOP30300.ITEMNMBR,
SOP30300.SOPNUMBE,
SOP30300.QUANTITY,
SOP30300.OXTNDPRC,
SOP30300.SOPTYPE,
SOP30200.DOCDATE,
'History' as source

FROM ((((PBS.dbo.SOP30300
SOP30300 LEFT OUTER JOIN PBS.dbo.IV00101 IV00101 ON
SOP30300.ITEMNMBR=IV00101.ITEMNMBR)

INNER JOIN
PBS.dbo.SOP30200 SOP30200 ON
(SOP30300.SOPTYPE=SOP30200.SOPTYPE) AND
(SOP30300.SOPNUMBE=SOP30200.SOPNUMBE))

LEFT OUTER JOIN
PBS.dbo.IV40600 IV40600 ON
IV00101.ITMGEDSC=IV40600.USCATVAL)

LEFT OUTER JOIN
PBS.dbo.IV40600 CATS ON
IV00101.USCATVLS_2=CATS.USCATVAL)

INNER JOIN
PBS.dbo.RM00101 RM00101 ON
SOP30200.CUSTNMBR=RM00101.CUSTNMBR

*This post is locked for comments

I have the same question (0)
  • Victoria Yudin Profile Picture
    22,769 on at

    Frank,

    I would change all of your joins to LEFT OUTER JOIN except the ones to SOP10100 and SOP30200.  That will definitely take care of issue #1.  If it does not take care of issue #2, then you may need to explain that one a bit more - you're not doing any calculations here, so I can't see how anything is being averaged, maybe it just looks like way because you're not getting all of your data back?

  • Trancefrank Profile Picture
    955 on at

    Victoria, This is true, no calculations are being done, I would want to average only on that second condition. Perhaps it has to be done in the Crystal Report itself? I made the Left Outer join changes but the numbers are still off  slightly.

  • Victoria Yudin Profile Picture
    22,769 on at

    Frank,

    I guess I misunderstood, I thought you were saying some kind of averaging is happening, but you're looking to do averaging.  If it's easy to do in the Crystal Report, you might as well do it there.  If not, you'd have to change your query to accomplish that. You'll likely need to add GROUP BY's or do the averaging separately and join it in to the rest of the query.

    For the numbers that are off slightly, what specifically are you basing that on?  The total counts in the SOP10200 and SOP30300 tables?  If so, most likely that's due to orphaned records and the fact that you're using an INNER JOIN to SOP10100 and SOP30200.  For example: 

    SOP10200 INNER JOIN SOP10100 will exclude any records from SOP10200 that do not have a corresponding record in SOP10100.  That's good - if there is no header record, the detail records are probably not valid.  You can verify this by running the following:

    SELECT * FROM SOP10200 WHERE SOPNUMBE NOT IN (SELECT SOPNUMBE FROM SOP10100)

    The above should work unless you have different SOPTYPE's with the same numbering schemes.

    If that returns any results, running Check Links on the Sales Series will typically get rid of any orphaned records.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics GP (Archived)

#1
mtabor Profile Picture

mtabor 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans