Skip to main content
Solved

CTE (Common Table Expression)


Is it possble to create a table based on CTE in timextender, or how would you do it? 

Best answer by Thomas Lind

Hi @aloz 

I missed that you added it twice I will remove the double.

Hi @aloz 

Yes, I got an old article from the old page where I use a cte query.

https://legacysupport.timextender.com/hc/en-us/articles/115005972746-Improve-execution-times-by-splitting-exchange-rates-out-on-dates

CREATE VIEW [DSA].[CurrencyExchangeRates] AS

WITH CTE AS
(SELECT 
	[CurrencyCode],
	[ExchangeRate],
	LAG([ToDate]) OVER (ORDER BY [CurrencyCode]) AS [FromDate],
	[ToDate]
FROM [ExchangeRates_V])

SELECT [CurrencyCode],
	[ExchangeRate],
	[DateValue] AS [Date] 
FROM CTE
LEFT JOIN [Date_V] AS D ON [FromDate] <= D.[DateValue] AND [ToDate] > D.[DateValue]

The issue is more likely with creating custom tables with #tempdataset

I would probably add that part to a stored procedure and then connect to it.

I got a example that does the same as above but in a better way.

CREATE PROCEDURE [DSA].[spExchangeRates]
@iCurrencyCode int = NULL,
@iMaxCurrencyCode int = NULL
AS
BEGIN
	CREATE TABLE #TempTable 
	(
		[CurrencyCode] int,
		[ExchangeRate] decimal(38,3),
		[FromDate] datetime,
		[ToDate] datetime
	)
	WHILE(@iCurrencyCode <= @iMaxCurrencyCode)
	BEGIN
		INSERT INTO #TempTable 
		(
			[CurrencyCode],
			[ExchangeRate],
			[FromDate],
			[ToDate]
		)
		SELECT [CurrencyCode],
			ISNULL([ExchangeRate],0) AS [ExchangeRate],
			ISNULL(LAG([ToDate]) OVER (ORDER BY [DW_Id]),DATEADD(yy, DATEDIFF(yy, 0, [ToDate]), 0)) AS [FromDate],
			[ToDate]
		FROM [ExchangeRates]
		WHERE [CurrencyCode] = @iCurrencyCode

		SET @iCurrencyCode = @iCurrencyCode + 1
	END
	SELECT * FROM #TempTable
	DROP TABLE #TempTable
END

To use the procedure I made a custom table insert.

DECLARE @1 as INT = (SELECT MIN([CurrencyCode]) FROM [ExchangeRates])
DECLARE @2 as INT = (SELECT MAX([CurrencyCode]) FROM [ExchangeRates])
DECLARE @Temp TABLE 
(
	[CurrencyCode] int,
	[ExchangeRate] decimal(38,3),
	[FromDate] datetime,
	[ToDate] datetime
)
INSERT @Temp EXEC [spExchangeRates]
@iCurrencyCode = @1,
@iMaxCurrencyCode = @2
SELECT [CurrencyCode],
	[ExchangeRate],
	[DateValue] AS [Date] 
FROM @Temp
LEFT JOIN [Date_V] AS D ON [FromDate] <= D.[DateValue] AND [ToDate] > D.[DateValue]

The declare @temp table is allowed and you could place it in a view as well.

View original
Did this topic help you find an answer to your question?

5 replies

devin.tiemens
TimeXtender Xpert
Forum|alt.badge.img+3
  • TimeXtender Xpert
  • 79 replies
  • August 28, 2024

Hi,

 

Yes this is possible via a View. After you created the view, if you want you can store the outcome in a table by dragging the view into your DSA or MDW.

 

Hope this helps.

 

Kind regards,

Devin


devin.tiemens
TimeXtender Xpert
Forum|alt.badge.img+3
  • TimeXtender Xpert
  • 79 replies
  • August 28, 2024

Maybe this link helps to create the view.

Christian Hauggaard

 

After this you only have to drag the view onto the DSA or MDW to save the outcome as a table.


daniel
TimeXtender Xpert
Forum|alt.badge.img+7
  • TimeXtender Xpert
  • 188 replies
  • August 28, 2024

Dear @aloz ,

Devin's view option is good. Or, you could also drag the table (again) to the layer you are working in. just keep the columns that you want and do the transformations and then do a lookup. You could also deselect the Enable physical valid table so it would act as a view. But now you have full traceability out of the box
 

 

= Daniel


Thomas Lind
Community Manager
Forum|alt.badge.img+5
  • Community Manager
  • 1017 replies
  • Answer
  • August 28, 2024

Hi @aloz 

I missed that you added it twice I will remove the double.

Hi @aloz 

Yes, I got an old article from the old page where I use a cte query.

https://legacysupport.timextender.com/hc/en-us/articles/115005972746-Improve-execution-times-by-splitting-exchange-rates-out-on-dates

CREATE VIEW [DSA].[CurrencyExchangeRates] AS

WITH CTE AS
(SELECT 
	[CurrencyCode],
	[ExchangeRate],
	LAG([ToDate]) OVER (ORDER BY [CurrencyCode]) AS [FromDate],
	[ToDate]
FROM [ExchangeRates_V])

SELECT [CurrencyCode],
	[ExchangeRate],
	[DateValue] AS [Date] 
FROM CTE
LEFT JOIN [Date_V] AS D ON [FromDate] <= D.[DateValue] AND [ToDate] > D.[DateValue]

The issue is more likely with creating custom tables with #tempdataset

I would probably add that part to a stored procedure and then connect to it.

I got a example that does the same as above but in a better way.

CREATE PROCEDURE [DSA].[spExchangeRates]
@iCurrencyCode int = NULL,
@iMaxCurrencyCode int = NULL
AS
BEGIN
	CREATE TABLE #TempTable 
	(
		[CurrencyCode] int,
		[ExchangeRate] decimal(38,3),
		[FromDate] datetime,
		[ToDate] datetime
	)
	WHILE(@iCurrencyCode <= @iMaxCurrencyCode)
	BEGIN
		INSERT INTO #TempTable 
		(
			[CurrencyCode],
			[ExchangeRate],
			[FromDate],
			[ToDate]
		)
		SELECT [CurrencyCode],
			ISNULL([ExchangeRate],0) AS [ExchangeRate],
			ISNULL(LAG([ToDate]) OVER (ORDER BY [DW_Id]),DATEADD(yy, DATEDIFF(yy, 0, [ToDate]), 0)) AS [FromDate],
			[ToDate]
		FROM [ExchangeRates]
		WHERE [CurrencyCode] = @iCurrencyCode

		SET @iCurrencyCode = @iCurrencyCode + 1
	END
	SELECT * FROM #TempTable
	DROP TABLE #TempTable
END

To use the procedure I made a custom table insert.

DECLARE @1 as INT = (SELECT MIN([CurrencyCode]) FROM [ExchangeRates])
DECLARE @2 as INT = (SELECT MAX([CurrencyCode]) FROM [ExchangeRates])
DECLARE @Temp TABLE 
(
	[CurrencyCode] int,
	[ExchangeRate] decimal(38,3),
	[FromDate] datetime,
	[ToDate] datetime
)
INSERT @Temp EXEC [spExchangeRates]
@iCurrencyCode = @1,
@iMaxCurrencyCode = @2
SELECT [CurrencyCode],
	[ExchangeRate],
	[DateValue] AS [Date] 
FROM @Temp
LEFT JOIN [Date_V] AS D ON [FromDate] <= D.[DateValue] AND [ToDate] > D.[DateValue]

The declare @temp table is allowed and you could place it in a view as well.


Christian Hauggaard
Community Manager
Forum|alt.badge.img+5

Hi @aloz do the above comments answer your question? If so please help us by marking a best answer above. Please let us know if you have any follow up questions 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings