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. 读取Excel列,转换为String输出(Java实现)

    需要导入的jar包 具体实现 public class ColumnToString { public static void main(String[] args) { new ColumnToSt ...

  2. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  3. oracle行转列,列转行

    多行转字符串这个比较简单,用||或concat函数可以实现 SQL Code select concat(id,username) str from app_userselect id||userna ...

  4. Servlet----------ServletContext (重要)

    1.ServletContext的概述 一个项目只有一个ServletContext对象!application 我们可以在N多个Servlet中获取这个唯一的对象,使用它来给多个Servlet传递数 ...

  5. 抽象语法符号ASN.1(Abstract Syntax Notation One)

      一.ASN.1 (Abstract Syntax Notation One) ASN.1包括两部分:数据描述语言(ISO 8824)和数据编码规则(ISO 8825).ASN.1的数据描述语言允许 ...

  6. SQL SERVER深入学习学习资料参考

    SQL SERVER深入学习学习资料参考 1.微软Webcast<sql server 2000完结篇>. 尽管微软Webcast出了很多关于Sql Server的系列课程,但是最为深入讲 ...

  7. mysql 开启profiling

    mysql 开启profiling

  8. jmeter连接mysql数据库报错Cannot create PoolableConnectionFactory (Could not create connection to database server.)

    今天在学习jmeter的jdbc取样器,发现在配置完JDBC Connection Configuration和JDBC Request后,点击运行.在查看结果树中显示响应数据: Cannot cre ...

  9. POJ1789:Truck History(Prim算法)

    http://poj.org/problem?id=1789 Description Advanced Cargo Movement, Ltd. uses trucks of different ty ...

  10. Kafka核心组件

    一.Kafka核心组件及工作方式 Producer :消息生产者,就是向kafka broker发消息的客户端 Consumer :消息消费者,向kafka broker取消息的客户端 Topic : ...