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 题目的更多相关文章

  1. LeetCode SQL题目(第一弹)

    LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...

  2. Oracle语法 及 SQL题目(一)

    目录 课例复制 SQL题目一 SQL题目二 SQL题目三 笔记 课例复制 OCM 全称:Oracle Certified Master 认证大师 含义:Oracle 原厂推出的数据库方向最高级别认证 ...

  3. Oracle语法 及 SQL题目(三)

    目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...

  4. 网上一些sql题目的解决(网上答案+自己答案)

    此篇博客内容引自“MySQL经典练习题及答案” 废话不不多说!!! 建表.插入数据. --建表 --学生表 CREATE TABLE Student( s_id VARCHAR(20), s_name ...

  5. 面试题中遇到的SQL题目

    1.假设有一张表示cj表 Name Subject Result 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 82 要求查询结果: 姓名 语文 ...

  6. 感觉挺有意思的SQL题目

    1.有如下数据,要求查询每个班最低分和最高分,并将最高分与最低分显示为同一列 ID Student CourseName Score1 张三 English 802 张三 Math 703 张三 Ch ...

  7. Oracle语法 及 SQL题目(二)

    目录 课例复制 思考题四 解题思路 思考题五 解题思路 课例复制 思考题四 最近,经过你的努力,你得到了一份工作,成为了百货公司的一位经理. 到位后,你发现你的销售数据库中有两张表,一个是商店促销时间 ...

  8. Sql题目精选练习

    1.每日经典sql 1.1.1 根据三张关系表查询雇员中工资最高的雇员的员工姓名.工资和部门号. salary(工资表) employee(员工表) department(部门表) Sql语句: SE ...

  9. Leetcode中的SQL题目练习(二)

    175. Combine Two Tables https://leetcode.com/problems/combine-two-tables/description/ Description Pe ...

随机推荐

  1. WIN7怎么把暴风影视库去除?

    最近在win7 64位系统上安装新版暴风,打开我的电脑里面多了个暴风影视库图标,点击右键也没有删除 每次一打开“计算机”看见这个很碍眼.怎么把它关闭呢? 方法如下: 打开注册表,删除此项 HKEY_C ...

  2. LoadRunner-参数化(参数间关联)

    多用户测试,每个用户账号对应一个密码,账号和密码需要一 一关联才能登录成功. 1.在已经对账号进行参数化后,选中密码的值->右键选择Replace with a Parameter,参数名为pa ...

  3. Callable接口解析

    1.接口的定义: public interface Callable<V> { V call() throws Exception; } 2.Callable和Runnable的异同 先看 ...

  4. Elegant Construction---hdu5813(构造图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5813 题意是:有n个点,每个点都能到达num个点,让我们构造任意一个有向图满足条件,即:使得 i 能到 ...

  5. libevent 网络IO分析

    libevent 网络IO分析 Table of Contents 1. 简介 2. 简单使用与入门 2.1. 定时器-timeout 超时回调 2.2. 信号事件 2.3. 读取 socket 3. ...

  6. 如何bitbucket上删除项目

    老外网页操作习惯不同,删除项目的按钮,我花了半天,突然瞟到delete,如下图(真他妈的隐蔽,记住这2017/3/7):

  7. utf8 与 utf-8区别

    在使用中常常遇到 utf-8 和 utf8,现在终于弄明白他们的使用不同之处了,现在来和大家分享一下,下面我们看一下 utf8 和 UTF-8 有什么区别. "UTF-8" 是标准 ...

  8. vue.set的用法

    Vue.set(this.food,'count',1) //就是给this.food里面添加一个count的属性,并且赋值为1

  9. Git warning:LF will be replaced by CRLF in readme.txt的原因与解决方案

    今天用Git bash遇到的问题,看了几个回答之后发现一个比较有价值的,给大家分享一下,其他很多的回答都有很或多或少存在一些弊端. 原回答地址在stackoverflow上,附上链接--http:// ...

  10. RMAN备份与恢复实践(转)

    1   RMAN备份与恢复实践 1.1  备份 1.1.1 对数据库进行全备 使用backup database命令执行备份 RMAN> BACKUP DATABASE; 执行上述命令后将对目标 ...