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. 2018/03/09 每日一个Linux命令 之 chgrp/chown

    每日一个Linux命令 2018-03-09 Linux 命令 chgrp/chown chgrp [-参数] [文件或者目录] chown [-参数] [文件所有者]:[文件所属群组] [文件或者目 ...

  2. mysql union出错: "Every derived table must have its own alias"

    SELECT count(*) from (SELECT province_code as area_id, count(*) from t_cas_rural_selected GROUP BY p ...

  3. Spark 数据源

    一.mysql作为数据源 import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession} /** * mysql作为数据源 * ...

  4. How Many Answers Are Wrong----hdu3038(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:数组第 a 个元素到第 b 个元素之间的和为sum: 求有几句话是假的,如果与前面的话有冲 ...

  5. [vue]vue-cli下载原理

    正常vue-cli这样操作就ok了 vue-cli github $ npm install -g vue-cli $ vue init webpack my-project $ cd my-proj ...

  6. for与while的特点及其if在什么情况下使用情况

    for和while的特点: 什么时候使用循环结构呢? 1:当对某些代码执行很多次时,使用循环结构完成. 2:当对一个条件进行一次判断时,可以使用if语句. 3:当对一个条件进行多次判断时,可以使用wh ...

  7. 使用POI读取/创建Execl(.xlsx)文件

    最近项目中用到了解析Execl表格的功能,在网上百度了一下自己写了一个小Demo.由于项目中使用的是Execl2007,就是后缀为.xlsx的,所以只研究了解析和创建Execl2007的文件,解析Ex ...

  8. cordova+Android Studio 1.0+ionic+win7(转)

    转自http://blog.csdn.net/fuyunww/article/details/42216125 目录(?)[-] 在项目目录下执行 a创建工程 b添加平台支持 c添加插件在Androi ...

  9. 使用IP连接SQL SERVER或者配置为连接字符串失败

    使用IP连接SQL SERVER或者配置为连接字符串失败 情景一:当在webconfig文件中使用   <add key="ConnectionString" value=& ...

  10. Ignite初探

    Guava是一个很方便的本地缓存工具,但是在多节点处理的过程中,本地缓存无法满足数据一致性的问题.分布式缓存Ignite很好的解决了数据一致性,可靠性,事务性等方面的问题. Ignite支持分区方式和 ...