元宵节_上元节_元宵

元宵佳节看Oracle技术粉们用SQL

发布时间:2016-11-28 19:13:05   点击数:

话团圆,画团圆,元宵佳节倍思亲,可是大家知道吗,万能的SQL可以帮助大家绘制团圆。

在ITPUB论坛里,一群SQL爱好者们会用SQL来描摹一切可能。请看如下这段SQL,为大家绘制了团团圆圆的五连环:

withaas(slctdistinctround(a.x+b.x)x,round(a.y+b.y)yfrom

(slct(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,cos(n/30*3.)*2x,

sin(n/30*3.)y

from(slctrownum-1nfromall_objctswhrrownum=30+30)))a,

(slctn,(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(m/3*3.)*2*15x,

sin(m/3*3.)*15y

from(slctcaswhnrownum=2thn3

whnrownum=3thn-2ls-6ndm,rownum-1n

fromall_objctswhrrownum=5)))b

)

slctrplac(sys_connct_by_path(point,/),/,null)star

from(slctb.y,b.x,dcod(a.x,null,,*)point

froma,

(slct*

from(slctrownum-1+(slctmin(x)froma)x

fromall_objcts

whrrownum=(slctmax(x)-min(x)+1froma)),

(slctrownum-1+(slctmin(y)froma)y

fromall_objcts

whrrownum=(slctmax(y)-min(y)+1froma)))b

whra.x(+)=b.x

anda.y(+)=b.y)

whrx=(slctmax(x)froma)

startwithx=(slctmin(x)froma)

connctbyy=priory

andx=priorx+1;

这段SQL在Oracl中输出了下图,请用SQL执行:

好吧,这是五个连环,事实上是奥运会的五环旗,在庆祝奥运期间,网友nyfor的随手创作。

再看如下一段SQL,则是输出了一个五角星:

withaas(

slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/20)*(1-1/5)*3.)*2x,

sin(trunc(n/20)*(1-1/5)*3.)y

from(slctrownum-1nfromall_objctswhrrownum=20*5))

)

slctrplac(sys_connct_by_path(point,/),/,null)star

from(slctb.y,b.x,dcod(a.x,null,,*)point

froma,

(slct*

from(slctrownum-1+(slctmin(x)froma)x

fromall_objcts

whrrownum=(slctmax(x)-min(x)+1froma)),

(slctrownum-1+(slctmin(y)froma)y

fromall_objcts

whrrownum=(slctmax(y)-min(y)+1froma)))b

whra.x(+)=b.x

anda.y(+)=b.y)

whrx=(slctmax(x)froma)

startwithx=(slctmin(x)froma)

connctbyy=priory

andx=priorx+1;

这个SQL的解释如下:

其中数字20表示五角星每一条边上的点的个数(你也可以设置的大一些或小一些),其中的数字5表示五角星的边数,其中的数字2是为了调整横向字符间距与纵向行距之间的差异而设置的,你也可以不乘以这个2,这里只是为了输出稍微好看一些.

调整期中数字5,你还可以输出7角星,9角星....注意我的SQL不能输出6角星,8角星,因为我的SQL算法中是以一笔画能够画成的星为基础设计的算法的.

比如,以下是7角形输出:

在一轮讨论之后,nwkid大神给出了一个系列的SQL改写,小编就列举如下。

SQL一:

withaas(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/20)*(1-1/5)*3.)*2x,

sin(trunc(n/20)*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=20*5))

)

SELECTLPAD(REPLACE(SUM(POWER(10,x-1)),0,),(SELECTMAX(x)FROMa))ASstar

FROMa

GROUPBYy

ORDERBYy;

SQL二:

withaas(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/20)*(1-1/5)*3.)x,

sin(trunc(n/20)*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=20*5))

)

SELECTLPAD(REPLACE(SUM(POWER(10,x)),0,),(SELECTMAX(x)+1FROMa))ASstar

FROMa

GROUPBYy

ORDERBYy;

SQL三:

withaas(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/20)*(1-1/5)*3.)*2x,

sin(trunc(n/20)*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=20*5))

)

SELECTTRANSLATE(LPAD(NVL(SUM(POWER(10,CASEWHENx=40THENx-40END)),0),(SELECTMAX(x)-39FROMaWHEREx=40))

LPAD(SUM(POWER(10,CASEWHENx40THENxEND)),40)

,01,*

)

ASstar

FROMa

GROUPBYy

ORDERBYy;

SQL四:

withaas(SELECTx,y

,ROW_NUMBER()OVER(PARTITIONBYyORDERBYx)rn

,MAX(x)OVER(PARTITIONBYy)maxx

FROM(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/20)*(1-1/5)*3.)*2x,

sin(trunc(n/20)*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=20*5)

)

)

)

,t(rn,x,y,str,maxx)AS(

SELECT1,x,y,LPAD(*,x+1),maxxFROMaWHERErn=1

UNIONALL

SELECTa.rn,a.x,t.y,str

RPAD(,a.x-t.x-1)

*,t.maxx

FROMt,a

WHEREt.rn=a.rn-1ANDt.y=a.y

)CYCLEx,ySETcycl_flagTOYDEFAULTN

SELECTstrFROMtWHEREx=maxxORDERBYy;

SQL五:

VARSCALENUMBER;

EXEC:SCALE:=3;

withaas(SELECTx,y

,ROW_NUMBER()OVER(PARTITIONBYyORDERBYx)rn

,MAX(x)OVER(PARTITIONBYy)maxx

FROM(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/(10*:SCALE))*(1-1/5)*3.)*2x,

sin(trunc(n/(10*:SCALE))*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=10*:SCALE*5)

)

)

)

,t(rn,x,y,str,maxx)AS(

SELECT1,x,y,LPAD(*,x+1),maxxFROMaWHERErn=1

UNIONALL

SELECTa.rn,a.x,t.y,str

RPAD(,a.x-t.x-1)

*,t.maxx

FROMt,a

WHEREt.rn=a.rn-1ANDt.y=a.y

)CYCLEx,ySETcycl_flagTOYDEFAULTN

SELECTstrFROMtWHEREx=maxxORDERBYy;

SQL六-利用wmsys.wm_concat的写法其实更简单:

withaas(SELECTx,y

,LAG(x,1,0)OVER(PARTITIONBYyORDERBYx)last_x

FROM(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/(10*:SCALE))*(1-1/5)*3.)*2x,

sin(trunc(n/(10*:SCALE))*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=10*:SCALE*5)

)

)

)

SELECTREPLACE(MAX(str),,)STR

FROM(SELECTy,wmsys.wm_concat(LPAD(*,x-last_x))OVER(PARTITIONBYyORDERBYx)str

FROMa

)

GROUPBYy

ORDERBYy;

SQL之七-wmsys.wm_concat的connctby替代写法:

withaas(SELECTx,y

,LAG(x,1,0)OVER(PARTITIONBYyORDERBYx)last_x

,ROW_NUMBER()OVER(PARTITIONBYyORDERBYx)rn

FROM(slctdistinctround(sum(x)ovr(ordrbyn))x,

round(sum(y)ovr(ordrbyn))y

from(slctn,

cos(trunc(n/(10*:SCALE))*(1-1/5)*3.)*2x,

sin(trunc(n/(10*:SCALE))*(1-1/5)*3.)y

from(slctrownum-1nfromDUALCONNECTBYrownum=10*:SCALE*5)

)

)

)

SELECTREPLACE(MAX(str),,)STR

FROM(SELECTy,SYS_CONNECT_BY_PATH(LPAD(*,x-last_x),,)str

FROMa

STARTWITHrn=1

CONNECTBYy=PRIORyANDrn=PRIORrn+1

)

GROUPBYy

ORDERBYy;

SQL如神,学习入化,动手为王,祝愿大家元宵节快乐!

如何加入云和恩墨大讲堂







































台湾治疗白癜风最好的医院
治疗白癜风药物


转载请注明:http://www.jiaruitoys.com/fsxg/2528.html
------分隔线----------------------------