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

在ITPUB论坛里,一群SQL爱好者们会用SQL来描摹一切可能。请看如下这段SQL,为大家绘制了团团圆圆的五连环:with a as (select distinct round(a.x + b.x) x,round(a.y + b.y) y from

(select (sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n, cos(n/30 * 3.1415926)*2  x,

sin(n/30 * 3.1415926) y

from (select rownum - 1 n from all_objects where rownum <= 30 +30))) a,

(select n, (sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

sin( m /3 * 3.1415926)* 15 y

from (select case when rownum <= 2 then 3

when rownum = 3 then -2 else -6 end m, rownum - 1 n

from all_objects where rownum <= 5))) b

)

select replace(sys_connect_by_path(point, '/'), '/', null) star

from (select b.y, b.x, decode(a.x, null, ' ', '*') point

from a,

(select *

from (select rownum - 1 + (select min(x) from a) x

from all_objects

where rownum <= (select max(x) - min(x) + 1 from a)),

(select rownum - 1 + (select min(y) from a) y

from all_objects

where rownum <= (select max(y) - min(y) + 1 from a))) b

where a.x(+) = b.x

and a.y(+) = b.y)

where x = (select max(x) from a)

start with x = (select min(x) from a)

connect by y = prior y

and x = prior x + 1;

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

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

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

select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from all_objects where rownum <= 20 * 5))

)

select replace(sys_connect_by_path(point, '/'), '/', null) star

from (select b.y, b.x, decode(a.x, null, ' ', '*') point

from a,

(select *

from (select rownum - 1 + (select min(x) from a) x

from all_objects

where rownum <= (select max(x) - min(x) + 1 from a)),

(select rownum - 1 + (select min(y) from a) y

from all_objects

where rownum <= (select max(y) - min(y) + 1 from a))) b

where a.x(+) = b.x

and a.y(+) = b.y)

where x = (select max(x) from a)

start with x = (select min(x) from a)

connect by y = prior y

and x = prior x + 1;

这个SQL的解释如下:

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

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

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

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

SQL一:with a as ( select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 20 * 5))

)

SELECT LPAD(REPLACE(SUM(POWER(10,x-1)),'0',' '),(SELECT MAX(x) FROM a)) AS star

FROM a

GROUP BY y

ORDER BY y;

SQL二:with a as ( select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 20 * 5))

)

SELECT LPAD(REPLACE(SUM(POWER(10,x)),'0',' '),(SELECT MAX(x)+1 FROM a)) AS star

FROM a

GROUP BY y

ORDER BY y;

SQL三:with a as ( select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 20 * 5))

)

SELECT TRANSLATE(LPAD(NVL(SUM(POWER(10,CASE WHEN x>=40 THEN x-40 END)),0),(SELECT MAX(x)-39 FROM a WHERE x>=40))

||LPAD(SUM(POWER(10,CASE WHEN x<40 THEN x END)),40)

,'01',' *'

)

AS star

FROM a

GROUP BY y

ORDER BY y;

SQL四:with a as (SELECT x,y

,ROW_NUMBER() OVER(PARTITION BY y ORDER BY x) rn

,MAX(x) OVER(PARTITION BY y) maxx

FROM (select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 20 * 5)

)

)

)

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

SELECT 1,x,y,LPAD('*',x+1),maxx FROM a WHERE rn=1

UNION ALL

SELECT a.rn,a.x,t.y,str||RPAD(' ',a.x-t.x-1)||'*',t.maxx

FROM t,a

WHERE t.rn=a.rn-1 AND t.y=a.y

) CYCLE x,y SET cycle_flag TO 'Y' DEFAULT 'N'

SELECT str FROM t WHERE x=maxx ORDER BY y;

SQL五:VAR SCALE NUMBER;

EXEC :SCALE :=3;

with a as (SELECT x,y

,ROW_NUMBER() OVER(PARTITION BY y ORDER BY x) rn

,MAX(x) OVER(PARTITION BY y) maxx

FROM (select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 10*:SCALE * 5)

)

)

)

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

SELECT 1,x,y,LPAD('*',x+1),maxx FROM a WHERE rn=1

UNION ALL

SELECT a.rn,a.x,t.y,str||RPAD(' ',a.x-t.x-1)||'*',t.maxx

FROM t,a

WHERE t.rn=a.rn-1 AND t.y=a.y

) CYCLE x,y SET cycle_flag TO 'Y' DEFAULT 'N'

SELECT str FROM t WHERE x=maxx ORDER BY y;

SQL六 - 利用wmsys.wm_concat的写法其实更简单:with a as (SELECT x,y

,LAG(x,1,0) OVER(PARTITION BY y ORDER BY x) last_x

FROM (select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 10*:SCALE * 5)

)

)

)

SELECT REPLACE(MAX(str),',') STR

FROM (SELECT y,wmsys.wm_concat(LPAD('*',x-last_x)) OVER(PARTITION BY y ORDER BY x) str

FROM a

)

GROUP BY y

ORDER BY y;

SQL之七 - wmsys.wm_concat的connect by替代写法:with a as (SELECT x,y

,LAG(x,1,0) OVER(PARTITION BY y ORDER BY x) last_x

,ROW_NUMBER() OVER(PARTITION BY y ORDER BY x) rn

FROM (select distinct round(sum(x) over(order by n)) x,

round(sum(y) over(order by n)) y

from (select n,

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

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

from (select rownum - 1 n from DUAL CONNECT BY rownum <= 10*:SCALE * 5)

)

)

)

SELECT REPLACE(MAX(str),',') STR

FROM (SELECT y,SYS_CONNECT_BY_PATH(LPAD('*',x-last_x),',') str

FROM a

START WITH rn=1

CONNECT BY y=PRIOR y AND rn=PRIOR rn+1

)

GROUP BY y

ORDER BY y;

SQL如神,学习入化,动手为王,祝愿大家元宵节快乐!如何加入"云和恩墨大讲堂"微信群

oracle画圆,元宵佳节:看Oracle技术粉们用SQL画团圆相关推荐

  1. 怎么在地图上画圆 php,SOSO地图API使用(一)在地图上画圆实现思路与代码

    前言:最近在做SOSO地图相关开发,遇到相关画圆知识,特此简单记录下来. 1.在页面中添加SOSO地图API引用,引用脚本: : 2.新建一个地图DIV容器,如下: 3.初始化地图: var cent ...

  2. oracle的索引在哪儿看,Oracle如何查看索引和视图

    视图-–是由SELECT查询语句(可以是单表或者多表查询)定义的一个"逻辑表",只有定义而无数据,是一个"虚表". 在创建视图时,只是将视图的定义信息保存在数据 ...

  3. oracle 密码文件在哪里看,Oracle数据库密码文件的使用

    Oracle数据库密码文件的使用 日期:2010年5月21日 作者: 在Oracle数据库系统中,用户假设要以特权用户身份(INTERNAL/SYSDBA/SYSOPER)登录Oracle 数据库能够 ...

  4. oracle表分析效果怎么看,Oracle 索引与表分析几种方法

    分析表与索引有几种方法,现在列举出来参考下. 1.分析表与索引(analyze 不会重建索引) analyze table tablename compute statistics 等同于 analy ...

  5. python circle函数如何画圆_Python练习实例56 | 画图,学用circle画圆形

    作者还记得在抖音上,有不少的Python学习爱好者还能够用Python里面自带的工具,来画各种各样的图形. 一句话,秀的简直不行不行的. 小黄人.jpg 那么今天,我们也来开始用Python来画出第一 ...

  6. python3画圆、直线_Bresenham直线算法与画圆算法

    在我们内部开发使用的一个工具中,我们需要几乎从 0 开始实现一个高效的二维图像渲染引擎.比较幸运的是,我们只需要画直线.圆以及矩形,其中比较复杂的是画直线和圆.画直线和圆已经有非常多的成熟的算法了,我 ...

  7. python用正方形画圆_Python 用turtle实现用正方形画圆的例子

    最近发现一个很有意思的画图的python库,叫做turtle,这里先说下用turtle这个库来实现用正方形画圆的思路. 每次都用乌龟(turtle) 来画出一个正方形,然后通过旋转3°后,继续画一样的 ...

  8. 三角函数对应在平面坐标上画圆

    目录 1.圆半径(r)为 : 1 2.圆半径(r)为 : r 3.例程 三角函数(Trigonometric Functions)是基本初等函数之一,是以角度(数学上最常用弧度制,下同)为自变量,角度 ...

  9. ABB机器人画圆编程_ABB工业机器人(条件执行数字信号判断,画方or画圆)

    一.前戏 条件:从安全点,到工具区域夹取工具(笔),到工作区域,判断数字信号 Di1 =1 ,Ture :画方,False:画圆,回到工具区域放下工具(笔),回到安全点 二. 准备工作 校准tcp工具 ...

最新文章

  1. kivy python 读取oracle数据库_Kivy和Python线程-如何在它们之间获取数据
  2. python文件操作举例
  3. 十八、中断之独立按键
  4. ORA-04028: cannot generate diana for object xxx
  5. matlab rootdir,Python cfg.ROOT_DIR属性代码示例
  6. 使用data attributes
  7. Windroy Lets Android run on Windows systems-- 国外androids 虚拟系统分享
  8. 轻量级网页安全漏洞扫描工具-Wapiti
  9. java开发入行真功夫pdf_Visual C++开发入行真功夫
  10. Windows下设置Mongodb用户名密码
  11. matlab c盘空间,手动清理C盘空间
  12. huawei.xmind
  13. Docker - 配置国内加速器加速镜像下载
  14. Kuberneters企业级容器云平台落地实践之二
  15. 关于redis服务的代码编码
  16. 1月末支付机构备付金总量达1.4万亿,较去年12月下滑两千多亿
  17. Vue学习 — 详解Vue生命周期
  18. Win11下载和安装T3标准版11.2
  19. OpenCV Java 实现票据、纸张的四边形边缘检测与提取、摆正
  20. 海康RTSP流转RTMP并推送至Web端展示

热门文章

  1. javascript设计模式_Javascript 前端设计模式
  2. 如何识别南孚电池是号码
  3. ESP32彩屏开发板(WT32-SC01),除了买买买,你还可以参与一起设计了
  4. android 上传html文件大小,浅谈关于Android WebView上传文件的解决方案
  5. Zynq-7000系统公共资源及特性
  6. 计算机应用基础测试成绩截图,计算机应用基础测试题图文稿.docx
  7. 为什么linux自带python_为什么一些linux自带python,而不是C,C++,java等其他编程语言?...
  8. 计算机科学与技术专业导论_教育部最新公布!西安工业大学新增4个本科专业!...
  9. oracle数据库动态拼接查询条件解决方案
  10. Educational Codeforces Round 64(Unrated for Div.1+Div. 2)