Advertisement
anchormodeling

Bitemporal Anchor Modeling 1C2R Two Tables

Nov 17th, 2011
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 22.32 KB | None | 0 0
  1. /*
  2.  Simple Bitemporal Test (using Anchor Modeling)
  3.  http://www.temporaldata.com/wp-content/uploads/2011/04/bitemporal-support-for-a-temporal-example.swf
  4. */
  5.  
  6. -- Anchor table
  7. CREATE TABLE [dbo].[FI_FinancialInstrument] (
  8.     [FI_ID] [int] IDENTITY(1,1) NOT NULL,
  9.     [Metadata_FI] [int] NOT NULL,
  10.     PRIMARY KEY CLUSTERED (
  11.         [FI_ID] ASC
  12.     )
  13. );
  14.  
  15. GO
  16.  
  17. -- Attribute table (keeping the currently recorded information)
  18. CREATE TABLE [dbo].[FI_RAT_FinancialInstrument_Rating] (
  19.     [FI_ID] [int] NOT NULL,
  20.     [FI_RAT_FinancialInstrument_Rating] [char](1) NOT NULL,
  21.     -- Changing time (Anchor Modeling), Valid time (Snodgrass), Effective time (Johnston)
  22.     [FI_RAT_ChangedAt] [date] NOT NULL,
  23.     -- Recording time (Anchor Modeling), Transaction time (Snodgrass), Assertion time (Johnston)
  24.     [FI_RAT_RecordedAt] [date] NOT NULL,
  25.     [Metadata_FI_RAT] [int] NOT NULL,
  26.     PRIMARY KEY CLUSTERED (
  27.         [FI_ID] ASC,
  28.         [FI_RAT_ChangedAt] DESC
  29.     ),
  30.     FOREIGN KEY ([FI_ID]) REFERENCES [dbo].[FI_FinancialInstrument] ([FI_ID])
  31. );
  32.  
  33. GO
  34.  
  35. -- Attribute table (keeping the erased, previously recorded information)
  36. CREATE TABLE [dbo].[eFI_RAT_FinancialInstrument_Rating] (
  37.     [FI_ID] [int] NOT NULL,
  38.     [FI_RAT_FinancialInstrument_Rating] [char](1) NOT NULL,
  39.     -- Changing time (Anchor Modeling), Valid time (Snodgrass), Effective time (Johnston)
  40.     [FI_RAT_ChangedAt] [date] NOT NULL,
  41.     -- Recording time (Anchor Modeling), Transaction time (Snodgrass), Assertion time (Johnston)
  42.     [FI_RAT_RecordedAt] [date] NOT NULL,
  43.     [FI_RAT_ErasedAt] [date] NOT NULL,
  44.     [Metadata_FI_RAT] [int] NOT NULL,
  45.     PRIMARY KEY CLUSTERED (
  46.         [FI_ID] ASC,
  47.         [FI_RAT_ChangedAt] DESC,
  48.         [FI_RAT_RecordedAt] DESC
  49.     ),
  50.     FOREIGN KEY ([FI_ID]) REFERENCES [dbo].[FI_FinancialInstrument] ([FI_ID])
  51. );
  52.  
  53. GO
  54.  
  55. -- This view combines recorded and erased information and adds a
  56. -- computed reliability bit, which we shall also use for 'deletes'
  57. CREATE VIEW [dbo].[tFI_RAT_FinancialInstrument_Rating]
  58. AS
  59. SELECT
  60.     [FI_ID],
  61.     [FI_RAT_FinancialInstrument_Rating],
  62.     [FI_RAT_ChangedAt],
  63.     [FI_RAT_RecordedAt],
  64.     null as [FI_RAT_ErasedAt],
  65.     1 as FI_RAT_Reliability,
  66.     [Metadata_FI_RAT]
  67. FROM
  68.     [dbo].[FI_RAT_FinancialInstrument_Rating]
  69. UNION ALL
  70. SELECT
  71.     [FI_ID],
  72.     [FI_RAT_FinancialInstrument_Rating],
  73.     [FI_RAT_ChangedAt],
  74.     [FI_RAT_RecordedAt],
  75.     [FI_RAT_ErasedAt],
  76.     0 as FI_RAT_Reliability,
  77.     [Metadata_FI_RAT]
  78. FROM
  79.     [dbo].[eFI_RAT_FinancialInstrument_Rating];
  80.  
  81. GO
  82.  
  83. -- Insert trigger on the view
  84. -- If the reliability bit is 1 then its a regular insert
  85. -- If the reliability bit is 0 then its a logical delete
  86. CREATE TRIGGER [dbo].[itFI_RAT_FinancialInstrument_Rating] ON [dbo].[tFI_RAT_FinancialInstrument_Rating]
  87. INSTEAD OF INSERT
  88. AS
  89. BEGIN
  90. SET NOCOUNT ON;
  91. INSERT INTO [dbo].[FI_RAT_FinancialInstrument_Rating] (
  92.     [FI_ID],
  93.     [FI_RAT_FinancialInstrument_Rating],
  94.     [FI_RAT_ChangedAt],
  95.     [FI_RAT_RecordedAt],
  96.     [Metadata_FI_RAT]
  97. )
  98. SELECT
  99.     [FI_ID],
  100.     [FI_RAT_FinancialInstrument_Rating],
  101.     [FI_RAT_ChangedAt],
  102.     [FI_RAT_RecordedAt],
  103.     [Metadata_FI_RAT]
  104. FROM
  105.     inserted
  106. WHERE
  107.     FI_RAT_Reliability = 1;
  108.  
  109. INSERT INTO [dbo].[eFI_RAT_FinancialInstrument_Rating] (
  110.     [FI_ID],
  111.     [FI_RAT_FinancialInstrument_Rating],
  112.     [FI_RAT_ChangedAt],
  113.     [FI_RAT_RecordedAt],
  114.     [FI_RAT_ErasedAt],
  115.     [Metadata_FI_RAT]
  116. )
  117. SELECT
  118.     [FI_RAT].[FI_ID],
  119.     [FI_RAT].[FI_RAT_FinancialInstrument_Rating],
  120.     [FI_RAT].[FI_RAT_ChangedAt],
  121.     [FI_RAT].[FI_RAT_RecordedAt],
  122.     ISNULL(i.[FI_RAT_ErasedAt], getdate()), -- DW/OLTP
  123.     i.[Metadata_FI_RAT]
  124. FROM
  125.     inserted i
  126. JOIN
  127.     [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  128. ON
  129.     i.FI_ID = [FI_RAT].FI_ID
  130. AND
  131.     i.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  132. WHERE
  133.     i.FI_RAT_Reliability = 0;
  134.    
  135. DELETE [FI_RAT]
  136. FROM
  137.     [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  138. JOIN
  139.     inserted i
  140. ON
  141.     i.FI_ID = [FI_RAT].FI_ID
  142. AND
  143.     i.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  144. WHERE
  145.     i.FI_RAT_Reliability = 0;
  146. END
  147.  
  148. GO 
  149.  
  150. /*
  151.     There are nine 'perspectives' of the information resulting
  152.     from latest, point-in-time, and interval perspectives
  153.     taken over the two timelines (changing and recording).
  154.     I will show three of them here.
  155. */
  156.  
  157. -- latest for changing time and latest for recording time
  158. CREATE VIEW [dbo].[llFI_FinancialInstrument]
  159. AS
  160. SELECT
  161.    [FI].FI_ID,
  162.    [FI].Metadata_FI,
  163.    [FI_RAT].FI_RAT_FinancialInstrument_Rating,
  164.    [FI_RAT].FI_RAT_ChangedAt,
  165.    [FI_RAT].FI_RAT_RecordedAt,
  166.    [FI_RAT].Metadata_FI_RAT
  167. FROM
  168.    [dbo].[FI_FinancialInstrument] [FI]
  169. LEFT JOIN
  170.    [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  171. ON
  172.    [FI_RAT].FI_ID = [FI].FI_ID
  173. AND
  174.    [FI_RAT].FI_RAT_ChangedAt = (
  175.       SELECT
  176.          max(sub.FI_RAT_ChangedAt)
  177.       FROM
  178.          [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  179.       WHERE
  180.          sub.FI_ID = [FI].FI_ID
  181.     );
  182.  
  183. GO
  184.  
  185. -- point-in-time for changing time and latest for recording time
  186. CREATE FUNCTION [dbo].[plFI_FinancialInstrument] (
  187.     @timepoint datetime
  188. )
  189. RETURNS TABLE
  190. RETURN
  191. SELECT
  192.    [FI].FI_ID,
  193.    [FI].Metadata_FI,
  194.    [FI_RAT].FI_RAT_FinancialInstrument_Rating,
  195.    [FI_RAT].FI_RAT_ChangedAt,
  196.    [FI_RAT].FI_RAT_RecordedAt,
  197.    [FI_RAT].Metadata_FI_RAT
  198. FROM
  199.    [dbo].[FI_FinancialInstrument] [FI]
  200. LEFT JOIN
  201.    [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  202. ON
  203.    [FI_RAT].FI_ID = [FI].FI_ID
  204. AND
  205.    [FI_RAT].FI_RAT_ChangedAt = (
  206.       SELECT
  207.          max(sub.FI_RAT_ChangedAt)
  208.       FROM
  209.          [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  210.       WHERE
  211.          sub.FI_ID = [FI].FI_ID
  212.       AND
  213.          sub.FI_RAT_ChangedAt <= @timepoint
  214.     );
  215.  
  216. GO
  217.  
  218. -- point-in-time for changing time and point-in-time for recording time
  219. CREATE FUNCTION [dbo].[ppFI_FinancialInstrument] (
  220.     @changingTimepoint datetime,
  221.     @recordingTimepoint datetime   
  222. )
  223. RETURNS TABLE
  224. RETURN
  225. SELECT
  226.    [FI].FI_ID,
  227.    [FI].Metadata_FI,  
  228.    case
  229.         when [FI_RAT].FI_ID is null
  230.             then [eFI_RAT].FI_RAT_FinancialInstrument_Rating
  231.         when [eFI_RAT].FI_ID is null
  232.             then [FI_RAT].FI_RAT_FinancialInstrument_Rating
  233.         when [FI_RAT].FI_RAT_ChangedAt > [eFI_RAT].FI_RAT_ChangedAt
  234.             then [FI_RAT].FI_RAT_FinancialInstrument_Rating
  235.         when [FI_RAT].FI_RAT_ChangedAt < [eFI_RAT].FI_RAT_ChangedAt
  236.             then [eFI_RAT].FI_RAT_FinancialInstrument_Rating
  237.         when [FI_RAT].FI_RAT_RecordedAt >= [eFI_RAT].FI_RAT_RecordedAt
  238.             then [FI_RAT].FI_RAT_FinancialInstrument_Rating
  239.         else [eFI_RAT].FI_RAT_FinancialInstrument_Rating
  240.     end as FI_RAT_FinancialInstrument_Rating,
  241.    case
  242.         when [FI_RAT].FI_ID is null
  243.             then [eFI_RAT].FI_RAT_ChangedAt
  244.         when [eFI_RAT].FI_ID is null
  245.             then [FI_RAT].FI_RAT_ChangedAt
  246.         when [FI_RAT].FI_RAT_ChangedAt > [eFI_RAT].FI_RAT_ChangedAt
  247.             then [FI_RAT].FI_RAT_ChangedAt
  248.         when [FI_RAT].FI_RAT_ChangedAt < [eFI_RAT].FI_RAT_ChangedAt
  249.             then [eFI_RAT].FI_RAT_ChangedAt
  250.         when [FI_RAT].FI_RAT_RecordedAt >= [eFI_RAT].FI_RAT_RecordedAt
  251.             then [FI_RAT].FI_RAT_ChangedAt
  252.         else [eFI_RAT].FI_RAT_ChangedAt
  253.     end as FI_RAT_ChangedAt,
  254.    case
  255.         when [FI_RAT].FI_ID is null
  256.             then [eFI_RAT].FI_RAT_RecordedAt
  257.         when [eFI_RAT].FI_ID is null
  258.             then [FI_RAT].FI_RAT_RecordedAt
  259.         when [FI_RAT].FI_RAT_ChangedAt > [eFI_RAT].FI_RAT_ChangedAt
  260.             then [FI_RAT].FI_RAT_RecordedAt
  261.         when [FI_RAT].FI_RAT_ChangedAt < [eFI_RAT].FI_RAT_ChangedAt
  262.             then [eFI_RAT].FI_RAT_RecordedAt
  263.         when [FI_RAT].FI_RAT_RecordedAt >= [eFI_RAT].FI_RAT_RecordedAt
  264.             then [FI_RAT].FI_RAT_RecordedAt            
  265.         else [eFI_RAT].FI_RAT_RecordedAt
  266.     end as FI_RAT_RecordedAt,
  267.    case
  268.         when [FI_RAT].FI_ID is null
  269.             then [eFI_RAT].FI_RAT_ErasedAt
  270.         when [eFI_RAT].FI_ID is null
  271.             then null
  272.         when [FI_RAT].FI_RAT_ChangedAt > [eFI_RAT].FI_RAT_ChangedAt
  273.             then null
  274.         when [FI_RAT].FI_RAT_ChangedAt < [eFI_RAT].FI_RAT_ChangedAt
  275.             then [eFI_RAT].FI_RAT_ErasedAt
  276.         when [FI_RAT].FI_RAT_RecordedAt >= [eFI_RAT].FI_RAT_RecordedAt
  277.             then null          
  278.         else [eFI_RAT].FI_RAT_ErasedAt
  279.     end as FI_RAT_ErasedAt,
  280.    case
  281.         when [FI_RAT].FI_ID is null
  282.             then [eFI_RAT].Metadata_FI_RAT
  283.         when [eFI_RAT].FI_ID is null
  284.             then [FI_RAT].Metadata_FI_RAT
  285.         when [FI_RAT].FI_RAT_ChangedAt > [eFI_RAT].FI_RAT_ChangedAt
  286.             then [FI_RAT].Metadata_FI_RAT
  287.         when [FI_RAT].FI_RAT_ChangedAt < [eFI_RAT].FI_RAT_ChangedAt
  288.             then [eFI_RAT].Metadata_FI_RAT
  289.         when [FI_RAT].FI_RAT_RecordedAt >= [eFI_RAT].FI_RAT_RecordedAt
  290.             then [FI_RAT].Metadata_FI_RAT
  291.         else [eFI_RAT].Metadata_FI_RAT
  292.     end as Metadata_FI_RAT
  293. FROM
  294.    [dbo].[FI_FinancialInstrument] [FI]
  295. LEFT JOIN
  296.    [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  297. ON
  298.    [FI_RAT].FI_ID = [FI].FI_ID
  299. AND
  300.    [FI_RAT].FI_RAT_ChangedAt = (
  301.       SELECT
  302.          max(sub.FI_RAT_ChangedAt)
  303.       FROM
  304.          [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  305.       WHERE
  306.          sub.FI_ID = [FI].FI_ID
  307.       AND
  308.          sub.FI_RAT_ChangedAt <= @changingTimepoint
  309.       AND
  310.          sub.FI_RAT_RecordedAt <= @recordingTimepoint
  311.     )
  312. LEFT JOIN
  313.    [dbo].[eFI_RAT_FinancialInstrument_Rating] [eFI_RAT]
  314. ON
  315.    [eFI_RAT].FI_ID = [FI].FI_ID
  316. AND
  317.    [eFI_RAT].FI_RAT_RecordedAt = (
  318.         SELECT
  319.             max(sub.FI_RAT_RecordedAt)
  320.         FROM
  321.             [dbo].[eFI_RAT_FinancialInstrument_Rating] sub
  322.         WHERE
  323.             sub.FI_ID = [FI].FI_ID
  324.         AND
  325.             sub.FI_RAT_RecordedAt <= @recordingTimepoint
  326.         AND
  327.             sub.FI_RAT_ErasedAt >= @recordingTimepoint
  328.     )
  329. AND
  330.     [eFI_RAT].FI_RAT_ChangedAt = (
  331.         SELECT
  332.             max(sub.FI_RAT_ChangedAt)
  333.         FROM
  334.             [dbo].[eFI_RAT_FinancialInstrument_Rating] sub
  335.         WHERE
  336.             sub.FI_ID = [eFI_RAT].FI_ID
  337.         AND
  338.             sub.FI_RAT_RecordedAt = [eFI_RAT].FI_RAT_RecordedAt    
  339.         AND
  340.             sub.FI_RAT_ChangedAt <= @changingTimepoint
  341.     );
  342.  
  343. GO
  344.  
  345. -- create one financial instrument
  346. insert into FI_FinancialInstrument (Metadata_FI) values (42);
  347.  
  348. -- we should have no information whatsoever yet
  349. delete from FI_RAT_FinancialInstrument_Rating;
  350. delete from eFI_RAT_FinancialInstrument_Rating;
  351. select * from tFI_RAT_FinancialInstrument_Rating;
  352. select * from llFI_FinancialInstrument;
  353.  
  354. -- DAY 1
  355. -- insert the first ratings for the created financial instrument
  356. insert into tFI_RAT_FinancialInstrument_Rating (
  357.     [FI_ID],
  358.     [FI_RAT_FinancialInstrument_Rating],
  359.     [FI_RAT_ChangedAt],
  360.     [FI_RAT_RecordedAt],
  361.     [FI_RAT_Reliability],
  362.     [Metadata_FI_RAT]
  363. ) values (1, 'A', '2011-03-17', '2011-03-17', 1, 42);
  364. insert into tFI_RAT_FinancialInstrument_Rating (
  365.     [FI_ID],
  366.     [FI_RAT_FinancialInstrument_Rating],
  367.     [FI_RAT_ChangedAt],
  368.     [FI_RAT_RecordedAt],
  369.     [FI_RAT_Reliability],
  370.     [Metadata_FI_RAT]
  371. ) values (1, '?', '2011-03-19', '2011-03-17', 1, 42);
  372.  
  373. -- all rows in the attribute tables
  374. select * from tFI_RAT_FinancialInstrument_Rating;
  375. -- looking at the split between recorded and erased
  376. select * from FI_RAT_FinancialInstrument_Rating;
  377. select * from eFI_RAT_FinancialInstrument_Rating;
  378. -- the latest view
  379. select * from llFI_FinancialInstrument;
  380. -- every day of the week in the test along changing time
  381. select * from plFI_FinancialInstrument('2011-03-15');
  382. select * from plFI_FinancialInstrument('2011-03-16');
  383. select * from plFI_FinancialInstrument('2011-03-17');
  384. select * from plFI_FinancialInstrument('2011-03-18');
  385. select * from plFI_FinancialInstrument('2011-03-19');
  386. select * from plFI_FinancialInstrument('2011-03-20');
  387.  
  388. -- DAY 2
  389. -- Note that the following requires user knowledge of existing data
  390. -- We need to remove what was wrong before we can insert the correction
  391. insert into tFI_RAT_FinancialInstrument_Rating (
  392.     [FI_ID],
  393.     [FI_RAT_ChangedAt],
  394.     [FI_RAT_ErasedAt],
  395.     [FI_RAT_Reliability],
  396.     [Metadata_FI_RAT]
  397. ) values (1, '2011-03-17', '2011-03-18', 0, 43);
  398.    
  399. insert into tFI_RAT_FinancialInstrument_Rating (
  400.     [FI_ID],
  401.     [FI_RAT_FinancialInstrument_Rating],
  402.     [FI_RAT_ChangedAt],
  403.     [FI_RAT_RecordedAt],
  404.     [FI_RAT_Reliability],
  405.     [Metadata_FI_RAT]
  406. ) values (1, 'B', '2011-03-16', '2011-03-18', 1, 43);
  407. insert into tFI_RAT_FinancialInstrument_Rating (
  408.     [FI_ID],
  409.     [FI_RAT_FinancialInstrument_Rating],
  410.     [FI_RAT_ChangedAt],
  411.     [FI_RAT_RecordedAt],
  412.     [FI_RAT_Reliability],
  413.     [Metadata_FI_RAT]
  414. ) values (1, 'A', '2011-03-18', '2011-03-18', 1, 43);
  415.  
  416. -- all rows in the attribute tables
  417. select * from tFI_RAT_FinancialInstrument_Rating;
  418. -- looking at the split between recorded and erased
  419. select * from FI_RAT_FinancialInstrument_Rating;
  420. select * from eFI_RAT_FinancialInstrument_Rating;
  421. -- the latest view
  422. select * from llFI_FinancialInstrument;
  423. -- every day of the week in the test along changing time
  424. select * from plFI_FinancialInstrument('2011-03-15');
  425. select * from plFI_FinancialInstrument('2011-03-16');
  426. select * from plFI_FinancialInstrument('2011-03-17');
  427. select * from plFI_FinancialInstrument('2011-03-18');
  428. select * from plFI_FinancialInstrument('2011-03-19');
  429. select * from plFI_FinancialInstrument('2011-03-20');
  430. -- some interesting intersections
  431. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  432. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  433.  
  434. -- DAY 3
  435. insert into tFI_RAT_FinancialInstrument_Rating (
  436.     [FI_ID],
  437.     [FI_RAT_ChangedAt],
  438.     [FI_RAT_ErasedAt],
  439.     [FI_RAT_Reliability],
  440.     [Metadata_FI_RAT]
  441. ) values (1, '2011-03-16', '2011-03-19', 0, 44);
  442. insert into tFI_RAT_FinancialInstrument_Rating (
  443.     [FI_ID],
  444.     [FI_RAT_ChangedAt],
  445.     [FI_RAT_ErasedAt],
  446.     [FI_RAT_Reliability],
  447.     [Metadata_FI_RAT]
  448. ) values (1, '2011-03-18', '2011-03-19', 0, 44);
  449. insert into tFI_RAT_FinancialInstrument_Rating (
  450.     [FI_ID],
  451.     [FI_RAT_ChangedAt],
  452.     [FI_RAT_ErasedAt],
  453.     [FI_RAT_Reliability],
  454.     [Metadata_FI_RAT]
  455. ) values (1, '2011-03-19', '2011-03-19', 0, 44);
  456. insert into tFI_RAT_FinancialInstrument_Rating (
  457.     [FI_ID],
  458.     [FI_RAT_FinancialInstrument_Rating],
  459.     [FI_RAT_ChangedAt],
  460.     [FI_RAT_RecordedAt],
  461.     [FI_RAT_Reliability],
  462.     [Metadata_FI_RAT]
  463. ) values (1, 'C', '2011-03-15', '2011-03-19', 1, 44);
  464. insert into tFI_RAT_FinancialInstrument_Rating (
  465.     [FI_ID],
  466.     [FI_RAT_FinancialInstrument_Rating],
  467.     [FI_RAT_ChangedAt],
  468.     [FI_RAT_RecordedAt],
  469.     [FI_RAT_Reliability],
  470.     [Metadata_FI_RAT]
  471. ) values (1, '?', '2011-03-20', '2011-03-19', 1, 44);
  472.  
  473. -- all rows in the attribute tables
  474. select * from tFI_RAT_FinancialInstrument_Rating;
  475. -- looking at the split between recorded and erased
  476. select * from FI_RAT_FinancialInstrument_Rating;
  477. select * from eFI_RAT_FinancialInstrument_Rating;
  478. -- the latest view
  479. select * from llFI_FinancialInstrument;
  480. -- every day of the week in the test along changing time
  481. select * from plFI_FinancialInstrument('2011-03-15');
  482. select * from plFI_FinancialInstrument('2011-03-16');
  483. select * from plFI_FinancialInstrument('2011-03-17');
  484. select * from plFI_FinancialInstrument('2011-03-18');
  485. select * from plFI_FinancialInstrument('2011-03-19');
  486. select * from plFI_FinancialInstrument('2011-03-20');
  487. -- some interesting intersections
  488. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  489. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  490. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-19');
  491.  
  492. -- DAY 4
  493. insert into tFI_RAT_FinancialInstrument_Rating (
  494.     [FI_ID],
  495.     [FI_RAT_ChangedAt],
  496.     [FI_RAT_ErasedAt],
  497.     [FI_RAT_Reliability],
  498.     [Metadata_FI_RAT]
  499. ) values (1, '2011-03-20', '2011-03-20', 0, 45);
  500. insert into tFI_RAT_FinancialInstrument_Rating (
  501.     [FI_ID],
  502.     [FI_RAT_FinancialInstrument_Rating],
  503.     [FI_RAT_ChangedAt],
  504.     [FI_RAT_RecordedAt],
  505.     [FI_RAT_Reliability],
  506.     [Metadata_FI_RAT]
  507. ) values (1, 'D', '2011-03-17', '2011-03-20', 1, 45);
  508.  
  509. -- all rows in the attribute tables
  510. select * from tFI_RAT_FinancialInstrument_Rating;
  511. -- looking at the split between recorded and erased
  512. select * from FI_RAT_FinancialInstrument_Rating;
  513. select * from eFI_RAT_FinancialInstrument_Rating;
  514. -- the latest view
  515. select * from llFI_FinancialInstrument;
  516. -- every day of the week in the test along changing time
  517. select * from plFI_FinancialInstrument('2011-03-15');
  518. select * from plFI_FinancialInstrument('2011-03-16');
  519. select * from plFI_FinancialInstrument('2011-03-17');
  520. select * from plFI_FinancialInstrument('2011-03-18');
  521. select * from plFI_FinancialInstrument('2011-03-19');
  522. select * from plFI_FinancialInstrument('2011-03-20');
  523. -- some interesting intersections
  524. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  525. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  526. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-19');
  527. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-20');
  528.  
  529.  
  530. -- DAY 5
  531. insert into tFI_RAT_FinancialInstrument_Rating (
  532.     [FI_ID],
  533.     [FI_RAT_FinancialInstrument_Rating],
  534.     [FI_RAT_ChangedAt],
  535.     [FI_RAT_RecordedAt],
  536.     [FI_RAT_Reliability],
  537.     [Metadata_FI_RAT]
  538. ) values (1, 'E', '2011-03-18', '2011-03-21', 1, 46);
  539. insert into tFI_RAT_FinancialInstrument_Rating (
  540.     [FI_ID],
  541.     [FI_RAT_FinancialInstrument_Rating],
  542.     [FI_RAT_ChangedAt],
  543.     [FI_RAT_RecordedAt],
  544.     [FI_RAT_Reliability],
  545.     [Metadata_FI_RAT]
  546. ) values (1, 'D', '2011-03-20', '2011-03-21', 1, 46);
  547.  
  548. -- all rows in the attribute tables
  549. select * from tFI_RAT_FinancialInstrument_Rating;
  550. -- looking at the split between recorded and erased
  551. select * from FI_RAT_FinancialInstrument_Rating;
  552. select * from eFI_RAT_FinancialInstrument_Rating;
  553. -- the latest view
  554. select * from llFI_FinancialInstrument;
  555. -- every day of the week in the test along changing time
  556. select * from plFI_FinancialInstrument('2011-03-15');
  557. select * from plFI_FinancialInstrument('2011-03-16');
  558. select * from plFI_FinancialInstrument('2011-03-17');
  559. select * from plFI_FinancialInstrument('2011-03-18');
  560. select * from plFI_FinancialInstrument('2011-03-19');
  561. select * from plFI_FinancialInstrument('2011-03-20');
  562. -- some interesting intersections
  563. select * from ppFI_FinancialInstrument('2011-03-18', '2011-03-20');
  564. select * from ppFI_FinancialInstrument('2011-03-18', '2011-03-21');
  565. select * from ppFI_FinancialInstrument('2011-03-19', '2011-03-20');
  566. select * from ppFI_FinancialInstrument('2011-03-19', '2011-03-21');
  567.  
  568. /* Our own simple test
  569.  
  570. Event 1, Arriving at 2005-09-10:
  571. - The rating of the instrument is 'A' from 2001-01-01.
  572. Event 2, Arriving at 2005-10-12:
  573. - The rating of the instrument was actually 'B' from 2001-01-01.
  574. Event 3, Arriving at 2005-10-20:
  575. - The rating of the instrument has been set to 'F' from 2003-02-13.
  576. Event 4, Arriving at 2005-11-09:
  577. - The rating of the instrument was set to 'D' at 2002-04-10.
  578. Event 5, Also arriving at 2005-11-09:
  579. - The rating of the instrument was then changed again to 'E' at 2002-08-20.
  580. Event 6, Arriving at 2006-09-21:
  581. - The rating was never set to 'F', it was an error.
  582.  
  583. */
  584.  
  585. -- create another financial instrument
  586. insert into FI_FinancialInstrument (Metadata_FI) values (42);
  587.  
  588. -- EVENT 1
  589. insert into tFI_RAT_FinancialInstrument_Rating (
  590.     [FI_ID],
  591.     [FI_RAT_FinancialInstrument_Rating],
  592.     [FI_RAT_ChangedAt],
  593.     [FI_RAT_RecordedAt],
  594.     [FI_RAT_Reliability],
  595.     [Metadata_FI_RAT]
  596. ) values (2, 'A', '2001-01-01', '2005-09-10', 1, 555);
  597.  
  598. -- all rows in the attribute table
  599. select * from tFI_RAT_FinancialInstrument_Rating;
  600. select * from FI_RAT_FinancialInstrument_Rating;
  601. select * from eFI_RAT_FinancialInstrument_Rating;
  602. -- the latest view
  603. select * from llFI_FinancialInstrument;
  604. -- before and after
  605. select * from plFI_FinancialInstrument('2000-12-31');
  606. select * from plFI_FinancialInstrument('2001-01-02');
  607.  
  608.  
  609. -- EVENT 2
  610. insert into tFI_RAT_FinancialInstrument_Rating (
  611.     [FI_ID],
  612.     [FI_RAT_ChangedAt],
  613.     [FI_RAT_ErasedAt],
  614.     [FI_RAT_Reliability],
  615.     [Metadata_FI_RAT]
  616. ) values (2, '2001-01-01', '2005-10-12', 0, 556);
  617. insert into tFI_RAT_FinancialInstrument_Rating (
  618.     [FI_ID],
  619.     [FI_RAT_FinancialInstrument_Rating],
  620.     [FI_RAT_ChangedAt],
  621.     [FI_RAT_RecordedAt],
  622.     [FI_RAT_Reliability],
  623.     [Metadata_FI_RAT]
  624. ) values (2, 'B', '2001-01-01', '2005-10-12', 1, 556);
  625.  
  626. -- all rows in the attribute table
  627. select * from tFI_RAT_FinancialInstrument_Rating;
  628. select * from FI_RAT_FinancialInstrument_Rating;
  629. select * from eFI_RAT_FinancialInstrument_Rating;
  630. -- the latest view
  631. select * from llFI_FinancialInstrument;
  632. -- before and after
  633. select * from plFI_FinancialInstrument('2000-12-31');
  634. select * from plFI_FinancialInstrument('2001-01-02');
  635. -- before and after in retrospect
  636. select * from ppFI_FinancialInstrument('2000-12-31', '2005-09-22');
  637. select * from ppFI_FinancialInstrument('2001-01-02', '2005-09-22');
  638.  
  639.  
  640. -- EVENT 3
  641. insert into tFI_RAT_FinancialInstrument_Rating (
  642.     [FI_ID],
  643.     [FI_RAT_FinancialInstrument_Rating],
  644.     [FI_RAT_ChangedAt],
  645.     [FI_RAT_RecordedAt],
  646.     [FI_RAT_Reliability],
  647.     [Metadata_FI_RAT]
  648. ) values (2, 'F', '2003-02-13', '2005-10-20', 1, 557);
  649.  
  650. -- all rows in the attribute table
  651. select * from tFI_RAT_FinancialInstrument_Rating;
  652. select * from FI_RAT_FinancialInstrument_Rating;
  653. select * from eFI_RAT_FinancialInstrument_Rating;
  654. -- the latest view
  655. select * from llFI_FinancialInstrument;
  656.  
  657.  
  658. -- EVENT 4
  659. insert into tFI_RAT_FinancialInstrument_Rating (
  660.     [FI_ID],
  661.     [FI_RAT_FinancialInstrument_Rating],
  662.     [FI_RAT_ChangedAt],
  663.     [FI_RAT_RecordedAt],
  664.     [FI_RAT_Reliability],
  665.     [Metadata_FI_RAT]
  666. ) values (2, 'D', '2002-04-10', '2005-11-09', 1, 558);
  667.  
  668. -- all rows in the attribute table
  669. select * from tFI_RAT_FinancialInstrument_Rating;
  670. select * from FI_RAT_FinancialInstrument_Rating;
  671. select * from eFI_RAT_FinancialInstrument_Rating;
  672. -- the latest view
  673. select * from llFI_FinancialInstrument;
  674. -- before
  675. select * from plFI_FinancialInstrument('2003-02-12');
  676.  
  677.  
  678. -- EVENT 5
  679. insert into tFI_RAT_FinancialInstrument_Rating (
  680.     [FI_ID],
  681.     [FI_RAT_FinancialInstrument_Rating],
  682.     [FI_RAT_ChangedAt],
  683.     [FI_RAT_RecordedAt],
  684.     [FI_RAT_Reliability],
  685.     [Metadata_FI_RAT]
  686. ) values (2, 'E', '2002-08-20', '2005-11-09', 1, 559);
  687.  
  688. -- all rows in the attribute table
  689. select * from tFI_RAT_FinancialInstrument_Rating;
  690. select * from FI_RAT_FinancialInstrument_Rating;
  691. select * from eFI_RAT_FinancialInstrument_Rating;
  692. -- the latest view
  693. select * from llFI_FinancialInstrument;
  694. -- before
  695. select * from plFI_FinancialInstrument('2003-02-12');
  696.  
  697.  
  698. -- EVENT 6
  699. insert into tFI_RAT_FinancialInstrument_Rating (
  700.     [FI_ID],
  701.     [FI_RAT_ChangedAt],
  702.     [FI_RAT_ErasedAt],
  703.     [FI_RAT_Reliability],
  704.     [Metadata_FI_RAT]
  705. ) values (2, '2003-02-13', '2006-09-21', 0, 557);
  706.  
  707. -- all rows in the attribute table
  708. select * from tFI_RAT_FinancialInstrument_Rating;
  709. select * from FI_RAT_FinancialInstrument_Rating;
  710. select * from eFI_RAT_FinancialInstrument_Rating;
  711. -- the latest view
  712. select * from llFI_FinancialInstrument;
  713. -- before
  714. select * from plFI_FinancialInstrument('2003-02-12');
  715. -- latest in retrospect
  716. select * from ppFI_FinancialInstrument(GETDATE(), '2006-09-20');
  717. select * from ppFI_FinancialInstrument(GETDATE(), '2005-10-12');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement