首页 > 数据库技术 > 详细

SQL谜题(加减符号替代)

时间:2015-10-28 14:23:51      阅读:322      评论:0      收藏:0      [点我收藏+]
问题:将以下字符串”.1.2.3.4.5.6.7.8.9 = 1“中的符号点(.)更改为符号加(+)或符号(-),有多少种方法?请用SQL解决此问题
计算过程:

CREATE
TABLE #(VAL INT) INSERT INTO # SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 CREATE TABLE #1(VAL CHAR(1)) INSERT INTO #1 SELECT + UNION ALL SELECT - ; WITH A AS( SELECT #1.VAL VAL1,#.VAL FROM # JOIN #1 ON 1=1) SELECT RTRIM(A1.VAL1)+RTRIM(A1.VAL)+ RTRIM(A2.VAL1)+‘‘+RTRIM(A2.VAL)+ RTRIM(A3.VAL1)+‘‘+RTRIM(A3.VAL)+ RTRIM(A4.VAL1)+‘‘+RTRIM(A4.VAL)+ RTRIM(A5.VAL1)+‘‘+RTRIM(A5.VAL)+ RTRIM(A6.VAL1)+‘‘+RTRIM(A6.VAL)+ RTRIM(A7.VAL1)+‘‘+RTRIM(A7.VAL)+ RTRIM(A8.VAL1)+‘‘+RTRIM(A8.VAL)+ RTRIM(A9.VAL1)+‘‘+RTRIM(A9.VAL) Result FROM A A1,A A2,A A3,A A4,A A5,A A6,A A7,A A8,A A9 WHERE A1.VAL=1 AND A2.VAL=2 AND A3.VAL=3 AND A4.VAL=4 AND A5.VAL=5 AND A6.VAL=6 AND A7.VAL=7 AND A8.VAL=8 AND A9.VAL=9 AND CASE WHEN A1.VAL1=- THEN -A1.VAL ELSE A1.VAL END+ CASE WHEN A2.VAL1=- THEN -A2.VAL ELSE A2.VAL END+ CASE WHEN A3.VAL1=- THEN -A3.VAL ELSE A3.VAL END+ CASE WHEN A4.VAL1=- THEN -A4.VAL ELSE A4.VAL END+ CASE WHEN A5.VAL1=- THEN -A5.VAL ELSE A5.VAL END+ CASE WHEN A6.VAL1=- THEN -A6.VAL ELSE A6.VAL END+ CASE WHEN A7.VAL1=- THEN -A7.VAL ELSE A7.VAL END+ CASE WHEN A8.VAL1=- THEN -A8.VAL ELSE A8.VAL END+ CASE WHEN A9.VAL1=- THEN -A9.VAL ELSE A9.VAL END=1

执行结果:

技术分享

SQL谜题(加减符号替代)

原文:http://www.cnblogs.com/martintuan/p/4917018.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!