热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

有用的SQL语句(删除重复记录,收缩日志)

都是一些比较有用的sql语句,学习的朋友可以参考下。

都是一些比较有用的sql语句,学习的朋友可以参考下。

删除重复记录,将TABLE_NAME中的不重复记录保存到#TABLE_NAME中

select distinct * into #table_name from table_name
delete from table_name
select * into table_name from #table_name
drop table #table_name

与此相关的是“select into”选项,可以在数据库属性
对话框中,勾起来此项,或者在Query Analyzer中执行
execute sp_dboption 'db_name','select into','true'
开启。默认值是关闭的。


*******************************************************
收缩事务日志(多次执行)

backup log register with NO_LOG
backup log register with TRUNCATE_ONLY
DBCC SHRINKDATABASE(register)

更多有用的sql语句
/*sql 语法学习*/

/*函数的学习---------------------------------------*/

获取当前时间(时/分/秒):select convert(varchar(10),getdate(),8)
获取当前年月日:select convert(varchar(10),getdate(),120)
获取当前年月:select convert(varchar(7),getdate(),120)
获取当前年月:select convert(varchar(10),year(getdate())) + '-' + convert(varchar(10),month(getDate()))

select cast(b as integer) as bb from table1 where b = '11'

select a,case b when '11' then '细细' when '22' then '呵呵' else '哈哈' end as 转换,c from table1

select a,b,case when c = '111' then '细细' when c = '222' then '呵呵' else '哈哈' end as 转换1 from table1

获取当前时间:print current_timestamp

/*---------------------------------------------*/

-----------------将sql查询输出到txt文本文件中-------------------------------------------
EXEC master..xp_cmdshell 'bcp 数据库名.dbo.表名 out d:\1.txt -c -q -U"sa" -P"password"'

---------------------------------------------------------------------------------------

---------------------------round的用法beigin------------------------------
declare @s float
set @s = 0.1566134
print round(@s,3)
---------------------------round的用法end---------------------------------

--------------------------------自动收缩数据库begin-----------------------------

EXEC [master]..sp_dboption [Database Name], 'autoshrink', 'TRUE'

--------------------------------自动收缩数据库end-----------------------------


-------------------------------去除首尾无效的字符begin--------------------------
declare @s varchar(20)
set @s=',,,1->1,'
while(left(@s,1)=',')
set @s=stuff(@s,1,1,'')
while(right(@s,1)=',')
set @s=stuff(reverse(@s),1,1,'')
select @s
-------------------------------去除首尾无效的字符end--------------------------


------------删除数据库中的重复记录(且仅保留一条有效记录)示例-----------------
create table A
(
userID int identity(1,1),
userName varchar(20),
userPwd varchar(20),
userEmail varchar(50)
)
insert into A(userName,userpwd) select 'qin','qin' union all select 'qin','qin1' union all select 'qin','qin1'
select * from A

--method one
delete from A where userid not in(select min(userid) as userid from A group by username ,userpwd)

--method two
delete from A where exists (select * from A b where a.username = b.username and a.userpwd = b.userpwd and a.userid
--method three
delete from a where userid not in(select min(userid) from A b where a.username = b.username and a.userpwd = b.userpwd and a.userid > b.userID)

select * from A
drop table A

------------删除数据库中的重复记录(且仅保留一条有效记录)示例-----------------



-------------------------------迭归的应用(找起点和终点之间的路径-----------------------------
create table t
(st varchar(20),ed varchar(20),km int)
go
insert t values ('A','B',1000)
insert t values ('A','C',1100)
insert t values ('A','D',900)
insert t values ('A','E',400)
insert t values ('B','D',300)
insert t values ('D','F',600)
insert t values ('E','A',400)
insert t values ('F','G',1000)
insert t values ('C','B',600)
go
--显示插入值
select * from t
go

--创建函数
--函数返回一个表,根据实际情况的不同一层一层的插入,可以充分利用生成的表
create function f_go(@col varchar(10))
returns @t table(col varchar(30),st varchar(20),ed varchar(20),km int,level int)
as
begin
declare @i int
set @i=1
insert @t select st+'-'+ed,*,@i from t where st=@col
while exists (select * from t a,@t b where
b.ed=a.st and b.level=@i and b.ed<>@col )
begin
set @i=@i+1
insert @t
select b.col+'-'+a.ed,a.st,a.ed,b.km+a.km,@i from t a,@t b
where b.level=@i-1 and b.ed=a.st and b.ed<>@col
end
return
end
go

--调用
--select * from dbo.f_go('A')
select col,km from dbo.f_go('a')

--删除环境
drop function f_go
drop table t

-------------------------------迭归的应用(找起点和终点之间的路径-----------------------------



--------按类别去最新的前N条记录,把同一类的放在一起,统计同一类的项的个数等-------------
create table t
(
ClassName varchar(50),
ClassCode varchar(10),
ClassID int identity(1,1)
)
insert into t
select 'cccc1','002' union all
select 'aaaa','001' union all
select 'bbbb','001' union all
select 'aaaa1','002' union all
select 'cccc','001' union all
select 'dddd','001' union all
select 'bbbb1','002' union all
select 'dddd1','002'
select * from t
select ClassCode = (case when exists(select 1 from t t1 where classCode = t1.ClassCode
and ClassID then '' else ClassCode end),ClassName from t order by ClassCode,ClassID desc

select count(*),classCode from (select top 100 percent ClassCode = (case when exists(select 1 from t t1 where classCode = t1.ClassCode
and ClassID then '' else ClassCode end),ClassName from t order by ClassCode,ClassID desc)a group by classcode

select classCode,className from t order by classCode,classID desc
drop table t

--------按类别去最新的前N条记录,把同一类的放在一起,统计同一类的项的个数等-------------


-------------同上,按类别进行统计,把同一类的项的其他内容进行相加并发在一个字段中------------------
create table tb(ProductID varchar(10),PositionID varchar(10))
insert into tb
select '10001','A1'
union all select '10001','B2'
union all select '10002','C3'
union all select '10002','D4'
union all select '10002','E5'
go

create function dbo.fc_str(@ProductID varchar(10))
returns varchar(100)
as
begin
declare @sql varchar(1000)
set @sql=''
select @sql=@sql+','+cast(PositionID as varchar(20)) from tb where ProductID=@ProductID
return stuff(@sql,1,1,'')
end
go

select ProductID,dbo.fc_str(ProductID) as PositionID from tb group by ProductID

drop table tb

drop function dbo.fc_str

-------------按类别进行统计,把同一类的项的其他内容进行相加并发在一个字段中------------------



--取各个类的前n条记录(每个类都取top n条)
--如果有数据库中有多个类,现在要取每个类的前n条记录,可用以下语句
Create Table TEST
(ID Int Identity(1,1),
h_id Int)
Insert TEST Select 100
Union All Select 100
Union All Select 100
Union All Select 101
Union All Select 101
Union All Select 101
Union All Select 100
GO
--方法一:
Select * From TEST A Where Id In(Select TOP 3 ID From TEST Where h_id=A.h_id)
--方法二:
Select * From TEST A Where Not Exists (Select 1 From TEST Where h_id=A.h_id And ID2)
--方法三:
Select * From TEST A Where (Select Count(*) From TEST Where h_id=A.h_id And IDGO
Drop Table TEST
GO


--分组统计,统计每个段中数据的个数
--一般成绩统计可以用到这个
declare @t table(id int,weight int)
insert into @t select 1, 20
insert into @t select 2, 15
insert into @t select 3, 5
insert into @t select 4, 60
insert into @t select 5, 12
insert into @t select 6, 33
insert into @t select 7, 45
insert into @t select 8, 59
insert into @t select 9, 89
insert into @t select 10,110

declare @p int
set @p=10
select
rtrim(p*@p)+'-'+rtrim((p+1)*@p">p*@p)+'-'+rtrim((p+1)*@p) as p,
num
from
(select (weight/@p">weight/@p) as p,count(*) as num from @t where weight between 10 and 100 group by (weight/@p">weight/@p)) a


----------------------------在in语句中只用自定义排序begin--------------------------------
declare @t table(id int,weight int)
insert into @t select 1, 20
insert into @t select 2, 15
insert into @t select 3, 5
insert into @t select 4, 60
insert into @t select 5, 12
insert into @t select 6, 33
insert into @t select 7, 45
insert into @t select 8, 59
insert into @t select 9, 89
insert into @t select 10,110
--默认in语句中sql会按照id进行排序
select * from @t where id in(2,4,3)
--用此方法可以按照我们传入的id顺序进行显示数据
select * from @t where id in(2,4,3) order by charindex(rtrim(id),',2,4,3,')

----------------------------在in语句中只用自定义排序end--------------------------------
推荐阅读
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 本文介绍了在SQL中查询分组后每组行数的统计方法。通过使用count()函数和GROUP BY子句可以统计每组的行数,但是如何统计所有组的行数呢?本文提供了一种实现方法,并给出了相应的SQL查询语句。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • Echarts图表重复加载、axis重复多次请求问题解决记录
    文章目录1.需求描述2.问题描述正常状态:问题状态:3.解决方法1.需求描述使用Echats实现了一个中国地图:通过选择查询周期&#x ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Python字典推导式及循环列表生成字典方法
    本文介绍了Python中使用字典推导式和循环列表生成字典的方法,包括通过循环列表生成相应的字典,并给出了执行结果。详细讲解了代码实现过程。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • “你永远都不知道明天和‘公司的意外’哪个先来。”疫情期间,这是我们最战战兢兢的心情。但是显然,有些人体会不了。这份行业数据,让笔者“柠檬” ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
author-avatar
众神痴梦_325
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有