DataBase 之 实用积累
(1)分页:(性能效果最好的是top/max 和Row_number()两种方法)
[1]写法1,not in/top
select top pagesize * from pagetest
where id not in
(select top (pagesize*(pageindex-1)) id
from pagetest order by id)
order by id
[2]写法2,not exists(引导的子句无结果集返回)
select top pagesize * from pagetest
where not exists
(select 1 from
(select top (pagesize*(pageindex-1)) id from pagetest order by id) a
where a.id = pagetest.id)
order by id
[3]写法3,max/top
select top pagesize * from pagetest
where id>
(select max(id) from
(select top (pagesize*(pageindex-1)) id
from pagetest order by id) a
)
order by id
[4]写法4,row_number()
select top pagesize * from
(select row_number()over(order by id)rownumber,*
from pagetest) a
where rownumber between startIndex and endIndex
[5]分页函数(先循环插入100000000条数据然后再分页)
create table #FYTable (id int identity(1,1),title nvarchar(20),con nvarchar(20))
declare @title nvarchar(300),
@con nvarchar(300)
declare @start int,@end int
set @start=0
set @end=1000000
set @title='Olive'
set @con='I love'
while @start<@end
begin
insert into #FYTable select @title,@con
set @start=@start+1
end
select * from #FYTable
create procedure dbo.FenYe
@count int,@yeshu int
as
select top(@yeshu) * from #FYTable where id not in (select top(@count*@yeshu) id from #FYTable)
(2)排序:排序函数Rank()
select SName,Sage,SSex
from (select SName,Sage,Ssex,Rank() over(order by sage) as rank
from Student) as a
where a.rank > 1 and a.rank < 9;
(3)表行列转换:
数据表行列转换(case when then)
create table Factory( FID int, FDepartment nvarchar(20), FMeterial nvarchar(20), FNumber int)
insert into Factory values(1,'工厂1','材料1',1);
insert into Factory values(1,'工厂2','材料1',1);
insert into Factory values(1,'工厂2','材料2',2);
insert into Factory values(1,'工厂2','材料3',1);
insert into Factory values(1,'工厂3','材料3',1);
insert into Factory values(1,'工厂2','材料1',2);
insert into Factory values(1,'工厂3','材料2',1);
select * from Factory
select FDepartment,
SUM(case FMeterial when '材料1' then FNumber else 0 end) as 材料1,
SUM(case FMeterial when '材料2' then FNumber else 0 end) as 材料2,
SUM(case FMeterial when '材料3' then FNumber else 0 end) as 材料3
from Factory group by FDepartment
--行列转换函数Pivot
create table Sell
(
[Year] int , [Quarter] nvarchar(2) , Quantity int
)
insert into Sell values(2011,'Q1',12)
insert into Sell values(2011,'Q2',13)
insert into Sell values(2011,'Q2',14)
insert into Sell values(2011,'Q3',15)
insert into Sell values(2011,'Q2',12)
insert into Sell values(2011,'Q3',13)
insert into Sell values(2011,'Q4',15)
insert into Sell values(2011,'Q4',17)
insert into Sell values(2011,'Q4',11)
insert into Sell values(2012,'Q3',13)
insert into Sell values(2012,'Q4',15)
insert into Sell values(2012,'Q2',17)
insert into Sell values(2012,'Q4',11)
insert into Sell values(2012,'Q3',13)
insert into Sell values(2012,'Q1',15)
insert into Sell values(2012,'Q1',17)
insert into Sell values(2012,'Q4',11)
select * from Sell
select * from Sell Pivot(sum(Quantity) for [Quarter] in (Q1,Q2,Q3,Q4) )as p
select [Year], SUM(case when [Quarter]='Q1' then Quantity end) as Q1,
SUM(case when [Quarter]='Q2' then Quantity end) as Q2,
SUM(case when [Quarter]='Q3' then Quantity end) as Q3,
SUM(case when [Quarter]='Q4' then Quantity end) as Q4
from Sell group by [Year]
--case when then 用例
drop table T
go
create table T (TID int identity(1,1) not null primary key,Quantity int not null,ProductID int not null,[Type] int not null)
go
insert into T select 2,12,1
insert into T select 6,12,2
insert into T select 3,10,2
insert into T select 8,10,1
insert into T select 2,12,1
select * from T
go
--统计每种商品有多少件
select ProductID,SUM (case when [Type]=1 then Quantity else -Quantity end) from T group by ProductID
--使用case when 为表的新列填充不同的数据
alter table T add Descriptions nvarchar(20) null
go
update T set Descriptions= case when [Type]=1 then '现存'
when [Type]=2 then '售出'
else '未知'
end
go
--怎么把这样一个表
--year month amount
--1991 1 1.1
--1991 2 1.2
--1991 3 1.3
--1991 4 1.4
--1992 1 2.1
--1992 2 2.2
--1992 3 2.3
--1992 4 2.4
--查成这样一个结果
--year m1 m2 m3 m4
--1991 1.1 1.2 1.3 1.4
--1992 2.1 2.2 2.3 2.4
create table tb_Test1
([year] int,[month] int,amount float)
insert into tb_Test1
select 1991,1,1.1
union select 1991,2,1.2
union select 1991,3,1.3
union select 1991,4,1.4
union select 1992,1,1.1
union select 1992,2,1.2
union select 1992,3,1.3
union select 1992,4,1.4
select * from tb_Test1t
--根据月份的不同分为不同的字段,然后再按照年份分组
select [year],sum(case [MONTH] when 1 then amount else 0 end) as m1,sum(case [MONTH] when 2 then amount else 0 end) as m2,sum(case [MONTH] when 3 then amount else 0 end) as m3,sum(case [MONTH] when 4 then amount else 0 end) as m4 from tb_Test1 group by [year]
--有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A中对应的value。这道题的SQL语句怎么写?
create table tb_A(keys int,value varchar(20))
create table tb_B(keys int,value varchar(20))
insert into tb_A values(1,'aa'),(2,'ab'),(3,'ac')
insert into tb_B values(1,'aa'),(2,'a'),(3,'a')
方法一--先将更新B中value的值更新为A中value的值,同时限定B中的keys=A.Keys 同时B.value<>A.value(不等于)
update tb_B set value=(select a.value from tb_A a where a.keys=tb_B.keys)
where tb_B.keys in(select tb_B.keys from tb_B,tb_A where tb_A.keys=tb_B.keys and tb_A.value<>tb_B.value)
select * from tb_B
select * from tb_A
delete from tb_A
delete from tb_B
方法二--先删除A,B一样的部分,然后再根据两表中Keys 的相等进行更新
update tb_B set tb_B.value=s.value from (select * from tb_A except select * from tb_B) as s where tb_B.keys=s.Keys
--原表:
--courseid coursename score
--1 java 70
--2 oracle 90
--3 xml 40
--4 jsp 30
--5 servlet 80
--为了便于阅读,查询此表后的结果显式如下(及格分数为60):
--courseid coursename score mark
--1 java 70 pass
--2 oracle 90 pass
--3 xml 40 fail
--4 jsp 30 fail
--5 servlet 80 pass
create table tb_B(id int identity(1,1),coursename varchar(30),score int )
insert into tb_B values('java',70),('oracle',90),('xml',40),('jsp',30),('servlet',80)
select * from tb_B
--case when 的使用
select id,coursename,score, case when score > 60 then 'pass' else 'fail' end as mark from tb_B
--原表:
--id proid proname
--1 1 M
--1 2 F
--2 1 N
--2 2 G
--3 1 B
--3 2 A
--查询后的表:
--id pro1 pro2
--1 M F
--2 N G
--3 B A
create table C(id int,proid int,proname varchar(2))
insert into C
values(1,1,'M'),(1,2,'F'),(2,1,'N'),(2,2,'G'),(3,1,'B'),(3,2,'A')
select * from C
select id,(select proname
from C c1
where proid=1 and c.id=c1.id ) as pro1,
(select proname from C c2 where proid=2 and c2.id=c.id ) as pro2
from C c group by id
--如下表a
--列 a1 a2
--记录 1 a
-- 1 b
-- 2 x
-- 2 y
-- 2 z
--用select能选成以下结果吗?
--1 ab
--2 xyz
create table D (a1 varchar(2),a2 varchar(2))
insert into D values ('1','a'),('1','b'),('2','x'),('2','y'),('2','z')
Create table D1 (id varchar(2),value varchar(2))
Alter table D1 alter column value varchar(20)
insert into D1 select Distinct(a1),'' from D
declare @id varchar(2),@value varchar(20)
declare LianJie cursor for select * from D
open LianJie
fetch next from LianJie into @id,@value
while @@FETCH_STATUS=0
begin
update D1 set value=value+@value where id=@id
fetch next from LianJie into @id,@value
end
close LianJie
deallocate LianJie
select * from D
select * from D1
——3. 表内容:
--2005-05-09 胜
--2005-05-09 胜
--2005-05-09 负
--2005-05-09 负
--2005-05-10 胜
--2005-05-10 负
--2005-05-10 负
--如果要生成下列结果, 该如何写sql语句?
-- 胜 负
--2005-05-09 2 2
--2005-05-10 1 2
create table E (dt varchar(20),shengfu nvarchar(20))
insert into E values('2012-9-15','胜'),('2012-9-15','胜'),('2012-9-15','负'),('2012-9-15','负'),('2012-9-16','胜'),('2012-9-16','胜'),('2012-9-16','负')
select * from E
select dt, SUM(case when shengfu='胜' then 1 else 0 end) as 胜,SUM(case when shengfu='负' then 1 else 0 end) as 负 from E group by dt
--表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列
create table E1 (A int ,B int,C int)
insert into E1 values(1,3,5),(6,4,2),(8,7,9)
select * from E1
select case when A>B then A else B end as AB,case when B>C then B else C end as BC from E1
table1
月份 部门业绩
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8
table2
部门 部门名称
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部
table3 (result)
部门部门名称 一月份 二月份 三月份
01 国内业务一部 10 null null
02 国内业务二部 10 8 null
03 国内业务三部 null 5 8
04 国际业务部 null null 9
create table #F([month] nvarchar(20), bumen nvarchar(20), yeji int)
insert into #F values('一月份','01',10),('一月份','02',10),('一月份','03',5),('二月份','02',8),('二月份','04',9),('三月份','03',8)
create table #F1(bumen nvarchar(20),bumenmingcheng nvarchar(20))
insert into #F1 values('01','国内业务一部'),('02','国内业务二部'),('03','国内业务三部'),('04','国际业务部')
select * from #F1
select f.bumen ,f1.bumenmingcheng,sum(case when f.[month]='一月份' then f.yeji else null end) as 一月份,
sum(case when f.[month]='二月份' then f.yeji else null end) as 二月份,
sum(case when f.[month]='三月份' then f.yeji else null end) as 三月份 from #F f,#F1 f1 where f.bumen=f1.bumen group by f.bumen,f1.bumenmingcheng
drop table #Str
create table #Str(id int identity(1,1),str11 varchar(20), [type] int )
insert into #Str values('how',1),('are',1),('you',1),('fine',2),('thank',2),('you',2)
select * from #Str
create table #Str1(ss nvarchar(100),id int )
insert into #Str1 select Distinct '',([type]) from #Str
select * from #Str1
declare @id1 int,@str1 nvarchar(100)
declare StrCursor cursor for select str11,[type] from #Str
open StrCursor
fetch next from StrCursor into @str1,@id1
while @@FETCH_STATUS=0
begin
update #Str1 set ss=ss+@str1 whereid=@id1
fetch next from StrCursor into @str1,@id1
end
close StrCursor
deallocate StrCursor
select * from #Str1
--Post
create Table Post(id int identity(1,1) primary key,title nvarchar(200),content text,CreateDate nvarchar(30))
--Comments
create Table Comments(Cid int identity(1,1) primary key,id int,content text,CreateDate nvarchar(30))
insert into Post select '我从远方赶来','我为她而狂野,我为你来看我不顾一切',cast(GETDATE() as nvarchar)
insert into Comments values
(1,'不虚此行呀',CAST(getdate() as nvarchar)),
(1,'这一个不能停留太久的世界',cast(getdate() as nvarchar)),
(1,'无可取代',cast(getdate() as nvarchar))
select * from Comments
create table #Ts (id int,com nvarchar(max))
insert into #Ts select Distinct(id),'' from Comments
declare @ii int,@com nvarchar(max)
declare TsCursor cursor for select id ,content from Comments
open TsCursor
fetch next from TsCursor into @ii,@com
while @@FETCH_STATUS=0
begin
update #Ts set com=com+@com whereid=@ii
fetch next from TsCursor into @ii,@com
end
close TsCursor
deallocate TsCursor
DataBase 之 实用积累的更多相关文章
- 【JavaScript】封装实用方法【持续积累】
介绍 主要记录一些平时积累或者常用方法或者小技巧的集合.以便在以后用到还要重复写或者忘记. 还有就是如果遇到好的方法封装值得收藏进行收藏.这里主要是记录一些包含JavaScript的一些积累.没有什么 ...
- Bootstrap进阶五:Web开发中很实用的交互效果积累
1.页面切换效果 我们已经在示例中罗列了一组动画,可以被应用到页面切换过程中,创造出很有趣的导航效果.  2.视差滚动(parallax-slider) 视差滚动(parallax-slider)已 ...
- 【原】MySQL实用SQL积累
[文档简述] 本文档用来记录一些常用的SQL语句,以达到快速查询的目的. [常用SQL] 1.mysql数据库中获取某个表的所有字段名 select COLUMN_NAME from informat ...
- 我积累的Java实用代码
1.解压zip文件 /** * 解压输入的zip流,Java默认的解压只能处理UTF-8编码的文件或者目录名,否则会报MALFORMED异常 * * @param is 输入流 * @param ou ...
- 首先给大家介绍一下数据库project师,数据库project师(Database Engineer),是从事管理和维护数据库管理系统(DBMS)
摘要 MySQL的最初的核心思想,主要是开源.简便易用.其开发可追溯至1985年,而第一个内部发行版本号诞生,已经是1995年. 到1998年,MySQL已经能够支持10中操作系统了.当中就包含win ...
- SQL的积累
SQL的积累学习(不常用的经常会忘记,所以以后用到的就会记在下面): --新增字段alter table t_Student add Test varchar(200)--删除字段alter tabl ...
- Vim新手入门资料和一些Vim实用小技巧
一些网络上质量较高的Vim资料 从我07年接触Vim以来,已经过去了8个年头,期间看过很多的Vim文章,我自己觉得非常不错,而且创作时间也比较近的文章有如下这些. Vim入门 目前为阿里巴巴高级技术专 ...
- 2016年6月份那些最实用的 jQuery 插件专辑
jQuery 是一个快速.流行的 JavaScript 库,jQuery 用于文档处理.事件处理.动画和 Ajax 交互非常简单,学习曲线也很平坦.2016年6月的 jQuery 插件专辑里,我们选择 ...
- 搭建一套自己实用的.net架构(3)【ORM-Dapper+DapperExtensions】
现在成熟的ORM比比皆是,这里只介绍Dapper的使用(最起码我在使用它,已经运用到项目中,小伙伴们反馈还可以). 优点: 1.开源.轻量.小巧.上手容易. 2.支持的数据库还蛮多的, Mysql,S ...
随机推荐
- PHP的MySQL扩展:PHP访问MySQL的常用扩展函数
来源:http://www.ido321.com/1024.html 一.PHP连接数据库及基本操作 MySQL采用的是’客户机/服务器’架构.使用PHP安装的MySQL扩展函数,和直接使用客户端软件 ...
- bzoj 4278 [ONTAK2015]Tasowanie(SA,贪心)
[题意] 给定两个字符串,求二路归并后最小字典序的字符串. [思路] 连接两个字符串后求出rank数组.通过比较rank数组进行二路归并. [代码] #include<cstdio> #i ...
- codeforce 702D Road to Post Office 物理计算路程题
http://codeforces.com/contest/702 题意:人到邮局去,距离d,汽车在出故障前能跑k,汽车1公里耗时a,人每公里耗时b,修理汽车时间t,问到达终点最短时间 思路:计算车和 ...
- mvc bundle功能(1)
现如今都提倡敏捷开发,快速开发,但是再要求速度的同时,还得保证质量!前端我是没办法,毕竟是直接要面向用户的,但是后台,解决方案那就多了,诸如extjs,bootstrap,kendoui,都可以解决. ...
- restsharp发送服务端请求回传session
今天工作遇到这样一个场景,我需要获取一个游戏目录列表,这个列表接口在线上已经存在,但是这个接口需要登录认证后才能获取到,所以实现这个功能我打算分两部来做: 1.首先调登录接口,以写上session 2 ...
- Genesis-3D开源游戏引擎简介!
Genesis-3D由搜狐畅游公司超百人引擎研发团队历时数年耗费巨资自主研发,是国内外首款商业开源的3D游戏引擎平台.它包括跨平台渲染引擎.2D引擎.物理引擎.音效系统.粒子系统.动画系统.服务器引擎 ...
- 转-CMMI在中国之混乱-CMMI比ISO9000会更惨
CMMI在中国之混乱-CMMI比ISO9000会更惨 自己接触CMM/CMMI已经有8年时间了,现在静心回顾一下,觉得CMMI在中国的命运会比ISO9000还悲惨. 一组现象或许让你我对此结论有更深入 ...
- Google Appengine参考路径
1.Hello, World! in 5 minutes 2.Creating a Guestbook -Introduction 3.Sample Applications 1.Programmin ...
- Glibc辅助运行库 (C RunTime Library): crt0.o,crt1.o,crti.o crtn.o,crtbegin.o crtend.o
crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o 等目标文件和daemon.o(由我们自己的C程序文件产生)链接成一个执行文件.前面这5个目标文件的作用分别是启 ...
- 最大连续子数和问题-homework-03
一.说明 这次的作业做的不好,一小点怨念ing····· 首先向TA说明下,我的小伙伴“丢下”我后我不知道,以至于发现剩下我一个的时间有点晚,我机智地找到了一个3个人的小组,又叫到了一个小伙伴,但是悲 ...