sql解惑 跟蹤投資組合問題

2021-08-29 21:57:26 字數 3931 閱讀 4044

這個謎底是給你的。 或許我是只見樹木, 不見森林, 但是如果想以一種不會導致無數迴圈引用的優雅方式來解決問題這個問題, 似乎並不容易。

這個謎底似乎是乙個關於完整的系統, 但我的問題是: 是否存在乙個方法, 在表設計階段消除明顯的迴圈引用。

create table portfolios

(file_id integer not null primary key,

issue_date date not null,

superseded_file_id integer not null references portfolios(file_id),

supersedes_file_id integer not null references portfolios(file_id)

);下面的謎底:

1.需要記錄那乙個投資組合代替了當前的投資組合

2.需要記錄這個投資組合替代的是那一種投資組合

3.需要能夠恢復某乙個投資組合(這個做的結論會替代乙個或一系列投資組合, 並導致迴圈引用。)

4.因為issue_date的存在,能夠記錄日期, 但那時如果恢復某個投資組合,那就又乙個難題!

5.不論select語句上的投資組合是什麼, 都能夠select出最新的投資組合。

6.需要能夠對一些的文件重新生成審計追蹤。

方法#1

因為在謎底的說明中無意表示式「前驅」和「後驅」的說法, 所以可知這個問題是處理序數計數的。讓我們應用已知的巢狀集合**代替它。

首先, 建立乙個表, 容納每個檔案的所有資訊:

create table portfolios(

file_id integer not null primary key,

other_stuff varchar2(40) not null

);向profolios表中插入資料

insert into portfolios values(222,'stuff');

insert into portfolios values(223,'old stuff');

insert into portfolios values(224,'new stuff');

insert into portfolios values(225,'borrowed stuff');

insert into portfolios values(322,'blue stuff');

insert into portfolios values(323,'purple stuff');

insert into portfolios values(324,'red stuff');

insert into portfolios values(325,'green stuff');

insert into portfolios values(999,'yellow stuff');

然後建立乙個表容納文件的後繼,幷包含兩個特殊的列: chain 和next

create table succession(

chain integer not null ,

next integer default 0 not null,

file_id integer not null references portfolios(file_id),

suc_date date not null,

primary key(chain, next)

);

向succession表中插入資料

insert into succession values(1,(select count(*) from succession where chain=1),222,to_date('2007-11-1','yyyy-mm-dd'));

insert into succession values(1,(select count(*) from succession where chain=1),223,to_date('2007-11-2','yyyy-mm-dd'));

insert into succession values(1,(select count(*) from succession where chain=1),224,to_date('2007-11-4','yyyy-mm-dd'));

insert into succession values(1,(select count(*) from succession where chain=1),225,to_date('2007-11-5','yyyy-mm-dd'));

insert into succession values(1,(select count(*) from succession where chain=1),999,to_date('2007-11-25','yyyy-mm-dd'));

insert into succession values(2,(select count(*) from succession where chain=2),322,to_date('2007-11-1','yyyy-mm-dd'));

insert into succession values(2,(select count(*) from succession where chain=2),323,to_date('2007-11-2','yyyy-mm-dd'));

insert into succession values(2,(select count(*) from succession where chain=2),324,to_date('2007-11-4','yyyy-mm-dd'));

insert into succession values(2,(select count(*) from succession where chain=2),322,to_date('2007-11-5','yyyy-mm-dd'));

insert into succession values(2,(select count(*) from succession where chain=2),323,to_date('2007-11-12','yyyy-mm-dd'));

insert into succession values(2,(select count(*) from succession where chain=2),999,to_date('2007-11-25','yyyy-mm-dd'));

選出最新組合,無論組合怎樣變化,我都能選出最新的組合:通過某個組合的max(next)找到file_id, 然後找到portfolios就可以

select distinct * from succession s, portfolios p

where s.file_id =p.file_id and next=

(select max(next) from succession s2

where s.chain=s2.chain);

完成審計功能: 通過chain, next排序就可以得到新順序的投資計畫了

select chain , next ,p1.file_id, other_stuff , suc_date

from succession s1,portfolios p1

where s1.file_id=p1.file_id

order by chain, next;

記錄這個投資組合組合替代哪個投資組合

select s1. file_id , f2.file_id

from succession s1, succession s2

where s1.chain=s2.chain

and s1.next=s2.next+1 and s1.file_id=999;

跟蹤使用者的SQL

serial sid addr sql id sql text v session 正在連線的回話 v stransaction 測試伺服器壓力大不大,代表還沒commit的dml操作 v lock 生命週期從dml語句開始,到dml語句結束 v sql 當前使用者的sql語句,sql id has...

投資統計查詢sql

select count from yyd users reginfo where regtime between 2016 07 11 00 00 00 and 2016 07 11 23 59 59 union all select count from yyd users reginfo wh...

SQL分組還原 SQL解惑讀書筆記 一

有如下的乙個表 createtableinventory goodschar 10 notnull,piecesint 11 notnull,primarykey goods engine innodbdefaultcharset utf8 有如下的資料 insertintoinventoryval...