Advertisement
anchormodeling

Bitemporal Anchor Modeling 1C1R One Table

Aug 23rd, 2011
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 14.77 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
  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.     [FI_RAT_Reliability] [tinyint] NOT NULL,
  26.     [Metadata_FI_RAT] [int] NOT NULL,
  27.     PRIMARY KEY CLUSTERED (
  28.         [FI_ID] ASC,
  29.         [FI_RAT_ChangedAt] DESC,
  30.         [FI_RAT_RecordedAt] DESC,
  31.         [FI_RAT_Reliability] DESC
  32.     ),
  33.     FOREIGN KEY ([FI_ID]) REFERENCES [dbo].[FI_FinancialInstrument] ([FI_ID])
  34. );
  35.  
  36. GO
  37.  
  38. /*
  39.     There are nine 'perspectives' of the information resulting
  40.     from latest, point-in-time, and interval perspectives
  41.     taken over the two timelines (changing and recording).
  42.     I will show three of them here.
  43. */
  44.  
  45. -- latest for changing time and latest for recording time
  46. CREATE VIEW [dbo].[llFI_FinancialInstrument]
  47. AS
  48. SELECT
  49.    [FI].FI_ID,
  50.    [FI].Metadata_FI,
  51.    [FI_RAT].FI_RAT_FinancialInstrument_Rating,
  52.    [FI_RAT].FI_RAT_ChangedAt,
  53.    [FI_RAT].FI_RAT_RecordedAt,
  54.    [FI_RAT].FI_RAT_Reliability,
  55.    [FI_RAT].Metadata_FI_RAT
  56. FROM
  57.    [dbo].[FI_FinancialInstrument] [FI]
  58. LEFT JOIN
  59.    [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  60. ON
  61.    [FI_RAT].FI_ID = [FI].FI_ID
  62. AND
  63.    [FI_RAT].FI_RAT_ChangedAt = (
  64.       SELECT
  65.          max(sub.FI_RAT_ChangedAt)
  66.       FROM
  67.          [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  68.       WHERE
  69.          sub.FI_ID = [FI].FI_ID
  70.       AND
  71.         (
  72.             select top 1
  73.                 rel.FI_RAT_Reliability
  74.             from
  75.                 [dbo].[FI_RAT_FinancialInstrument_Rating] rel
  76.             where
  77.                 rel.FI_ID = [FI].FI_ID
  78.             and
  79.                 rel.FI_RAT_ChangedAt = sub.FI_RAT_ChangedAt
  80.             order by
  81.                 rel.FI_RAT_RecordedAt desc,
  82.                 rel.FI_RAT_Reliability desc
  83.          ) > 0
  84.     )
  85. AND
  86.    [FI_RAT].FI_RAT_RecordedAt = (
  87.         SELECT
  88.             MAX(sub.FI_RAT_RecordedAt)
  89.         FROM
  90.             [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  91.         WHERE
  92.             sub.FI_ID = [FI_RAT].FI_ID
  93.         AND
  94.             sub.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  95.     )
  96. AND
  97.     [FI_RAT].FI_RAT_Reliability = (
  98.         SELECT
  99.             MAX(sub.FI_RAT_Reliability)
  100.         FROM
  101.             [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  102.         WHERE
  103.             sub.FI_ID = [FI_RAT].FI_ID
  104.         AND
  105.             sub.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  106.         AND
  107.             sub.FI_RAT_RecordedAt = [FI_RAT].FI_RAT_RecordedAt     
  108.     );
  109.  
  110. GO
  111.  
  112. -- point-in-time for changing time and latest for recording time
  113. CREATE FUNCTION [dbo].[plFI_FinancialInstrument] (
  114.     @timepoint datetime
  115. )
  116. RETURNS TABLE
  117. RETURN
  118. SELECT
  119.    [FI].FI_ID,
  120.    [FI].Metadata_FI,
  121.    [FI_RAT].FI_RAT_FinancialInstrument_Rating,
  122.    [FI_RAT].FI_RAT_ChangedAt,
  123.    [FI_RAT].FI_RAT_RecordedAt,
  124.    [FI_RAT].FI_RAT_Reliability,
  125.    [FI_RAT].Metadata_FI_RAT
  126. FROM
  127.    [dbo].[FI_FinancialInstrument] [FI]
  128. LEFT JOIN
  129.    [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  130. ON
  131.    [FI_RAT].FI_ID = [FI].FI_ID
  132. AND
  133.    [FI_RAT].FI_RAT_ChangedAt = (
  134.       SELECT
  135.          max(sub.FI_RAT_ChangedAt)
  136.       FROM
  137.          [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  138.       WHERE
  139.          sub.FI_ID = [FI].FI_ID
  140.       AND
  141.          sub.FI_RAT_ChangedAt <= @timepoint
  142.       AND
  143.         (
  144.             select top 1
  145.                 rel.FI_RAT_Reliability
  146.             from
  147.                 [dbo].[FI_RAT_FinancialInstrument_Rating] rel
  148.             where
  149.                 rel.FI_ID = [FI].FI_ID
  150.             and
  151.                 rel.FI_RAT_ChangedAt = sub.FI_RAT_ChangedAt
  152.             order by
  153.                 rel.FI_RAT_RecordedAt desc,
  154.                 rel.FI_RAT_Reliability desc
  155.          ) > 0
  156.     )
  157. AND
  158.    [FI_RAT].FI_RAT_RecordedAt = (
  159.         SELECT
  160.             MAX(sub.FI_RAT_RecordedAt)
  161.         FROM
  162.             [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  163.         WHERE
  164.             sub.FI_ID = [FI_RAT].FI_ID
  165.         AND
  166.             sub.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  167.     )
  168. AND
  169.     [FI_RAT].FI_RAT_Reliability = (
  170.         SELECT
  171.             MAX(sub.FI_RAT_Reliability)
  172.         FROM
  173.             [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  174.         WHERE
  175.             sub.FI_ID = [FI_RAT].FI_ID
  176.         AND
  177.             sub.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  178.         AND
  179.             sub.FI_RAT_RecordedAt = [FI_RAT].FI_RAT_RecordedAt     
  180.     );
  181.  
  182. GO
  183.  
  184. -- point-in-time for changing time and point-in-time for recording time
  185. CREATE FUNCTION [dbo].[ppFI_FinancialInstrument] (
  186.     @changingTimepoint datetime,
  187.     @recordingTimepoint 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].FI_RAT_Reliability,
  198.    [FI_RAT].Metadata_FI_RAT
  199. FROM
  200.    [dbo].[FI_FinancialInstrument] [FI]
  201. LEFT JOIN
  202.    [dbo].[FI_RAT_FinancialInstrument_Rating] [FI_RAT]
  203. ON
  204.    [FI_RAT].FI_ID = [FI].FI_ID
  205. AND
  206.    [FI_RAT].FI_RAT_ChangedAt = (
  207.       SELECT
  208.          max(sub.FI_RAT_ChangedAt)
  209.       FROM
  210.          [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  211.       WHERE
  212.          sub.FI_ID = [FI].FI_ID
  213.       AND
  214.          sub.FI_RAT_ChangedAt <= @changingTimepoint
  215.       AND
  216.         (
  217.             select top 1
  218.                 rel.FI_RAT_Reliability
  219.             from
  220.                 [dbo].[FI_RAT_FinancialInstrument_Rating] rel
  221.             where
  222.                 rel.FI_ID = [FI].FI_ID
  223.             and
  224.                 rel.FI_RAT_ChangedAt = sub.FI_RAT_ChangedAt
  225.             and
  226.                 rel.FI_RAT_RecordedAt <= @recordingTimepoint
  227.             order by
  228.                 rel.FI_RAT_RecordedAt desc,
  229.                 rel.FI_RAT_Reliability desc
  230.          ) > 0
  231.     )
  232. AND
  233.    [FI_RAT].FI_RAT_RecordedAt = (
  234.         SELECT
  235.             MAX(sub.FI_RAT_RecordedAt)
  236.         FROM
  237.             [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  238.         WHERE
  239.             sub.FI_ID = [FI_RAT].FI_ID
  240.         AND
  241.             sub.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  242.         AND
  243.             sub.FI_RAT_RecordedAt <= @recordingTimepoint
  244.     )
  245. AND
  246.     [FI_RAT].FI_RAT_Reliability = (
  247.         SELECT
  248.             MAX(sub.FI_RAT_Reliability)
  249.         FROM
  250.             [dbo].[FI_RAT_FinancialInstrument_Rating] sub
  251.         WHERE
  252.             sub.FI_ID = [FI_RAT].FI_ID
  253.         AND
  254.             sub.FI_RAT_ChangedAt = [FI_RAT].FI_RAT_ChangedAt
  255.         AND
  256.             sub.FI_RAT_RecordedAt = [FI_RAT].FI_RAT_RecordedAt     
  257.     );
  258.  
  259.  
  260. GO
  261.  
  262. -- create one financial instrument
  263. insert into FI_FinancialInstrument (Metadata_FI) values (42);
  264.  
  265. -- we should have no information whatsoever yet
  266. delete from FI_RAT_FinancialInstrument_Rating;
  267. select * from FI_RAT_FinancialInstrument_Rating;
  268. select * from llFI_FinancialInstrument;
  269.  
  270.  
  271. -- DAY 1
  272. -- insert the first ratings for the created financial instrument
  273. insert into FI_RAT_FinancialInstrument_Rating
  274. values (1, 'A', '2011-03-17', '2011-03-17', 1, 42);
  275. insert into FI_RAT_FinancialInstrument_Rating
  276. values (1, '?', '2011-03-19', '2011-03-17', 1, 42);
  277.  
  278. -- all rows in the attribute table
  279. select * from FI_RAT_FinancialInstrument_Rating;
  280. -- the latest view
  281. select * from llFI_FinancialInstrument;
  282. -- every day of the week in the test along changing time
  283. select * from plFI_FinancialInstrument('2011-03-15');
  284. select * from plFI_FinancialInstrument('2011-03-16');
  285. select * from plFI_FinancialInstrument('2011-03-17');
  286. select * from plFI_FinancialInstrument('2011-03-18');
  287. select * from plFI_FinancialInstrument('2011-03-19');
  288. select * from plFI_FinancialInstrument('2011-03-20');
  289.  
  290.  
  291. -- DAY 2
  292. -- Note that the following requires user knowledge of existing data
  293. insert into FI_RAT_FinancialInstrument_Rating
  294. values (1, 'A', '2011-03-17', '2011-03-18', 0, 42);
  295. insert into FI_RAT_FinancialInstrument_Rating
  296. values (1, 'B', '2011-03-16', '2011-03-18', 1, 42);
  297. insert into FI_RAT_FinancialInstrument_Rating
  298. values (1, 'A', '2011-03-18', '2011-03-18', 1, 42);
  299.  
  300. -- all rows in the attribute table
  301. select * from FI_RAT_FinancialInstrument_Rating;
  302. -- the latest view
  303. select * from llFI_FinancialInstrument;
  304. -- every day of the week in the test along changing time
  305. select * from plFI_FinancialInstrument('2011-03-15');
  306. select * from plFI_FinancialInstrument('2011-03-16');
  307. select * from plFI_FinancialInstrument('2011-03-17');
  308. select * from plFI_FinancialInstrument('2011-03-18');
  309. select * from plFI_FinancialInstrument('2011-03-19');
  310. select * from plFI_FinancialInstrument('2011-03-20');
  311. -- some interesting intersections
  312. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  313. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  314.  
  315.  
  316. -- DAY 3
  317. insert into FI_RAT_FinancialInstrument_Rating
  318. values (1, 'B', '2011-03-16', '2011-03-19', 0, 42);
  319. insert into FI_RAT_FinancialInstrument_Rating
  320. values (1, 'A', '2011-03-18', '2011-03-19', 0, 42);
  321. insert into FI_RAT_FinancialInstrument_Rating
  322. values (1, '?', '2011-03-19', '2011-03-19', 0, 42);
  323. insert into FI_RAT_FinancialInstrument_Rating
  324. values (1, 'C', '2011-03-15', '2011-03-19', 1, 42);
  325. insert into FI_RAT_FinancialInstrument_Rating
  326. values (1, '?', '2011-03-20', '2011-03-19', 1, 42);
  327.  
  328. -- all rows in the attribute table
  329. select * from FI_RAT_FinancialInstrument_Rating;
  330. -- the latest view
  331. select * from llFI_FinancialInstrument;
  332. -- every day of the week in the test along changing time
  333. select * from plFI_FinancialInstrument('2011-03-15');
  334. select * from plFI_FinancialInstrument('2011-03-16');
  335. select * from plFI_FinancialInstrument('2011-03-17');
  336. select * from plFI_FinancialInstrument('2011-03-18');
  337. select * from plFI_FinancialInstrument('2011-03-19');
  338. select * from plFI_FinancialInstrument('2011-03-20');
  339. -- some interesting intersections
  340. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  341. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  342. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-19');
  343.  
  344.  
  345. -- DAY 4
  346. insert into FI_RAT_FinancialInstrument_Rating
  347. values (1, '?', '2011-03-20', '2011-03-20', 0, 42);
  348. insert into FI_RAT_FinancialInstrument_Rating
  349. values (1, 'D', '2011-03-17', '2011-03-20', 1, 42);
  350.  
  351. -- all rows in the attribute table
  352. select * from FI_RAT_FinancialInstrument_Rating;
  353. -- the latest view
  354. select * from llFI_FinancialInstrument;
  355. -- every day of the week in the test along changing time
  356. select * from plFI_FinancialInstrument('2011-03-15');
  357. select * from plFI_FinancialInstrument('2011-03-16');
  358. select * from plFI_FinancialInstrument('2011-03-17');
  359. select * from plFI_FinancialInstrument('2011-03-18');
  360. select * from plFI_FinancialInstrument('2011-03-19');
  361. select * from plFI_FinancialInstrument('2011-03-20');
  362. -- some interesting intersections
  363. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-17');
  364. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-18');
  365. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-19');
  366. select * from ppFI_FinancialInstrument('2011-03-17', '2011-03-20');
  367.  
  368.  
  369. -- DAY 5
  370. insert into FI_RAT_FinancialInstrument_Rating
  371. values (1, 'E', '2011-03-18', '2011-03-21', 1, 42);
  372. insert into FI_RAT_FinancialInstrument_Rating
  373. values (1, 'D', '2011-03-20', '2011-03-21', 1, 42);
  374.  
  375. -- all rows in the attribute table
  376. select * from FI_RAT_FinancialInstrument_Rating;
  377. -- the latest view
  378. select * from llFI_FinancialInstrument;
  379. -- every day of the week in the test along changing time
  380. select * from plFI_FinancialInstrument('2011-03-15');
  381. select * from plFI_FinancialInstrument('2011-03-16');
  382. select * from plFI_FinancialInstrument('2011-03-17');
  383. select * from plFI_FinancialInstrument('2011-03-18');
  384. select * from plFI_FinancialInstrument('2011-03-19');
  385. select * from plFI_FinancialInstrument('2011-03-20');
  386. -- some interesting intersections
  387. select * from ppFI_FinancialInstrument('2011-03-18', '2011-03-20');
  388. select * from ppFI_FinancialInstrument('2011-03-18', '2011-03-21');
  389. select * from ppFI_FinancialInstrument('2011-03-19', '2011-03-20');
  390. select * from ppFI_FinancialInstrument('2011-03-19', '2011-03-21');
  391.  
  392.  
  393. /* Our own simple test
  394.  
  395. Event 1, Arriving at 2005-09-10:
  396. - The rating of the instrument is 'A' from 2001-01-01.
  397. Event 2, Arriving at 2005-10-12:
  398. - The rating of the instrument was actually 'B' from 2001-01-01.
  399. Event 3, Arriving at 2005-10-20:
  400. - The rating of the instrument has been set to 'F' from 2003-02-13.
  401. Event 4, Arriving at 2005-11-09:
  402. - The rating of the instrument was set to 'D' at 2002-04-10.
  403. Event 5, Also arriving at 2005-11-09:
  404. - The rating of the instrument was then changed again to 'E' at 2002-08-20.
  405. Event 6, Arriving at 2006-09-21:
  406. - The rating was never set to 'F', it was an error.
  407.  
  408. */
  409.  
  410. -- create another financial instrument
  411. insert into FI_FinancialInstrument (Metadata_FI) values (42);
  412.  
  413. -- EVENT 1
  414. insert into FI_RAT_FinancialInstrument_Rating
  415. values (2, 'A', '2001-01-01', '2005-09-10', 1, 555);
  416.  
  417. -- all rows in the attribute table
  418. select * from FI_RAT_FinancialInstrument_Rating;
  419. -- the latest view
  420. select * from llFI_FinancialInstrument;
  421. -- before and after
  422. select * from plFI_FinancialInstrument('2000-12-31');
  423. select * from plFI_FinancialInstrument('2001-01-02');
  424.  
  425.  
  426. -- EVENT 2
  427. insert into FI_RAT_FinancialInstrument_Rating
  428. values (2, 'A', '2001-01-01', '2005-10-12', 0, 556);
  429. insert into FI_RAT_FinancialInstrument_Rating
  430. values (2, 'B', '2001-01-01', '2005-10-12', 1, 556);
  431.  
  432. -- all rows in the attribute table
  433. select * from FI_RAT_FinancialInstrument_Rating;
  434. -- the latest view
  435. select * from llFI_FinancialInstrument;
  436. -- before and after
  437. select * from plFI_FinancialInstrument('2000-12-31');
  438. select * from plFI_FinancialInstrument('2001-01-02');
  439. -- before and after in retrospect
  440. select * from ppFI_FinancialInstrument('2000-12-31', '2005-09-22');
  441. select * from ppFI_FinancialInstrument('2001-01-02', '2005-09-22');
  442.  
  443.  
  444. -- EVENT 3
  445. insert into FI_RAT_FinancialInstrument_Rating
  446. values (2, 'F', '2003-02-13', '2005-10-20', 1, 557);
  447.  
  448. -- all rows in the attribute table
  449. select * from FI_RAT_FinancialInstrument_Rating;
  450. -- the latest view
  451. select * from llFI_FinancialInstrument;
  452.  
  453.  
  454. -- EVENT 4
  455. insert into FI_RAT_FinancialInstrument_Rating
  456. values (2, 'D', '2002-04-10', '2005-11-09', 1, 558);
  457.  
  458. -- all rows in the attribute table
  459. select * from FI_RAT_FinancialInstrument_Rating;
  460. -- the latest view
  461. select * from llFI_FinancialInstrument;
  462. -- before
  463. select * from plFI_FinancialInstrument('2003-02-12');
  464.  
  465.  
  466. -- EVENT 5
  467. insert into FI_RAT_FinancialInstrument_Rating
  468. values (2, 'E', '2002-08-20', '2005-11-09', 1, 559);
  469.  
  470. -- all rows in the attribute table
  471. select * from FI_RAT_FinancialInstrument_Rating;
  472. -- the latest view
  473. select * from llFI_FinancialInstrument;
  474. -- before
  475. select * from plFI_FinancialInstrument('2003-02-12');
  476.  
  477.  
  478. -- EVENT 6
  479. insert into FI_RAT_FinancialInstrument_Rating
  480. values (2, 'F', '2003-02-13', '2006-09-21', 0, 557);
  481.  
  482. -- all rows in the attribute table
  483. select * from FI_RAT_FinancialInstrument_Rating;
  484. -- the latest view
  485. select * from llFI_FinancialInstrument;
  486. -- before
  487. select * from plFI_FinancialInstrument('2003-02-12');
  488. -- latest in retrospect
  489. select * from ppFI_FinancialInstrument(GETDATE(), '2006-09-20');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement