SqlServer整理收集
来源:互联网 发布:网络授课平台 兼职 编辑:程序博客网 时间:2024/06/06 02:15
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
正在学习期望收集一些常见的SqlServer问题
SqlServer问题摘要
1. 合并若干个表?
描述:建立一个新表,其字段结构是其他若干个表Join以后的结果。
解决思路:select*into新表名from旧表1名,旧表2名,……
实例:
--表NewTableName的字段为表titles和titleauthor字段表相加;
---记录为记录为select*fromtitles,titleauthor的记录
select*intoNewTableNamefromtitles,titleauthor
--表NewTableName的字段[title_id,title,au_id]
--记录为selecta.title_id,a.title,b.au_idfromtitlesa,titleauthorbwherea.title_id=b.title_id
selecta.title_id,a.title,b.au_idintoNewTableNamefromtitlesa,titleauthorbwherea.title_id=b.title_id
2. 查询N-M条记录?
描述:要从记录集中取出中间一段记录来(如:取10条中的第3-8条)
解决思路:
1. 有关键字或确保唯一的候选关键字字段:
方法1:从第n条记录开始取m-n+1条数据
方法2:升序取前m条记录最为a,再降序从a中取m-n+1条记录作为b
2. 没有上述字段:
增加一个自增字段生成一个临时表,在从临时表中取记录[Where自增字段名>n-1]
实例: --
--取第4到第7条记录[对1方法一]
SELECTtop4*FROMjobsWHEREjob_idnotin(SELECTtop3job_idFROMjobs)
--[方法二]
SELECTtop4*FROM
(SELECTtop4*FROM
(SELECTtop7*FROMjobsORDERBYjob_idASC) a
ORDERBYjob_idDESC
)b
ORDERBYjob_idASC
--取第2到第3条记录[对1方法一]
selectIDENTITY(int,1,1)asiid,*into#temptablefromdiscountsselecttop2*from#temptablewhereiid>=2
3. 按年度季度统计汇总?
描述:统计表employee的各年度各季度的雇佣人数
解决思路:Groupby和[函数datepart()]
实例:
//季度:quarter
SELECTyear(hire_date)年度,datepart(q,hire_date)季度,count(emp_id)雇佣人数FROMemployee
GROUPBYyear(hire_date),datepart(q,hire_date)
ORDERBY年度,季度
4. 随机取n条记录?
描述:从表中随机提取n条记录
解决思路:按照用Guid
实例:
--从表中随机取5条记录
SELECTtop5*FROMtitlesORDERBYnewid()
5. 求出任意时间所在季度的天数?
描述:如题
解决思路:注意闰年
实例:
declare@tsmalldatetime
select@t='2001-4-1'
selectcasedatepart(quarter,@t)
when1then
(casewhen(year(@t)%4=0andyear(@t)%100<>0)oryear(@t)%400=0then91else90end)
when2then91
when3then92
when4then92共3页 第1页
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
正在学习期望收集一些常见的SqlServer问题
SqlServer问题摘要
1. 合并若干个表?
描述:建立一个新表,其字段结构是其他若干个表Join以后的结果。
解决思路:select*into新表名from旧表1名,旧表2名,……
实例:
--表NewTableName的字段为表titles和titleauthor字段表相加;
---记录为记录为select*fromtitles,titleauthor的记录
select*intoNewTableNamefromtitles,titleauthor
--表NewTableName的字段[title_id,title,au_id]
--记录为selecta.title_id,a.title,b.au_idfromtitlesa,titleauthorbwherea.title_id=b.title_id
selecta.title_id,a.title,b.au_idintoNewTableNamefromtitlesa,titleauthorbwherea.title_id=b.title_id
2. 查询N-M条记录?
描述:要从记录集中取出中间一段记录来(如:取10条中的第3-8条)
解决思路:
1. 有关键字或确保唯一的候选关键字字段:
方法1:从第n条记录开始取m-n+1条数据
方法2:升序取前m条记录最为a,再降序从a中取m-n+1条记录作为b
2. 没有上述字段:
增加一个自增字段生成一个临时表,在从临时表中取记录[Where自增字段名>n-1]
实例: --
--取第4到第7条记录[对1方法一]
SELECTtop4*FROMjobsWHEREjob_idnotin(SELECTtop3job_idFROMjobs)
--[方法二]
SELECTtop4*FROM
(SELECTtop4*FROM
(SELECTtop7*FROMjobsORDERBYjob_idASC) a
ORDERBYjob_idDESC
)b
ORDERBYjob_idASC
--取第2到第3条记录[对1方法一]
selectIDENTITY(int,1,1)asiid,*into#temptablefromdiscountsselecttop2*from#temptablewhereiid>=2
3. 按年度季度统计汇总?
描述:统计表employee的各年度各季度的雇佣人数
解决思路:Groupby和[函数datepart()]
实例:
//季度:quarter
SELECTyear(hire_date)年度,datepart(q,hire_date)季度,count(emp_id)雇佣人数FROMemployee
GROUPBYyear(hire_date),datepart(q,hire_date)
ORDERBY年度,季度
4. 随机取n条记录?
描述:从表中随机提取n条记录
解决思路:按照用Guid
实例:
--从表中随机取5条记录
SELECTtop5*FROMtitlesORDERBYnewid()
5. 求出任意时间所在季度的天数?
描述:如题
解决思路:注意闰年
实例:
declare@tsmalldatetime
select@t='2001-4-1'
selectcasedatepart(quarter,@t)
when1then
(casewhen(year(@t)%4=0andyear(@t)%100<>0)oryear(@t)%400=0then91else90end)
when2then91
when3then92
when4then92共3页 第1页
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- SqlServer整理收集
- 收集整理
- SQLServer和Oracle的常用函数对比(收集,不断整理中)
- SQLServer和Oracle的常用函数对比(收集,不断整理中)
- SqlServer语句收集
- SqlServer资料收集
- SQLServer优化资料整理
- SQLServer优化资料整理
- sqlserver 基础知识大整理
- SqlServer语法整理
- SqlServer链接字符串整理
- SQLServer优化资料整理
- sqlserver 数据恢复 资料收集
- SQLServer存储过程收集总结
- 自动化收集SQLSERVER诊断信息
- SQLServer Windows数据收集器
- linuxQQ群收集整理
- 名词解释(收集整理)
- 软件设计师考试大纲
- 利用uniread解决下的SQL*Plus命令行历史回调功能
- WPF,SilverLight中直线的样式示例
- 千里之行始于足下
- 理解和使用NT驱动程序的执行上下文(一)
- SqlServer整理收集
- 获取Divx的版本号(测试中)
- 链表和数组(转)
- 用户自定义的修复
- BPO概念的理解
- 连接浏览器SHELL窗口的代码
- 学习SQL文档
- 编译 FFMPEG 动态库
- 连接IE浏览器窗口的代码