SQL 難點解決 序列生成

2021-08-29 05:31:52 字數 4404 閱讀 9128

1、    生成連續整數序列

mysql8:  with recursive t(n) as (

select 1

union all

select n+1 from t where n<7

select * from t;

oracle:select level n

from dual connect by level<=7;

spl:

a=to(1,7)

a1:構造從 1 到 7 的整數序列

示例 1:百雞問題,雞翁一值錢五,雞母一值錢三,雞雛三值錢一。百錢買百雞,問雞翁、母、雛各幾

mysql8:  

with recursive jg(n) as (select 1 union all select n+1 from jg where n<100/5),

jm(n) as (select 1 union all select n+1 from jm where n<100/3),

jc(n) as (select 3 union all select n+3 from jc where n<98)

select jg.n jw, jm.n jm, jc.n jc

from jg cross join jm cross join jc

where jg.n*5+jm.n*3+jc.n/3=100 and jg.n+jm.n+jc.n=100

spl:

a=to(100/5)

=to(100/3)

=33.(~*3)

=create(jw,jm,jc)

>a1.run(a2.run(a3.run(if(a1.~+a2.~+a3.~==100   && a1.~*5+a2.~*3+a3.~/3==100,a4.insert(0,a1.~,a2.~,a3.~)))))

a1:構造1到20的整數序列

a2:構造1到33的整數序列

a3:構造1到99且步長為3的整數序列

a4:建立資料結構為(jw,jm,jc)的序表

a5:對a1、a2、a3的資料進行巢狀迴圈,若滿足於a1成員+a2成員+a3成員==100且a1成員*5+a2成員*3+a3成員/3==100則追加到a4序表中

示例2:將指定列中冒號分隔的串劃分成多行

oracle:

with t(k,f) as (select 1 , 'a1:a2:a3' from dual

union all select 2, 'b1:b2' from dual),

t1 as (select k,f, length(f)-length(replace(f,':',''))+1 cnt from t),

t2 as (select level n from dual connect by level<=(select max(cnt) from t1)),

t3 as (select t1.k, t1.f, n, cnt,

case when n=1 then 1 else instr(f,':',1,n-1)+1 end p1, 

case when n=cnt then length(f)+1 else instr(f,':',1,n) end p2

from t1 join t2 on t2.n<=t1.cnt)

select k,substr(f,p1,p2-p1) f from t3 order by k;

spl:

a=create(k,f).record([1,"a1:a2:a3",2,"b1:b2"])

>a1.run(f=f.split(":"))

=a1.(f.new(a1.k:k,   ~:f))

=a3.conj()

a1:建立資料結構為(k,f)的序表,並追加2條記錄(1, 「a1:a2:a3)和(2,」b1:b2」)

a2:將a1的字段f用冒號劃分成序列並重新賦值給字段f

a3:針對a1每條記錄構造資料結構為(k,f)的序表,並根據欄位f中成員構造記錄(a1.k,f成員)追加到此序表中

2、    生成連續日期序列

mysql8:

with recursive

t(d) as (select date'2018-10-03'

union all

select d+1 from t where dselect d,dayofweek(d) w from t;

spl:

a=periods("2018-10-03",   "2018-10-09")

a1:生成2018-10-03到2018-10-09的日期序列

示例:列出2015-01-03到2015-01-07每天的銷量彙總

mysql8:

with recursive

t(d,v) as (select date'2015-01-04',30

union all select date'2015-01-06',50

union all select date'2015-01-07',50

union all select date'2015-01-03',40

union all select date'2015-01-04', 80),

s(d) as (select date'2015-01-03'

union all

select d+1 from s where dselect s.d, sum(t.v) v

from s left join t on s.d=t.d

group by s.d;

spl:

a[2015-01-04, 30,   2015-01-06,50, 2015-01-07,50, 2015-01-03,40, 2015-01-04,80]

=create(d,v).record(a1)

=periods("2015-01-03",   "2015-01-07")

=a2.align@a(a3,d)

=a4.new(a3(#):d,   ~.sum(v):v)

a4:a2中記錄按欄位d的值對齊到a3

a5:根據a4和a3對位構造統計後的序表

3、    生成連續的工作日(不包含週六週日)序列

mysql8:

with recursive

t(d) as (select date'2018-10-03'

union all

select d+1 from t where dselect d,dayofweek(d) w from t

where dayofweek(d)<=5;

spl:

a=workdays(date("2018-10-03"),   date("2018-10-09"))

=a1.new(~:d,day@w(~)-1:w)

a1:構造從2018-10-03到2018-10-09不包含週六週日的日期序列

a2:根據a1構造日期及相應週幾的序表

4、    根據序列生成表

PROTOTYPE模式難點解析

這個模式在c 中什麼時候才會使用,為什麼不直接new,大家只要明白乙個場景,就會知道它的用處。比如,現在讓你設計乙個遊戲,一共有8個關卡,每關的小兵有四種,但是第一關的小兵最弱,第八關的小兵最強,你怎麼設計?你可能認為這還不簡單,需要小兵的時候,直接new乙個相應的小兵,就可以了。但是這樣做會遇見乙...

面試疑難點解析

list,set,map,有什麼區別?list和set實際上市實現了collection介面,那麼collection介面的原理你能簡單描述一下嗎?list介面可以插入多個null值,並且重複值,而且list是乙個有序的集合。set是乙個不可重複的集合,裡面的元素都只出現,最多隻出現1次,並且set...

關於SVM的難點解讀

在各種對svm的講解中,有乙個知識點都講得不夠透徹 svm的目標函式是最大化支援向量的幾何間隔,但怎麼最後就變成了最小化法向量 斜率 了呢?可以想像一下,乙個超平面,斜率和截距以相同的倍數增大,這個超平面是不變的。也就是說,乙個固定的超平面的引數卻是不固定的。在我們求最優超平面時,解空間也就變成了無...