Advertisement
anchormodeling

Concurrent-reliance-temporal

Jun 5th, 2013
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 29.19 KB | None | 0 0
  1. /*
  2.       A concurrent-reliance-temporal implementation of Anchor Modeling
  3.       by Lars Rönnbäck, November 2012
  4.  
  5.       http://www.anchormodeling.com
  6.  */
  7.  
  8. ---------------- CREATE THE IMPLEMENTATION -------------------
  9.  
  10. SELECT @@version;
  11.  
  12. DROP VIEW [dbo].[llFI_FinancialInstrument];
  13. DROP FUNCTION [dbo].[llrFI_FinancialInstrument];
  14. DROP FUNCTION [dbo].[plFI_FinancialInstrument];
  15. DROP FUNCTION [dbo].[lpFI_FinancialInstrument];
  16. DROP FUNCTION [dbo].[ppFI_FinancialInstrument];
  17. DROP FUNCTION [dbo].[pvpvrFI_FinancialInstrument];
  18. DROP FUNCTION [dbo].[rFI_RAT_FinancialInstrument_Rating];
  19. DROP FUNCTION [dbo].[rFI_RAT_FinancialInstrument_Rating_Posit];
  20. DROP FUNCTION [dbo].[rFI_RAT_FinancialInstrument_Rating_Annex];
  21. DROP VIEW [dbo].[FI_RAT_FinancialInstrument_Rating];
  22. DROP TABLE [dbo].[FI_RAT_FinancialInstrument_Rating_Annex];
  23. DROP TABLE [dbo].[FI_RAT_FinancialInstrument_Rating_Posit];
  24. DROP TABLE [dbo].[FI_FinancialInstrument];
  25. DROP FUNCTION [dbo].[vFI_RAT_FinancialInstrument_Rating];
  26.  
  27. /* -------------------------- Anchor --------------------------
  28.  * Holds identities of entities in the domain.
  29.  *
  30.  */
  31. CREATE TABLE [dbo].[FI_FinancialInstrument] (
  32.    [FI_ID] [int] IDENTITY(1,1) NOT NULL,
  33.    [Metadata_FI] [int] NOT NULL,
  34.    PRIMARY KEY CLUSTERED (
  35.       [FI_ID] ASC
  36.    )
  37. );
  38.  
  39. GO
  40.  
  41. /* --------------------- Attribute Posit ----------------------
  42.  * A statement binding a property value to an identity,
  43.  * for every point in changing time.
  44.  *
  45.  *    A_p: I x T_c -> D
  46.  */
  47. CREATE TABLE [dbo].[FI_RAT_FinancialInstrument_Rating_Posit] (
  48.    [FI_RAT_ID] [int] IDENTITY(1,1) NOT NULL,
  49.    [FI_ID] [int] NOT NULL,
  50.    [FI_RAT_FinancialInstrument_Rating] [char](1) NOT NULL,
  51.    -- Changing time (Anchor Modeling)
  52.    -- Valid time (Snodgrass)
  53.    -- Effective time (Johnston)
  54.    -- Application time (SQL:2011)
  55.    [FI_RAT_ChangedAt] [date] NOT NULL,
  56.    CONSTRAINT [pkFI_RAT_FinancialInstrument_Rating_Posit]
  57.    PRIMARY KEY NONCLUSTERED (
  58.       [FI_RAT_ID] ASC
  59.    ),
  60.    CONSTRAINT [uqFI_RAT_FinancialInstrument_Rating_Posit]
  61.    UNIQUE CLUSTERED (
  62.       [FI_ID] ASC,
  63.       [FI_RAT_ChangedAt] DESC,
  64.       [FI_RAT_FinancialInstrument_Rating]
  65.    ),
  66.    FOREIGN KEY ([FI_ID])
  67.    REFERENCES [dbo].[FI_FinancialInstrument] ([FI_ID])
  68. );
  69.  
  70. GO
  71. /* --------------------- Attribute Annex ----------------------
  72.  * A statement binding a posit to its metadata, in this case
  73.  * an agent's assessment of its reliability,
  74.  * for every point in positing time.
  75.  *
  76.  *   A_a: I x T_r -> (A, R)
  77.  */
  78.  
  79. CREATE TABLE [dbo].[FI_RAT_FinancialInstrument_Rating_Annex] (
  80.    [FI_RAT_ID] [int] NOT NULL,
  81.    -- Positing time (Anchor Modeling)
  82.    -- Transaction time (Snodgrass)
  83.    -- Assertion time (Johnston)
  84.    -- System versioned time (SQL:2011)
  85.    [FI_RAT_PositedAt] [date] NOT NULL,
  86.    -- make the following data type settable in the tool
  87.    [FI_RAT_Positor] [tinyint] NOT NULL,
  88.    -- make the following data type settable in the tool
  89.    [FI_RAT_Reliability] [tinyint] NOT NULL,
  90.    [Metadata_FI_RAT] [int] NOT NULL,
  91.    [FI_RAT_Reliable] AS CAST(
  92.    CASE
  93.       -- make this cutoff settable in the tool
  94.       WHEN [FI_RAT_Reliability] <= 0 THEN 0
  95.       ELSE 1
  96.    END AS [bit]),
  97.    CONSTRAINT [pkFI_RAT_FinancialInstrument_Rating_Annex]
  98.    PRIMARY KEY CLUSTERED (
  99.       [FI_RAT_ID] ASC,
  100.       [FI_RAT_Positor] ASC,
  101.       [FI_RAT_PositedAt] DESC
  102.    ),
  103.    FOREIGN KEY ([FI_RAT_ID])
  104.    REFERENCES [dbo].[FI_RAT_FinancialInstrument_Rating_Posit] ([FI_RAT_ID])
  105. );
  106.  
  107. GO
  108.  
  109. -- Create an assembled view of the attribute
  110. CREATE VIEW [dbo].[FI_RAT_FinancialInstrument_Rating]
  111. WITH SCHEMABINDING
  112. AS
  113. SELECT
  114.    [FI_RAT_p].[FI_ID],
  115.    [FI_RAT_p].[FI_RAT_FinancialInstrument_Rating],
  116.    [FI_RAT_p].[FI_RAT_ChangedAt],
  117.    [FI_RAT_a].[FI_RAT_PositedAt],
  118.    [FI_RAT_a].[FI_RAT_Positor],
  119.    [FI_RAT_a].[FI_RAT_Reliability],
  120.    [FI_RAT_p].[FI_RAT_ID],
  121.    [FI_RAT_a].[Metadata_FI_RAT],
  122.    [FI_RAT_a].[FI_RAT_Reliable]
  123. FROM
  124.    [dbo].[FI_RAT_FinancialInstrument_Rating_Posit] [FI_RAT_p]
  125. JOIN
  126.    [dbo].[FI_RAT_FinancialInstrument_Rating_Annex] [FI_RAT_a]
  127. ON
  128.    [FI_RAT_a].FI_RAT_ID = [FI_RAT_p].FI_RAT_ID;
  129.    
  130. GO
  131.  
  132. -- Constraint ensuring that recorded and erased posits are temporally consistent
  133. CREATE UNIQUE CLUSTERED INDEX [pkFI_RAT_FinancialInstrument_Rating]
  134. ON [dbo].[FI_RAT_FinancialInstrument_Rating](
  135.    [FI_RAT_Positor] ASC,
  136.    [FI_RAT_Reliable] DESC,
  137.    [FI_ID] ASC,
  138.    [FI_RAT_ChangedAt] DESC,
  139.    [FI_RAT_PositedAt] DESC
  140. );
  141.  
  142. GO
  143.  
  144. -- Rewind function for the annex
  145. -- all rows on or before a point in positing time
  146. CREATE FUNCTION [dbo].[rFI_RAT_FinancialInstrument_Rating_Annex] (
  147.    @positingTimepoint datetime
  148. )
  149. RETURNS TABLE WITH SCHEMABINDING
  150. AS RETURN
  151. SELECT
  152.    [FI_RAT_ID],
  153.    [FI_RAT_PositedAt],
  154.    [FI_RAT_Positor],
  155.    [FI_RAT_Reliability],
  156.    [Metadata_FI_RAT],
  157.    DENSE_RANK() OVER (
  158.       PARTITION BY [FI_RAT_ID], [FI_RAT_Positor]
  159.       ORDER BY [FI_RAT_PositedAt] DESC
  160.    ) AS [FI_RAT_PositingVersion]
  161. FROM
  162.    [dbo].[FI_RAT_FinancialInstrument_Rating_Annex]
  163. WHERE
  164.    [FI_RAT_PositedAt] <= @positingTimepoint;
  165.  
  166. GO
  167.  
  168. -- Rewind function for the posit
  169. -- all rows on or before a point in changing time
  170. CREATE FUNCTION [dbo].[rFI_RAT_FinancialInstrument_Rating_Posit] (
  171.    @changingTimepoint datetime
  172. )
  173. RETURNS TABLE WITH SCHEMABINDING
  174. AS RETURN
  175. SELECT
  176.    [FI_RAT_ID],
  177.    [FI_ID],
  178.    [FI_RAT_ChangedAt],
  179.    FI_RAT_FinancialInstrument_Rating
  180. FROM
  181.    [dbo].[FI_RAT_FinancialInstrument_Rating_Posit]
  182. WHERE
  183.    [FI_RAT_ChangedAt] <= @changingTimepoint;
  184.  
  185. GO
  186.  
  187. -- Rewind function for the attribute
  188. -- all recorded rows at the given timepoints and
  189. -- having higher than the given reliability
  190. CREATE FUNCTION [dbo].[rFI_RAT_FinancialInstrument_Rating] (
  191.    @changingTimepoint datetime,
  192.    @positingTimepoint datetime,
  193.    @positingVersion int, -- same type as identity
  194.    @reliability tinyint
  195. )
  196. RETURNS TABLE WITH SCHEMABINDING
  197. AS RETURN
  198. SELECT
  199.    [FI_RAT_p].[FI_ID],
  200.    [FI_RAT_p].[FI_RAT_FinancialInstrument_Rating],
  201.    [FI_RAT_p].[FI_RAT_ChangedAt],
  202.    [FI_RAT_a].[FI_RAT_PositedAt],
  203.    [FI_RAT_a].[FI_RAT_Positor],
  204.    [FI_RAT_a].[FI_RAT_Reliability],
  205.    [FI_RAT_p].[FI_RAT_ID],
  206.    [FI_RAT_a].[Metadata_FI_RAT],
  207.    [FI_RAT_a].[FI_RAT_PositingVersion],
  208.    DENSE_RANK() OVER (
  209.       PARTITION BY [FI_ID], [FI_RAT_Positor]  
  210.       ORDER BY [FI_RAT_ChangedAt] DESC, [FI_RAT_PositedAt] DESC
  211.    ) AS [FI_RAT_ChangingVersion]
  212. FROM
  213.    [dbo].[rFI_RAT_FinancialInstrument_Rating_Posit](
  214.       @changingTimepoint
  215.    ) [FI_RAT_p]
  216. JOIN
  217.    [dbo].[rFI_RAT_FinancialInstrument_Rating_Annex](
  218.       @positingTimepoint
  219.    ) [FI_RAT_a]
  220. ON
  221.    [FI_RAT_a].[FI_RAT_ID] = [FI_RAT_p].[FI_RAT_ID]
  222. AND
  223.    -- both needed to get the correct rank for ChangingVersion
  224.    [FI_RAT_a].[FI_RAT_Reliability] > @reliability
  225. AND
  226.    [FI_RAT_a].[FI_RAT_PositingVersion] = @positingVersion;
  227.  
  228. GO
  229.  
  230. -- function that returns 1 if the previous value is identical
  231. CREATE FUNCTION [dbo].[vFI_RAT_FinancialInstrument_Rating] (
  232.    @posit int,
  233.    @positor tinyint,
  234.    @reliable bit,
  235.    @positedAt datetime
  236. )
  237. RETURNS tinyint AS
  238. BEGIN
  239.    DECLARE @identical int = 0;
  240.    DECLARE @value char(1);
  241.    DECLARE @id int;
  242.    DECLARE @changedAt datetime;
  243.    SELECT
  244.       @value = [FI_RAT_FinancialInstrument_Rating],
  245.       @id = [FI_ID],
  246.       @changedAt = [FI_RAT_ChangedAt]
  247.    FROM
  248.       [dbo].[FI_RAT_FinancialInstrument_Rating_Posit]
  249.    WHERE
  250.       [FI_RAT_ID] = @posit;
  251.  
  252.    IF(@value IN (
  253.       SELECT
  254.          d.FI_RAT_FinancialInstrument_Rating
  255.       FROM (
  256.          SELECT
  257.             FI_RAT_FinancialInstrument_Rating,
  258.             ROW_NUMBER() OVER (
  259.                PARTITION BY BeforeOrAfter ORDER BY FI_RAT_ChangedAt ASC
  260.             ) AS FollowingVersion,
  261.             ROW_NUMBER() OVER (
  262.                PARTITION BY BeforeOrAfter ORDER BY FI_RAT_ChangedAt DESC
  263.             ) AS PrecedingVersion,
  264.             BeforeOrAfter
  265.          FROM (
  266.             SELECT
  267.                FI_RAT_FinancialInstrument_Rating,
  268.                FI_RAT_ChangedAt,
  269.                CASE
  270.                   WHEN FI_RAT_ChangedAt < @changedAt THEN 'B'
  271.                   ELSE 'A'
  272.                END as BeforeOrAfter
  273.             FROM
  274.                [dbo].[FI_RAT_FinancialInstrument_Rating]
  275.             WHERE
  276.                FI_ID = @id
  277.             AND  
  278.                FI_RAT_Positor = @positor
  279.             AND
  280.                FI_RAT_ChangedAt <> @changedAt
  281.             AND
  282.                FI_RAT_Reliable = @reliable
  283.          ) s
  284.       ) d
  285.       WHERE
  286.          (d.BeforeOrAfter = 'A' AND d.FollowingVersion = 1)
  287.       OR
  288.          (d.BeforeOrAfter = 'B' AND d.PrecedingVersion = 1)
  289.    ))
  290.    SET @identical = 1;
  291.    RETURN @identical;
  292. END
  293. GO
  294.  
  295. -- add the function above as a check that prevents restatements
  296. ALTER TABLE [dbo].[FI_RAT_FinancialInstrument_Rating_Annex]
  297. ADD CONSTRAINT [prFI_RAT_FinancialInstrument_Rating_Annex] CHECK (
  298. [dbo].[vFI_RAT_FinancialInstrument_Rating](
  299.    [FI_RAT_ID],
  300.    [FI_RAT_Positor],
  301.    [FI_RAT_Reliability],
  302.    [FI_RAT_PositedAt]
  303. ) = 0);
  304.  
  305. GO
  306.  
  307. /*
  308.  * This function is the mother of all time traveling functions.
  309.  *
  310.  * @changingTimepoint
  311.  *     the point in changing time you wish to travel to.
  312.  *
  313.  * @changingVersion
  314.  *     the version with respect to the given point in time
  315.  *     you would like to see, where 1 is the latest version,
  316.  *     2 is the second to latest, and so on.
  317.  *
  318.  * @positingTimepoint
  319.  *     the point in positing time you wish to travel to.
  320.  *
  321.  * @positingVersion
  322.  *     the version with respect to the given point in time
  323.  *     you would like to see, where 1 is the latest version,
  324.  *     2 is the second to latest, and so on.
  325.  *
  326.  * @reliability
  327.  *     the minimum (non-inclusive) reliability of the
  328.  *     returned information.
  329.  *
  330.  */
  331. CREATE FUNCTION [dbo].[pvpvrFI_FinancialInstrument] (
  332.    @changingTimepoint datetime,
  333.    @changingVersion int,
  334.    @positingTimepoint datetime,
  335.    @positingVersion int,
  336.    @reliability tinyint
  337. )
  338. RETURNS TABLE WITH SCHEMABINDING
  339. AS RETURN
  340. SELECT
  341.    [FI].[FI_ID],
  342.    [FI_RAT].[FI_RAT_FinancialInstrument_Rating],
  343.    [FI_RAT].[FI_RAT_ChangedAt],
  344.    [FI_RAT].[FI_RAT_PositedAt],
  345.    [FI_RAT].[FI_RAT_Positor],
  346.    [FI_RAT].[FI_RAT_Reliability],
  347.    [FI_RAT].[FI_RAT_ID],
  348.    [FI_RAT].[Metadata_FI_RAT]
  349. FROM
  350.    [dbo].[FI_FinancialInstrument] [FI]
  351. LEFT JOIN
  352.    [dbo].[rFI_RAT_FinancialInstrument_Rating](
  353.       @changingTimepoint,
  354.       @positingTimepoint,
  355.       @positingVersion,
  356.       @reliability
  357.    ) [FI_RAT]
  358. ON
  359.    [FI_RAT].[FI_ID] = [FI].[FI_ID]
  360. AND
  361.    [FI_RAT].FI_RAT_ChangingVersion = @changingVersion;
  362.  
  363. GO
  364.  
  365. -- the latest view of reliable information
  366. CREATE VIEW [dbo].[llFI_FinancialInstrument]
  367. AS
  368. SELECT
  369.    [FI_ID],
  370.    [FI_RAT_FinancialInstrument_Rating],
  371.    [FI_RAT_ChangedAt],
  372.    [FI_RAT_PositedAt],
  373.    [FI_RAT_Positor],
  374.    [FI_RAT_Reliability],
  375.    [FI_RAT_ID],
  376.    [Metadata_FI_RAT]
  377. FROM
  378.    [dbo].[pvpvrFI_FinancialInstrument] (
  379.       '9999-12-31',
  380.       1,
  381.       '9999-12-31',
  382.       1,
  383.       0
  384.    );
  385.  
  386. GO
  387.  
  388. -- the latest view of information with reliability
  389. CREATE FUNCTION [dbo].[llrFI_FinancialInstrument] (
  390.    @reliability tinyint
  391. )
  392. RETURNS TABLE WITH SCHEMABINDING
  393. AS RETURN
  394. SELECT
  395.    [FI_ID],
  396.    [FI_RAT_FinancialInstrument_Rating],
  397.    [FI_RAT_ChangedAt],
  398.    [FI_RAT_PositedAt],
  399.    [FI_RAT_Positor],
  400.    [FI_RAT_Reliability],
  401.    [FI_RAT_ID],
  402.    [Metadata_FI_RAT]
  403. FROM
  404.    [dbo].[pvpvrFI_FinancialInstrument] (
  405.       '9999-12-31',
  406.       1,
  407.       '9999-12-31',
  408.       1,
  409.       @reliability
  410.    );
  411.  
  412. GO
  413.    
  414. -- point-in-time for changing time and latest for positing time
  415. CREATE FUNCTION [dbo].[plFI_FinancialInstrument] (
  416.    @changingTimepoint datetime
  417. )
  418. RETURNS TABLE WITH SCHEMABINDING
  419. AS RETURN
  420. SELECT
  421.    [FI_ID],
  422.    [FI_RAT_FinancialInstrument_Rating],
  423.    [FI_RAT_ChangedAt],
  424.    [FI_RAT_PositedAt],
  425.    [FI_RAT_Positor],
  426.    [FI_RAT_Reliability],
  427.    [FI_RAT_ID],
  428.    [Metadata_FI_RAT]
  429. FROM
  430.    [dbo].[pvpvrFI_FinancialInstrument] (
  431.       @changingTimepoint,
  432.       1,
  433.       '9999-12-31',
  434.       1,
  435.       0
  436.    );
  437.    
  438. GO
  439.  
  440. -- latest for changing time and point-in-time for positing time
  441. CREATE FUNCTION [dbo].[lpFI_FinancialInstrument] (
  442.    @positingTimepoint datetime
  443. )
  444. RETURNS TABLE WITH SCHEMABINDING
  445. AS RETURN
  446. SELECT
  447.    [FI_ID],
  448.    [FI_RAT_FinancialInstrument_Rating],
  449.    [FI_RAT_ChangedAt],
  450.    [FI_RAT_PositedAt],
  451.    [FI_RAT_Positor],
  452.    [FI_RAT_Reliability],
  453.    [FI_RAT_ID],
  454.    [Metadata_FI_RAT]
  455. FROM
  456.    [dbo].[pvpvrFI_FinancialInstrument] (
  457.       '9999-12-31',
  458.       1,
  459.       @positingTimepoint,
  460.       1,
  461.       0
  462.    );
  463.    
  464. GO
  465.  
  466. -- point-in-time for changing time and point-in-time for positing time
  467. CREATE FUNCTION [dbo].[ppFI_FinancialInstrument] (
  468.    @changingTimepoint datetime,
  469.    @positingTimepoint datetime
  470. )
  471. RETURNS TABLE WITH SCHEMABINDING
  472. AS RETURN
  473. SELECT
  474.    [FI_ID],
  475.    [FI_RAT_FinancialInstrument_Rating],
  476.    [FI_RAT_ChangedAt],
  477.    [FI_RAT_PositedAt],
  478.    [FI_RAT_Positor],
  479.    [FI_RAT_Reliability],
  480.    [FI_RAT_ID],
  481.    [Metadata_FI_RAT]
  482. FROM
  483.    [dbo].[pvpvrFI_FinancialInstrument] (
  484.       @changingTimepoint,
  485.       1,
  486.       @positingTimepoint,
  487.       1,
  488.       0
  489.    );
  490.      
  491. GO
  492.  
  493. -- 0, Don't test anything
  494. -- 1, Simple bitemporal test by Craig Baumunk
  495. -- 2, Our own slightly more complex test
  496. -- 3, Performance test
  497.  
  498. DECLARE @test int = 2;
  499.  
  500. /*
  501.  Simple Bitemporal Test (using Anchor Modeling) follow link for data:
  502.  http://bitemporalmodeling.com/bitemporal-support-for-a-temporal-example/
  503. */
  504.  
  505. if(@test = 1)
  506. begin
  507.  
  508. -- create one financial instrument
  509. insert into FI_FinancialInstrument (Metadata_FI) values (42);
  510.  
  511. -- we should have no information whatsoever yet
  512. select * from FI_RAT_FinancialInstrument_Rating;
  513. -- one row should be returned for annex 1 and 100% reliability
  514. select * from llFI_FinancialInstrument;
  515.  
  516.  
  517. -- DAY 1
  518. -- insert the first ratings for the created financial instrument
  519. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  520.    FI_ID,
  521.    FI_RAT_FinancialInstrument_Rating,
  522.    FI_RAT_ChangedAt
  523. ) values
  524. (1, 'A', '2011-03-17'),
  525. (1, '?', '2011-03-19');
  526.  
  527. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  528.    FI_RAT_ID,
  529.    FI_RAT_PositedAt,
  530.    FI_RAT_Positor,
  531.    FI_RAT_Reliability,
  532.    Metadata_FI_RAT
  533. ) values
  534. (1, '2011-03-17', 1, 1, 42),
  535. (2, '2011-03-17', 1, 1, 42);
  536.  
  537. -- the latest view
  538. select * from llFI_FinancialInstrument;
  539. -- every day of the week in the test along changing time
  540. select * from plFI_FinancialInstrument('2011-03-15');
  541. select * from plFI_FinancialInstrument('2011-03-16');
  542. select * from plFI_FinancialInstrument('2011-03-17');
  543. select * from plFI_FinancialInstrument('2011-03-18');
  544. select * from plFI_FinancialInstrument('2011-03-19');
  545. select * from plFI_FinancialInstrument('2011-03-20');
  546.  
  547.  
  548. -- DAY 2
  549. -- Note that the following requires user knowledge of existing data
  550. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  551.    FI_ID,
  552.    FI_RAT_FinancialInstrument_Rating,
  553.    FI_RAT_ChangedAt
  554. ) values
  555. (1, 'B', '2011-03-16'),
  556. (1, 'A', '2011-03-18');
  557.  
  558. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  559.    FI_RAT_ID,
  560.    FI_RAT_PositedAt,
  561.    FI_RAT_Positor,
  562.    FI_RAT_Reliability,
  563.    Metadata_FI_RAT
  564. ) values
  565. (1, '2011-03-18', 1, 0, 42),
  566. (3, '2011-03-18', 1, 1, 42),
  567. (4, '2011-03-18', 1, 1, 42);
  568.  
  569. -- the latest view
  570. select * from llFI_FinancialInstrument;
  571. -- every day of the week in the test along changing time
  572. select * from plFI_FinancialInstrument('2011-03-15');
  573. select * from plFI_FinancialInstrument('2011-03-16');
  574. select * from plFI_FinancialInstrument('2011-03-17');
  575. select * from plFI_FinancialInstrument('2011-03-18');
  576. select * from plFI_FinancialInstrument('2011-03-19');
  577. select * from plFI_FinancialInstrument('2011-03-20');
  578. -- some interesting intersections
  579. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  580. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  581.  
  582.  
  583. -- DAY 3
  584. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  585.    FI_ID,
  586.    FI_RAT_FinancialInstrument_Rating,
  587.    FI_RAT_ChangedAt
  588. ) values
  589. (1, 'C', '2011-03-15'),
  590. (1, '?', '2011-03-20');
  591.  
  592. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  593.    FI_RAT_ID,
  594.    FI_RAT_PositedAt,
  595.    FI_RAT_Positor,
  596.    FI_RAT_Reliability,
  597.    Metadata_FI_RAT
  598. ) values
  599. (2, '2011-03-19', 1, 0, 42),
  600. (3, '2011-03-19', 1, 0, 42),
  601. (4, '2011-03-19', 1, 0, 42),
  602. (5, '2011-03-19', 1, 1, 42),
  603. (6, '2011-03-19', 1, 1, 42);
  604.  
  605. -- all rows in the attribute table
  606. select * from FI_RAT_FinancialInstrument_Rating;
  607. -- the latest view
  608. select * from llFI_FinancialInstrument;
  609. -- every day of the week in the test along changing time
  610. select * from plFI_FinancialInstrument('2011-03-15');
  611. select * from plFI_FinancialInstrument('2011-03-16');
  612. select * from plFI_FinancialInstrument('2011-03-17');
  613. select * from plFI_FinancialInstrument('2011-03-18');
  614. select * from plFI_FinancialInstrument('2011-03-19');
  615. select * from plFI_FinancialInstrument('2011-03-20');
  616. -- some interesting intersections
  617. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  618. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  619. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-19');
  620.  
  621.  
  622. -- DAY 4
  623. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  624.    FI_ID,
  625.    FI_RAT_FinancialInstrument_Rating,
  626.    FI_RAT_ChangedAt
  627. ) values
  628. (1, 'D', '2011-03-17');
  629.  
  630. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  631.    FI_RAT_ID,
  632.    FI_RAT_PositedAt,
  633.    FI_RAT_Positor,
  634.    FI_RAT_Reliability,
  635.    Metadata_FI_RAT
  636. ) values
  637. (6, '2011-03-20', 1, 0, 42),
  638. (7, '2011-03-20', 1, 1, 42);
  639.  
  640. -- all rows in the attribute table
  641. select * from FI_RAT_FinancialInstrument_Rating;
  642. -- the latest view
  643. select * from llFI_FinancialInstrument;
  644. -- every day of the week in the test along changing time
  645. select * from plFI_FinancialInstrument('2011-03-15');
  646. select * from plFI_FinancialInstrument('2011-03-16');
  647. select * from plFI_FinancialInstrument('2011-03-17');
  648. select * from plFI_FinancialInstrument('2011-03-18');
  649. select * from plFI_FinancialInstrument('2011-03-19');
  650. select * from plFI_FinancialInstrument('2011-03-20');
  651. -- some interesting intersections
  652. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  653. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  654. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-19');
  655. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-20');
  656.  
  657.  
  658. -- DAY 5
  659. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  660.    FI_ID,
  661.    FI_RAT_FinancialInstrument_Rating,
  662.    FI_RAT_ChangedAt
  663. ) values
  664. (1, 'E', '2011-03-18'),
  665. (1, 'D', '2011-03-20');
  666.  
  667. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  668.    FI_RAT_ID,
  669.    FI_RAT_PositedAt,
  670.    FI_RAT_Positor,
  671.    FI_RAT_Reliability,
  672.    Metadata_FI_RAT
  673. ) values
  674. (8, '2011-03-21', 1, 1, 42),
  675. (9, '2011-03-21', 1, 1, 42);
  676.  
  677. -- all rows in the attribute table
  678. select * from FI_RAT_FinancialInstrument_Rating;
  679. -- the latest view
  680. select * from llFI_FinancialInstrument;
  681. -- every day of the week in the test along changing time
  682. select * from plFI_FinancialInstrument('2011-03-15');
  683. select * from plFI_FinancialInstrument('2011-03-16');
  684. select * from plFI_FinancialInstrument('2011-03-17');
  685. select * from plFI_FinancialInstrument('2011-03-18');
  686. select * from plFI_FinancialInstrument('2011-03-19');
  687. select * from plFI_FinancialInstrument('2011-03-20');
  688. -- some interesting intersections
  689. select * from ppFI_FinancialInstrument('2011-03-18', '2011-03-20');
  690. select * from ppFI_FinancialInstrument('2011-03-18', '2011-03-21');
  691. select * from ppFI_FinancialInstrument('2011-03-19', '2011-03-20');
  692. select * from ppFI_FinancialInstrument('2011-03-19', '2011-03-21');
  693.  
  694. end
  695.  
  696.  
  697. if(@test = 2)
  698. begin
  699.  
  700. /* Our own simple test
  701.  
  702. Event 123, System 42 (100%), Arriving at 2001-01-01:
  703. - The rating of the instrument is 'A' from 1995-05-05.
  704. Event 124, System 42 (100%), Arriving at 2002-02-02:
  705. - The rating of the instrument was actually 'B' from 1995-05-05.
  706. Event 125, System 42 (100%), Arriving at 2003-03-03:
  707. - The rating of the instrument has been set to 'F' from 1999-09-09.
  708. Event 126, System 42 (100%), Arriving at 2004-04-04:
  709. - The rating of the instrument was set to 'D' at 1996-06-06.
  710. Event 127, System 42 (100%), Also arriving at 2004-04-04:
  711. - The rating of the instrument was then changed again to 'E' at 1997-07-07.
  712. Event 128, System 42 (100%), Arriving at 2005-05-05:
  713. - The rating was never set to 'F', it was an error.
  714. Event 129, System 99 (100%), Also arriving at 2005-05-05:
  715. - The rating is actually 'F' from '1999-09-09' and onwards.
  716. Event 130, System 99 (50%), Arriving at 2006-06-06:
  717. - The reliability of rating 'F' is downgraded to 50%.
  718. Event 131, System 42 (100%), Arriving at 2004-04-04:
  719. - The rating is 'E' from 1997-08-09 (retroactive restatement wrp to preceding value).
  720. Event 132, System 99 (100%), Arrived at 2001-01-01 but was forgotten until now:
  721. - The rating is 'F' from 1998-09-10 (retroactive restatement wrp to following value).
  722.  
  723. */
  724.  
  725. -- create a financial instrument
  726. insert into FI_FinancialInstrument (Metadata_FI) values (0);
  727.  
  728. -- Event 123, System 42 (100%), Arriving at 2001-01-01:
  729. -- The rating of the instrument is 'A' from 1995-05-05.
  730. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  731.    FI_ID,
  732.    FI_RAT_FinancialInstrument_Rating,
  733.    FI_RAT_ChangedAt
  734. ) values
  735. (1, 'A', '1995-05-05');
  736.  
  737. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  738.    FI_RAT_ID,
  739.    FI_RAT_PositedAt,
  740.    FI_RAT_Positor,
  741.    FI_RAT_Reliability,
  742.    Metadata_FI_RAT
  743. ) values
  744. (1, '2001-01-01', 42, 100, 123);
  745.  
  746. -- all rows in the attribute table
  747. select * from FI_RAT_FinancialInstrument_Rating;
  748. -- the latest view
  749. select * from llFI_FinancialInstrument;
  750. -- before and after
  751. select * from plFI_FinancialInstrument('1994-04-04');
  752. select * from plFI_FinancialInstrument('1996-06-06');
  753.  
  754. -- Event 124, System 42 (100%), Arriving at 2002-02-02:
  755. -- The rating of the instrument was actually 'B' from 1995-05-05.
  756. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  757.    FI_ID,
  758.    FI_RAT_FinancialInstrument_Rating,
  759.    FI_RAT_ChangedAt
  760. ) values
  761. (1, 'B', '1995-05-05');
  762.  
  763. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  764.    FI_RAT_ID,
  765.    FI_RAT_PositedAt,
  766.    FI_RAT_Positor,
  767.    FI_RAT_Reliability,
  768.    Metadata_FI_RAT
  769. ) values
  770. --(1, '2002-02-02', 42, 100, 124), -- is optional!
  771. (2, '2002-02-02', 42, 100, 124);
  772.  
  773. select dbo.vFI_RAT_FinancialInstrument_Rating(2, 42, 100, '2002-02-02')
  774.  
  775. -- all rows in the attribute table
  776. select * from FI_RAT_FinancialInstrument_Rating;
  777. -- the latest view
  778. select * from llFI_FinancialInstrument;
  779. -- before and after
  780. select * from plFI_FinancialInstrument('1994-04-04');
  781. select * from plFI_FinancialInstrument('1996-06-06');
  782. -- before and after in retrospect
  783. select * from ppFI_FinancialInstrument('1994-04-04', '2001-01-01');
  784. select * from ppFI_FinancialInstrument('1996-06-06', '2001-01-01');
  785.  
  786. -- Event 125, System 42 (100%), Arriving at 2003-03-03:
  787. -- The rating of the instrument has been set to 'F' from 1999-09-09.
  788. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  789.    FI_ID,
  790.    FI_RAT_FinancialInstrument_Rating,
  791.    FI_RAT_ChangedAt
  792. ) values
  793. (1, 'F', '1999-09-09');
  794.  
  795. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  796.    FI_RAT_ID,
  797.    FI_RAT_PositedAt,
  798.    FI_RAT_Positor,
  799.    FI_RAT_Reliability,
  800.    Metadata_FI_RAT
  801. ) values
  802. (3, '2003-03-03', 42, 100, 125);
  803.  
  804. -- all rows in the attribute table
  805. select * from FI_RAT_FinancialInstrument_Rating;
  806. -- the latest view
  807. select * from llFI_FinancialInstrument;
  808.  
  809.  
  810. -- Event 126, System 42 (100%), Arriving at 2004-04-04:
  811. -- The rating of the instrument was set to 'D' at 1996-06-06.
  812. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  813.    FI_ID,
  814.    FI_RAT_FinancialInstrument_Rating,
  815.    FI_RAT_ChangedAt
  816. ) values
  817. (1, 'D', '1996-06-06');
  818.  
  819. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  820.    FI_RAT_ID,
  821.    FI_RAT_PositedAt,
  822.    FI_RAT_Positor,
  823.    FI_RAT_Reliability,
  824.    Metadata_FI_RAT
  825. ) values
  826. (4, '2004-04-04', 42, 100, 126);
  827.  
  828.  
  829. -- all rows in the attribute table
  830. select * from FI_RAT_FinancialInstrument_Rating;
  831. -- the latest view
  832. select * from llFI_FinancialInstrument;
  833. -- before
  834. select * from plFI_FinancialInstrument('1996-06-06');
  835.  
  836. -- Event 127, System 42 (100%), Also arriving at 2004-04-04:
  837. -- The rating of the instrument was then changed again to 'E' at 1997-07-07.
  838. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  839.    FI_ID,
  840.    FI_RAT_FinancialInstrument_Rating,
  841.    FI_RAT_ChangedAt
  842. ) values
  843. (1, 'E', '1997-07-07');
  844.  
  845. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  846.    FI_RAT_ID,
  847.    FI_RAT_PositedAt,
  848.    FI_RAT_Positor,
  849.    FI_RAT_Reliability,
  850.    Metadata_FI_RAT
  851. ) values
  852. (5, '2004-04-04', 42, 100, 127);
  853.  
  854. -- all rows in the attribute table
  855. select * from FI_RAT_FinancialInstrument_Rating;
  856. -- the latest view
  857. select * from llFI_FinancialInstrument;
  858. -- before
  859. select * from plFI_FinancialInstrument('1996-06-06');
  860. select * from plFI_FinancialInstrument('1997-07-07');
  861.  
  862.  
  863. -- Event 128, System 42 (100%), Arriving at 2005-05-05:
  864. -- The rating was never set to 'F', it was an error.
  865. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  866.    FI_RAT_ID,
  867.    FI_RAT_PositedAt,
  868.    FI_RAT_Positor,
  869.    FI_RAT_Reliability,
  870.    Metadata_FI_RAT
  871. ) values
  872. (3, '2005-05-05', 42, 0, 128); -- 0 reliability ~ erased
  873.  
  874.  
  875. -- all rows in the attribute table
  876. select * from FI_RAT_FinancialInstrument_Rating;
  877. -- the latest view
  878. select * from llFI_FinancialInstrument;
  879. -- latest in retrospect
  880. select * from lpFI_FinancialInstrument('2004-04-04');
  881.  
  882. -- Event 129, System 99 (100%), Also arriving at 2005-05-05:
  883. -- The rating is actually 'F' from '1999-09-09' and onwards.
  884. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  885.    FI_RAT_ID,
  886.    FI_RAT_PositedAt,
  887.    FI_RAT_Positor,
  888.    FI_RAT_Reliability,
  889.    Metadata_FI_RAT
  890. ) values
  891. (3, '2005-05-05', 99, 100, 129);
  892.  
  893. -- all rows in the attribute table
  894. select * from FI_RAT_FinancialInstrument_Rating;
  895. -- the latest view
  896. select * from llFI_FinancialInstrument;
  897. -- before (notice that system 2 does not have an opinion at this date)
  898. select * from plFI_FinancialInstrument('1998-08-08');
  899. -- latest in retrospect
  900. select * from lpFI_FinancialInstrument('2004-04-04');
  901.  
  902. -- Event 130, System 99 (50%), Arriving at 2006-06-06:
  903. -- The reliability of rating 'F' is downgraded to 50%.
  904. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  905.    FI_RAT_ID,
  906.    FI_RAT_PositedAt,
  907.    FI_RAT_Positor,
  908.    FI_RAT_Reliability,
  909.    Metadata_FI_RAT
  910. ) values
  911. (3, '2006-06-06', 99, 50, 130);
  912.  
  913. -- all rows in the attribute table
  914. select * from FI_RAT_FinancialInstrument_Rating;
  915. -- the latest view
  916. select * from llFI_FinancialInstrument;
  917. -- the latest view with less reliable information excluded
  918. select * from llrFI_FinancialInstrument(50);
  919. -- before (notice that system 99 does not have an opinion at this date)
  920. select * from plFI_FinancialInstrument('1998-08-08');
  921. -- latest in retrospect
  922. select * from lpFI_FinancialInstrument('2004-04-04');
  923. -- latest in a different retrospect (reliability of 'F' is 100% here)
  924. select * from lpFI_FinancialInstrument('2005-05-05');
  925. -- second to latest view (there is no preceding version for system 99)
  926. select * from pvpvrFI_FinancialInstrument('9999-12-31', 2, '9999-12-31', 1, 0);
  927.  
  928. -- Event 131, System 42 (100%), Arriving at 2004-04-04:
  929. -- The rating is 'E' from 1997-08-09 (restatement).
  930. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  931.    FI_ID,
  932.    FI_RAT_FinancialInstrument_Rating,
  933.    FI_RAT_ChangedAt
  934. ) values
  935. (1, 'E', '1997-08-09');
  936.  
  937. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  938.    FI_RAT_ID,
  939.    FI_RAT_PositedAt,
  940.    FI_RAT_Positor,
  941.    FI_RAT_Reliability,
  942.    Metadata_FI_RAT
  943. ) values
  944. (6, '2004-04-04', 42, 100, 131);
  945. -- BANG! Fails restatement check.
  946.  
  947. -- Event 132, System 99 (100%), Arrived at 2001-01-01 but was forgotten until now:
  948. -- The rating is 'F' from 1998-09-10 (retroactive restatement wrp to following value).
  949. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  950.    FI_ID,
  951.    FI_RAT_FinancialInstrument_Rating,
  952.    FI_RAT_ChangedAt
  953. ) values
  954. (1, 'F', '1998-09-10');
  955.  
  956. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  957.    FI_RAT_ID,
  958.    FI_RAT_PositedAt,
  959.    FI_RAT_Positor,
  960.    FI_RAT_Reliability,
  961.    Metadata_FI_RAT
  962. ) values
  963. (7, '2001-01-01', 99, 100, 132);
  964. -- BANG! Fails restatement check.
  965.  
  966. end
  967.  
  968. if(@test = 3)
  969. begin
  970.  
  971. ------------ PERFORMANCE TESTING (Create a bunch of test data) ---------------
  972. with numGenerator as (
  973.    select
  974.       1 as num
  975.    union all
  976.    select
  977.       num + 1
  978.    from
  979.       numGenerator
  980.    where
  981.       num < 1000000
  982. )
  983. insert into FI_FinancialInstrument (
  984.    Metadata_FI
  985. )
  986. select
  987.    num
  988. from
  989.    numGenerator
  990. option(MAXRECURSION 0);
  991.  
  992. insert into FI_RAT_FinancialInstrument_Rating_Posit (
  993.    FI_ID,
  994.    FI_RAT_FinancialInstrument_Rating,
  995.    FI_RAT_ChangedAt
  996. )
  997. select
  998.    FI_ID,
  999.    'A',
  1000.    getdate()
  1001. from
  1002.    FI_FinancialInstrument;
  1003.  
  1004. insert into FI_RAT_FinancialInstrument_Rating_Annex (
  1005.    FI_RAT_ID,
  1006.    FI_RAT_PositedAt,
  1007.    FI_RAT_Positor,
  1008.    FI_RAT_Reliability,
  1009.    Metadata_FI_RAT
  1010. )
  1011. select
  1012.    FI_RAT_ID,
  1013.    getdate(),
  1014.    1,
  1015.    1,
  1016.    42
  1017. from
  1018.    FI_RAT_FinancialInstrument_Rating_Posit;  
  1019.  
  1020. -- look at the execution plan for this
  1021. -- tried implementing the two suggested indexes from the plan,
  1022. -- but that made the query 60% slower!
  1023. declare @start datetime = getdate();
  1024. select
  1025.    count(*)
  1026. from
  1027.    llFI_FinancialInstrument
  1028. where
  1029.    FI_RAT_FinancialInstrument_Rating = 'A';
  1030. select DATEDIFF(MS, @start, GETDATE());
  1031.  
  1032. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement