sql 题目
1.自增列
通用:
select id=(select count() from table b where b.sid<a.sid) ,* from table a;
select id=identity(int,,),* from ...
第二个已经有主键自增列的就不可以用了
还有就是rownumber
2.
CREATE TABLE dbo.#testTab
(
Id int NOT NULL
)
添加数据:
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();
insert into #testTab values();

select s2.id+,s3.id- from (
select * from ( select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
select t1.Id,
t2.Id as UpId,t3.id as NextId from #testTab as t1
left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+)
where t2.Id is null or t3.Id is null) as t4
) as s1 where (s1.init < (
select COUNT() from #testTab as t1 left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+) where t2.Id is null or t3.Id is null
) and s1.UpId is null) or (s1.UpId is null and s1.NextId is null)
) as s2 left join
(select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
select t1.Id,
t2.Id as UpId,t3.id as NextId from #testTab as t1
left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+)
where t2.Id is null or t3.Id is null) as t4) s3
on s2.init = s3.init-
where s3.Id is not null

其实就是查出id列排序后数据之间的间隔数据的最小与最大值
分析:
//1 t4
select t1.Id,t2.Id as UpId,t3.id as NextId from #testTab as t1
left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+)
where t2.Id is null or t3.Id is null

//2 s1
select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
select t1.Id,t2.Id as UpId,t3.id as NextId from #testTab as t1
left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+)
where t2.Id is null or t3.Id is null)as t4

//3 s2 //获取间隔数据中小的
select * from ( select Row_Number() over (order by id) as init,t4.Id,t4.UpId,t4.NextId from (
select t1.Id,t2.Id as UpId,t3.id as NextId from #testTab as t1
left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+)
where t2.Id is null or t3.Id is null) as t4
) as s1 where (s1.init < (
select COUNT() from #testTab as t1 left join #testTab as t2 on t1.Id = (t2.Id-)
left join #testTab as t3 on t1.Id = (t3.Id+) where t2.Id is null or t3.Id is null
) and s1.UpId is null) or (s1.UpId is null and s1.NextId is null)

这一步是最重要的,它的思路是:
先找出间隔中比较小的那端
s1.init<(.......)and s1.UpId is null 是为了这里的话就是26,27,剔除不是间隔的数据
s1.UpId is null and s1.NextId is null 是为了如果这里26,27只有一位的话,它其实是不能被剔除的,它是间隔的数据
最后就是通过间隔中较小的数据的init+1来查找间隔的较大数据,其实s3就是s1,左连进就可以了。
3.sql%余数
要整除时就要最后一个数字,就是不能为0
类似其他的Mod
select case %count() when then count() else %count() end from Alliance_B2BContacter where IsShow='T'
这个里面就是一个随机数(这里是2)整除表数量后的序号,不能为0,当为0是就取表最后一条数据。
sql 题目的更多相关文章
- LeetCode SQL题目(第一弹)
LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...
- Oracle语法 及 SQL题目(一)
目录 课例复制 SQL题目一 SQL题目二 SQL题目三 笔记 课例复制 OCM 全称:Oracle Certified Master 认证大师 含义:Oracle 原厂推出的数据库方向最高级别认证 ...
- Oracle语法 及 SQL题目(三)
目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...
- 网上一些sql题目的解决(网上答案+自己答案)
此篇博客内容引自“MySQL经典练习题及答案” 废话不不多说!!! 建表.插入数据. --建表 --学生表 CREATE TABLE Student( s_id VARCHAR(20), s_name ...
- 面试题中遇到的SQL题目
1.假设有一张表示cj表 Name Subject Result 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 82 要求查询结果: 姓名 语文 ...
- 感觉挺有意思的SQL题目
1.有如下数据,要求查询每个班最低分和最高分,并将最高分与最低分显示为同一列 ID Student CourseName Score1 张三 English 802 张三 Math 703 张三 Ch ...
- Oracle语法 及 SQL题目(二)
目录 课例复制 思考题四 解题思路 思考题五 解题思路 课例复制 思考题四 最近,经过你的努力,你得到了一份工作,成为了百货公司的一位经理. 到位后,你发现你的销售数据库中有两张表,一个是商店促销时间 ...
- Sql题目精选练习
1.每日经典sql 1.1.1 根据三张关系表查询雇员中工资最高的雇员的员工姓名.工资和部门号. salary(工资表) employee(员工表) department(部门表) Sql语句: SE ...
- Leetcode中的SQL题目练习(二)
175. Combine Two Tables https://leetcode.com/problems/combine-two-tables/description/ Description Pe ...
随机推荐
- PL/SQL常用表达式及举例(一)
IF 判断条件 THEN 满足条件时执行语句 END IF; DECLARE v_countResult NUMBER; BEGIN SELECT COUNT(empno) INTO v_countR ...
- SP11469 SUBSET-Balanced Cow Subsets meet-in-the-middle+状压
正解:折半搜索 解题报告: 传送门! 这题我开始看到的时候贼开心地就把这题的代码直接粘过来辣 然后就T辣,,,仔细思考一下,为什么呢? 因为会枚举到很多相同的状态 举个eg 20 1 1 1 1 1 ...
- (3.5)mysql基础深入——mysqld_safe脚本功能及流程
(3.5)mysql基础深入——mysqld_safe脚本功能及流程 目录 1.mysqld_safe过程总结 2.mysql_safe启动的好处 3.mysqld_safe 参数 4.mysqld_ ...
- 更新Newtonsoft.Json后报异常,未能加载文件或程序集“Newtonsoft.Json
未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个 ...
- Unity注意事项
1.尽量避免出现中文路径 2.做2D游戏时,图片最好要与背景图片保持一定距离 以免重叠
- docker学习(3)--Dockfile详解
转载请注明出处:http://www.cnblogs.com/lighten/p/6900556.html 1.基本说明 Dockfile是一个用于编写docker镜像生成过程的文件,其有特定的语法. ...
- OCR学习及tesseract的一些测试
最近接触OCR,先收集一些资料,包括成熟软件.SDK.流行算法. 1. 一个对现有OCR软件及SDK的总结,比较全面,包括支持平台.编程语言.支持字体语言.输出格式.相关链接等 http://en.w ...
- myeclipse连接sql server2008 r2数据库
我用的myeclipse自带的jdk1.6连接的,所以选用sqljdbc4.jar的jar包,我是win7电脑 之前也看到一些用户留的微软官方连接,但是官方那边已经取消下载了,所以我重新去找了下 链接 ...
- [转]Visual C++ RunTime的特征——非烫即屯
Visual C++ RunTime的特征——非烫即屯 大一刚学C语言,第二次上机课,当我发现我照着书抄写的程序在运行之外的黑框里面跳出一排“烫烫烫烫烫”,当时就震惊了.你们能想象一个来自小城, 在大 ...
- Loadrunder场景设计篇——添加windows Resource计数器和指标说明
目的 监控要测试的windows服务器的资源使用情况 A.添加计数器步骤 1.添加视图,方法双击.拖动左侧的Windows Rescources到右边图形区,或右键右侧已有视图,Open a New ...