Forum Discussion

Marco's avatar
Marco
Contributor II
3 months ago

currency names of the FX rates or the connection between the FX rates

Hi Everyone. I am running an SQL query, but I am now encountering an issue with currency conversions. The only thing my table contains is the column with the currency name, but it is not very helpf...
  • sameburn's avatar
    sameburn
    3 months ago

    Hi Marco 

    Since this information does not exist in a table.  The only option available other than your CASE statement would be to get creative and do something with your SQL in a Business Rule instead.  For example, if you move your logic to a Business Rule e.g. Dashboard DataSet.  You have a bit more flexibility with how you can reference things e.g. using a dictionary.  Below is an untested example of how you can do this. 

    Here we create a placeholder NVARCHAR column called Currency in our SQL query.  We then loop through the DataTable result and use a Dictionary (this time the ID is the Key and the Name is the Value) to lookup the Name based on the SourceCurrencyId and populate the Currency column with the corresponding Currency Name

    This should give you same result, without that big CASE statement. 

    	' Get Currency Dictionary
    	Dim currencyDict As Dictionary(Of Integer, String) = Currency.GetItems().ToDictionary(Function(kvp) kvp.Id, Function(kvp) kvp.Name)
    	
    	' Declare SQL	
    	Dim sql As String = "WITH CTE
    						 AS (
    						 SELECT 
    						  fxt.[UniqueID]
    						 ,fxt.Name
    						 ,fx.SourceCurrencyId
    						 ,'Unknown' Currency
    						 ,fx.Amount
    						 FROM [FxRateType] fxt
    						 LEFT JOIN [FxRate] fx
    							ON fxt.UniqueID = fx.FxRateTypeID
    						 WHERE                 
    						 (
    		                    Month(GETDATE()) = 1
    		                    AND Name LIKE '%' + CAST((Year(GETDATE()) -1) AS NVARCHAR(4)) + 'M2%'
    		                 )
    		                 OR (
    		                    Month(GETDATE()) > 1
    		                    AND Name LIKE '%' + CAST(Year(GETDATE()) AS NVARCHAR(4)) + 'M2%'
    		                 )
    						    )
    						 SELECT SourceCurrencyId, Currency, Amount FROM CTE"
    	
    	'Execute Query on App DB
    	Dim dt As New DataTable()
    	Using dbConnApp As DBConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)
    	    dt = BRApi.Database.ExecuteSql(dbConnApp, sql.ToString(), True)
            If Not dt Is Nothing Then
    			' prepare placeholder column
    			dt.Columns("Currency").ReadOnly = False
    			dt.Columns("Currency").MaxLength = -1
    			' Loop on rows
                For Each dr As DataRow In dt.Rows
                    'Process rows
    				dr.BeginEdit
    					' Lookup Currency Name from Dictionary by passing in SourceCurrencyId DataRow and assign value to Currency column
                    	dr("Currency") = currencyDict.Item(dr("SourceCurrencyId"))
    				dr.EndEdit
                Next
            End If
    	End Using