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 ...
随机推荐
- 学习计划 mysql 用户管理与权限
最近在学习数据库的 主从复制 里面涉及到了关于用户及其管理权限的赋予,之前一直没有认真的学习这个. 现在想具体的学习一下. -- 为什么 数据库 要实现多用户管理? 举个最简单的例子,你需要和第三方做 ...
- Centos7网桥配置
CentOS 的网桥虽然配置了很多次,不过总是记不住那几条,还是简单记录下,增加网桥可以通过brctl命令,但是为了简便快捷,直接生成配置文件即可 1.在/etc/sysconfig/network- ...
- shell_exec() has been disabled for security reasons错误怎么解决?
ytkah在用composer安装插件时出现了shell_exec() has been disabled for security reasons错误提示,这个是php配置的问题,shell_exe ...
- Tensorflow(二)
1---------------- 试用tensorflow的模块,必须配套tensorflow的方法 import tensorflow as tf a=3 ##定义 行向量 w=tf.Variab ...
- [py][mx]xadmin注册切换主题功能和网站名称修改
注册主题 这里将基础的设置放到users模块 users/adminx.py from xadmin import views class BaseSetting(object): enable_th ...
- Andrew Ng-ML-第十二章-机器学习系统设计
1.确定执行的优先级 图1.邮件垃圾分类举例 选择100个单词作为指示是否是垃圾邮件的指标,将这些单词作为特征向量,只用0/1表示,出现多次也只用1表示,特征变量用来表示邮件. 通常情况下,会选择训练 ...
- $scope.$apply
对于一个在前端属于纯新手的我来说,Javascript都还是一知半解,要想直接上手angular JS,遇到的阻力还真是不少.不过我相信,只要下功夫,即使是反人类的设计也不是什么大的问题. Okay, ...
- [LeetCode] 560. Subarray Sum Equals K_Medium
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 怎么申请 bing api key
1:打开网址 https://login.live.com/ 注册帐号并登录(点击上图中的登录按钮即可),在新窗口点击下方的“立即注册”(有帐号的可以直接登录) 2:填写相关信息(推荐使用hotmai ...
- Java-使用IO流对大文件进行分割和分割后的合并
有的时候我们想要操作的文件很大,比如:我们想要上传一个大文件,但是收到上传文件大小的限制,无法上传,这是我们可以将一个大的文件分割成若干个小文件进行操作,然后再把小文件还原成源文件.分割后的每个小文件 ...