Quantcast
Channel: Microsoft Dynamics GP
Viewing all articles
Browse latest Browse all 38722

Forum Post: T-SQL: MAX not working

$
0
0
Hello: If any of you have any knowledge of T-SQL, I sure would appreciate the help! :) Please start by reviewing the first set of T-SQL code that I show, below. The "MAX(DYNAMICS..MC00100.XCHGRATE) AS [DIRECT]" clause is producing incorrect results. The only reason that I placed "MAX" for that field, in the first place, is because I needed an aggregate clause or whatever to prevent duplicates. If something else gets me the correct data, I'd love to know it. Anyway, the two embedded pictures at the end show what I mean when I say "incorrect results". The first shows a direct exchange rate of 0.2723500. This is wrong. Per the second embedded picture, the direct exchange rate should be 0.2723200. The reason 0.2723200 is correct is because the DUBAI..GL20000.TRXDATE (POSTING_DATE) for the row shown in the first picture is between the EXCHDATE (exchange date) and the EXPNDATE (expiration date) shown in the second picture. (That exchange rate, also, meets the criteria in my where clause of the code.) The code for the second picture is the second set of code below. (It's the code that contains only one line.) So, I tried to fix the first code by adding a HAVING clause at the end, as shown in the third set of code below. But, using this created duplicates. Does anyone have any ideas, on how to get this "DIRECT" field to show correct data? Thanks! Much appreciated! John SELECT LEFT(DUBAI..GL00105.ACTNUMST, 2) AS [ENTITY], 'Company Name' AS [NAME], DUBAI..GL20000.OPENYEAR AS [YEAR], DUBAI..GL20000.JRNENTRY, DUBAI..GL20000.REFRENCE AS [REFERENCE], DUBAI..GL20000.DSCRIPTN AS [DIST_REFERENCE], DUBAI..GL20000.SERIES, DUBAI..GL20000.TRXDATE AS [POSTING_DATE], DUBAI..GL00105.ACTNUMST AS [ACCOUNT], DUBAI..GL20000.DEBITAMT AS [DEBIT], DUBAI..GL20000.CRDTAMNT AS [CREDIT], SUM((DUBAI..GL20000.DEBITAMT - DUBAI..GL20000.CRDTAMNT)) AS [TOTAL LOCAL], SUM(DUBAI..GL20000.DEBITAMT * DYNAMICS..MC00100.XCHGRATE) AS [USD_DEBIT], SUM(DUBAI..GL20000.CRDTAMNT * DYNAMICS..MC00100.XCHGRATE) AS [USD_CREDIT], SUM((DUBAI..GL20000.DEBITAMT * DYNAMICS..MC00100.XCHGRATE) - (DUBAI..GL20000.CRDTAMNT * DYNAMICS..MC00100.XCHGRATE)) AS [TOTAL USD], MAX(DYNAMICS..MC00100.XCHGRATE) AS [DIRECT], DUBAI..GL00100.USERDEF1 AS [BPC_ACCOUNT], DUBAI..GL00100.USERDEF2 AS [BPC_COST_CTR], DUBAI..GL00100.USRDEFS1 AS [BPC_PROFIT_CTR], DUBAI..GL00100.USRDEFS2 AS [BPC_TRADING_PTR], DUBAI..GL00100.ACTDESCR AS [DESCRIPTION], DUBAI..GL20000.ORPSTDDT AS [ORIG_POSTED_DATE], DUBAI..GL20000.ORMSTRNM AS [ORIG_MASTER_NAME], DUBAI..GL20000.CURNCYID AS [CURRENCY] FROM DUBAI..GL20000 INNER JOIN DUBAI..GL00105 ON DUBAI..GL20000.ACTINDX = DUBAI..GL00105.ACTINDX INNER JOIN DUBAI..GL00100 ON DUBAI..GL00105.ACTINDX = DUBAI..GL00100.ACTINDX LEFT OUTER JOIN DUBAI..MC00200 ON DUBAI..GL20000.ACTINDX = DUBAI..MC00200.ACTINDX LEFT OUTER JOIN DYNAMICS..MC00100 ON DUBAI..GL20000.CURNCYID = LEFT(DYNAMICS..MC00100.EXGTBLID, 3) WHERE DYNAMICS..MC00100.CURNCYID = 'Z-US$' AND DUBAI..GL00105.ACTNUMBR_1 = '20' AND DUBAI..MC00200.CurrencyTranslationType <> '0' or (DUBAI..GL00105.ACTNUMBR_1 = '20' AND DUBAI..MC00200.CurrencyTranslationType = '1' AND DYNAMICS..MC00100.EXGTBLID = '%_MR' AND month(DUBAI..GL20000.TRXDATE) = month(DYNAMICS..MC00100.EXCHDATE) AND year(DUBAI..GL20000.TRXDATE) = year(DYNAMICS..MC00100.EXCHDATE)) OR (DUBAI..GL00105.ACTNUMBR_1 = '20' AND DUBAI..MC00200.CurrencyTranslationType = '2' AND DYNAMICS..MC00100.EXGTBLID = '%_BS' AND month(DUBAI..GL20000.TRXDATE) = month(DYNAMICS..MC00100.EXCHDATE) AND year(DUBAI..GL20000.TRXDATE) = year(DYNAMICS..MC00100.EXCHDATE)) OR (DUBAI..GL00105.ACTNUMBR_1 = '20' AND DUBAI..MC00200.CurrencyTranslationType = '3' AND DYNAMICS..MC00100.EXGTBLID = '%_HIST' AND month(DUBAI..GL20000.TRXDATE) = month(DYNAMICS..MC00100.EXCHDATE) AND year(DUBAI..GL20000.TRXDATE) = year(DYNAMICS..MC00100.EXCHDATE)) GROUP BY LEFT(DUBAI..GL00105.ACTNUMST, 2) , DUBAI..GL20000.OPENYEAR , DUBAI..GL20000.JRNENTRY , DUBAI..GL20000.REFRENCE , DUBAI..GL20000.DSCRIPTN , DUBAI..GL20000.TRXDATE , DUBAI..GL00105.ACTNUMST , DUBAI..GL20000.DEBITAMT , DUBAI..GL20000.CRDTAMNT , DUBAI..GL00100.USERDEF1 , DUBAI..GL00100.USERDEF2 , DUBAI..GL00100.USRDEFS1 , DUBAI..GL00100.USRDEFS2 , DUBAI..GL00100.ACTDESCR , DUBAI..GL20000.ORPSTDDT , DUBAI..GL20000.ORMSTRNM , DUBAI..GL20000.CURNCYID , DUBAI..GL20000.SERIES select * from MC00100 where CURNCYID = 'Z-US$' and EXGTBLID LIKE 'AED%' ORDER BY EXPNDATE , DYNAMICS..MC00100.EXCHDATE , DYNAMICS..MC00100.EXPNDATE HAVING DUBAI..GL20000.TRXDATE >= DYNAMICS..MC00100.EXCHDATE AND DUBAI..GL20000.TRXDATE <= DYNAMICS..MC00100.EXPNDATE

Viewing all articles
Browse latest Browse all 38722

Trending Articles