
--DELTA

--Example 1
	--When omitted, the retrieval mode by default is Delta.
	
	SELECT	DateTime, TagName, Value
	FROM 	History
	WHERE 	TagName = 'M100.TT.PV'
	AND		wwRetrievalMode='Delta'
	
	--1. How many records are retrieved?____
	--2. What is the time window covered by the above query?________
	
--Example 2
	--Relative time window 2 hours ago until now.
	--Notice the amount of records retrieved compared with Example 1.

	SELECT 	DateTime, TagName, Value
	FROM 	History
	WHERE 	TagName = 'M100.TT.PV'
	AND		DateTime > Dateadd(hh, -2, Getdate())
	AND  	DateTime <= Getdate()

	--3. How many records are retrieved with the above query?_____

--CYCLIC

--Example 3

	SELECT 	DateTime, TagName, Value
	FROM 	History
	WHERE 	TagName ='M100.LT.PV'
	AND 	wwRetrievalMode = 'cyclic'

	--4. How many records are retrieved with the above query?________
	--5. What is the time window covered by the above query?________

--Example 4
	--Relative time window 120 minutes ago until now.

	SELECT 	DateTime, TagName, Value
	FROM 	History
	WHERE 	TagName ='M100.LT.PV'
	AND 	wwRetrievalMode = 'cyclic'
	AND		DateTime > Dateadd(mi, -120, Getdate())
	AND  	DateTime <= Getdate()

	--6. How many records are retrieved with the above query?________
	--7. What is the time window covered by the above query?______
	--8. Change the -120 minutes for  -180 and write how many records are retrieved: _______
	--9. Cyclic mode retrieves____ records evenly spaced for the specified period.

--FULL

--Example 5

	SELECT 	DateTime, TagName, Value
	FROM 	History
	WHERE 	TagName = 'M100.LT.PV.Forced5'
	AND 	wwRetrievalMode = 'Full'

	--10. What is the time window covered by the above query?______

--Example 6
	--Relative time window 3 minutes ago until now.
	
	SELECT 	DateTime, TagName, Value
	FROM 	History
	WHERE 	TagName = 'M100.LT.PV.Forced5'
	AND 	wwRetrievalMode = 'Full'
	AND		DateTime > Dateadd(mi, -3, Getdate())
	AND  	DateTime <= Getdate()

	--11. The value ____appears consecutively repeated in the retrieved set of records.
	--12. Why do you see repeated values in the retrieved set of records?
	--_________________________________________________________________


--BEST FIT

--Example 7

	SELECT	DateTime, TagName, Value
	FROM	History
	WHERE	TagName = 'M100.LT.PV'
	AND		wwRetrievalMode = 'BestFit'

	--13. What is the time window covered by the above query?______

--Example 8
	--Best Fit by default retrieves values for 100 cycles for the 
        --specified period. This query retrieves values for a relative time window 
        --30 minutes ago until now.

	SELECT	DateTime, TagName, Value
	FROM	History
	WHERE	TagName = 'M100.LT.PV'
	AND		wwRetrievalMode = 'BestFit'
	AND		DateTime > Dateadd(mi, -30, Getdate())
	AND  	DateTime <= Getdate()

	--14. Why do you see more than 100 records retrieved by this query?
	--__________________________________________________________

--WIDE TABLE QUERY

--Example 9
	--wwRetrievalMode can be also used in Open Queries.

	SELECT * FROM OpenQuery(InSQL, '
		SELECT DateTime, [M100.TT.PV], [M100.LT.PV]
		FROM   WideHistory
		WHERE  DateTime > Dateadd(mi, -15, Getdate())
		AND    DateTime <= Getdate()
		AND    wwRetrievalMode = "Delta"
	')

	--15. Do you see any repeated values in the results of this query?___
	--16. If so, why are you getting repeated values if the retrieval mode is Delta?_____
