To add rows to the [P2 Relational DB].dbo.Timeseries Tags table following the specified pattern, you'll want to use an INSERT INTO statement along with a sequence of VALUES clauses to insert the data. You may also require some procedural logic or programming to increment the Timestamp by 12 hours for each subsequent row.
However, to simplify the SQL statement, we will first define the sequence of values and timestamps, and then we'll use the INSERT INTO command.
The pattern of 'Value' is 16 integers long: 2,3,2,4,3,4,3,5,4,1,5,1,5,2,1,3. It repeats every 16 entries. To compute timestamps, we can add intervals of 12 hours to the start date.
Here's an SQL script that manually creates the sequence by enumerating all the values with their respective timestamps:
DECLARE @startTimestamp DATETIME
DECLARE @currentTimestamp DATETIME
DECLARE @valueSequence INT[]
DECLARE @index INT
SET @startTimestamp = '2022-11-23T18:00:00.000'
SET @currentTimestamp = @startTimestamp
SET @valueSequence = [2,3,2,4,3,4,3,5,4,1,5,1,5,2,1,3]
SET @index = 0
WHILE @currentTimestamp <= '2023-01-17T06:00:00.000'
BEGIN
INSERT INTO [P2 Relational DB].dbo.[Timeseries Tags] (Tagname, Value, Timestamp)
VALUES ('A.R_ShiftNumTest', @valueSequence[@index % 16], @currentTimestamp);
-- Increment timestamp by 12 hours
SET @currentTimestamp = DATEADD(HOUR, 12, @currentTimestamp)
-- Increment index
SET @index = @index + 1
END
This script assumes that your SQL environment supports arrays and control-flow structures like WHILE. If it does not, this script would not work with vanilla T-SQL without modifications, as you would need a different method to generate each value in sequence and to iterate for each timestamp increment.
Please note that most SQL environments, including MS SQL Server, do not support array-like structures as depicted above or the use of the modulus operator on an array variable. You would typically need to create a separate table with a row for each value and join this table on a generated sequence of numbers or use a series of UNION ALL statements for each value while using DATEADD to increment the timestamp for each row:
INSERT INTO [P2 Relational DB].dbo.[Timeseries Tags] (Tagname, Value, Timestamp)
SELECT 'A.R_ShiftNumTest', [Value], DATEADD(HOUR, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1) * 12, '2022-11-23T18:00:00.000')
FROM (
VALUES (2), (3), (2), (4), (3), (4), (3), (5),
(4), (1), (5), (1), (5), (2), (1), (3)
) AS Seq([Value])
CROSS JOIN (
SELECT TOP (YourCalculationForHowManyRowsYouNeed) NULL FROM SomeLargeTable
) AS n
WHERE DATEADD(HOUR, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1) * 12, '2022-11-23T18:00:00.000') <= '2023-01-17T06:00:00.000'
This example also assumes that "SomeLargeTable" is a table in your database with a sufficient number of rows to complete your series. The YourCalculationForHowManyRowsYouNeed should be replaced with the exact number of intervals you need which must be calculated based on the time range you provided.