• 计算机讨论版
  • 电脑诊所
  • 菜鸟学院
  • 软件世界
  • 安全专区
  • 硬件世界
  • 网络世界
  • 网页设计
  • 图像设计
  • 程序设计
  • 操作系统
  • 计算机考试
  • 电脑故障
  • 电脑学习
  • 电脑技术
  • 电脑入门
  • 计算机知识
  • 电脑之家
  • 故障诊断
  • 电脑医院
  • 电脑学校
  • 电脑维护
  • 电脑医生
  • 电脑问答
  • 计算机问题
  • 电脑小知识
  • 电脑软件
  • 电脑diy
  • 你问我答
  • 网友俱乐部
  • 实用技巧
  • 初级应用
  • 心得分享
  • 软硬兼施
  • 电脑之家
  • 维护资料
  • 软件应用
  • 软件交流
  • 电脑硬件
  • 硬件知识
  • 电脑网络
  • windows
  • 程序设计

    程序设计
    ·DES怎样用C语言来实现啊??????急啊!!!!!...
    ·masm的使用?
    ·寻优秀的C++网站和论坛。谢谢
    ·有那为朋友知道那里有下载C++的教材?
    ·vb试题求助
    ·爱汇编,爱生活
    ·[求助]哪个有ActionScript f...
    ·HTML[原创]
    ·各位大虾,学JSP该如何入手啊,帮帮小弟,在此谢过
    ·在线等,汇编程序问题,出现致命错误了!!!
    ·[求助]跪求jsp相关资料
    ·毕业设计求助
    ·求救,求救,应该模拟操作系统进程调度的实验
    ·tc 2.0问题
    ·老师作业成绩大虾帮忙呀
    ·别人收藏的Transact_SQL小手册,适合初学者
    ·求助:请问谁会用JSP啊!
    ·求救一道很简单的VB题目
    ·SQL综合应用学习<二>
    ·sybase安装
    ·C语言高手帮忙看一下!!
    ·这个程序怎么会???不可思意
    ·[推荐]游戏梦&数学算法
    ·[讨论]强盗分金算法
    ·[灌水]程序人生
    ·求源代码
    ·[讨论]一个关于球的算法
    ·极限编程简介
    ·[推荐]整数溢出与程序安全
    ·急需《COBOL 从入门到精通》

    (转贴)Sql Server 整理收集

    类别: 程序设计  时间: 2007.03.15

    含笑
    (转贴)Sql Server 整理收集
    Sql Server 整理收集    xubinhui(原作)  
       
    正在学习SQL Server 期望收集一些常见的Sql Server 问题 

    Sql Server问题摘要

    1.         合并若干个表?

    描述:建立一个新表,其字段结构是其他若干个表Join以后的结果。

    解决思路:select * into 新表名 from 旧表1名,旧表2名,……

    实例:

    --表NewTableName的字段为表titles和titleauthor字段表相加;

    ---记录为记录为select * from titles,titleauthor的记录

    select * into NewTableName from titles,titleauthor 

    --表NewTableName的字段[title_id,title,au_id]

    -- 记录为select a.title_id,a.title,b.au_id from titles a,titleauthor b where a.title_id=b.title_id

    select a.title_id,a.title,b.au_id into NewTableName from titles a,titleauthor b where a.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方法一]

    SELECT top 4 * FROM jobs WHERE job_id not in (SELECT top 3 job_id FROM jobs)

    --[方法二]

    SELECT top 4 * FROM

    (SELECT top 4 * FROM

    (SELECT top 7 * FROM jobs ORDER BY job_id ASC)  a

    ORDER BY job_id DESC

    ) b

    ORDER BY job_id ASC

    --取第2到第3条记录 [对1方法一]

    select IDENTITY(int,1,1) as iid,* into #temptable from discounts select top 2 * from #temptable where iid>=2

    3.         按年度季度统计汇总?

    描述: 统计表employee的各年度各季度的雇佣人数

    解决思路:Group by 和 [函数datepart()]    

    实例:

             //季度:quarter

    SELECT year(hire_date) 年度,datepart(q,hire_date) 季度,count(emp_id)雇佣人数 FROM employee

    GROUP BY year(hire_date),datepart(q,hire_date)

    ORDER BY 年度,季度

    4.         随机取n条记录?

    描述: 从表中随机提取n条记录

    解决思路:按照用Guid 

    实例:

             --从表中随机取5条记录

    SELECT top 5 * FROM titles ORDER BY newid()

    5.         求出任意时间所在季度的天数?

    描述: 如题

    解决思路:注意闰年 

    实例:

    declare @t smalldatetime

    select @t = '2001-4-1'

    select case datepart(quarter,@t) 

                      when 1 then 

    (case when (year(@t)%4=0 and year(@t)%100<>0) or year(@t)%400=0 then 91 else 90 end)

                      when 2 then 91

                      when 3 then 92

                      when 4 then 92

          end

    6.         求出任意时间的季度的第一天?

    描述: 如题

    解决思路: 如下

    实例:

    -- [思路:把日期向前推到季度的第一个月的当前号

    --注意:5月31号不会推成4月31号 ,系统自动转为30号

    declare @d datetime

    set @d='2003-5-31'

    select @d as 指定日期,

    dateadd(month,-(month(@d)-1)%3,@d)-day(dateadd(month,-(month(@d)-1)%3,@d))+1 as 季度的第一天

    -- [思路:分别求出年月日在转为日期型]

    declare @t smalldatetime

    select @t = '2001-9-30'

    select cast(cast(year(@t) as varchar) + '-' + cast(3*datepart(q,@t)-2 as varchar) + '-1' as datetime)

    --求当前的日期季度的第一天

    select cast(cast(year(getdate()) as varchar) + '-' + cast(3*datepart(q,getdate())-2 as varchar) + '-1' as datetime)

    7.         时间重叠度的问题?

    出处:http://expert.csdn.net/Expert/topic/2806/2806966.xml?temp=.5277979

    描述:

    create table #a(id int identity(1,1),date1 datetime,date2 datetime)

    insert #a select '2004-02-29 16:45:00','2004-02-29 20:45:00'

    union all select '2004-02-29 18:45:00','2004-02-29 22:45:00'

    union all select '2004-03-01 10:45:00','2004-03-01 13:45:00'

    union all select '2004-03-01 13:45:00','2004-03-01 16:45:00'

    union all select '2004-03-01 13:47:00','2004-03-01 14:25:00'

    union all select '2004-03-01 16:45:00','2004-03-01 19:15:00'

    union all select '2004-03-01 17:45:00','2004-03-01 18:55:00'

    union all select '2004-03-01 18:45:00','2004-03-01 21:45:00'

    select *from #a

    我现在要找出时间上重叠超过1个小时的所有记录!

    比如第1、2条:一个是从16:45---20:45,第二条是从18:45---22:45时间上重叠了2个小时,所以这两条都要取出,其它的也是一样的规则!

    date2-date1小于1小时的不用考虑,如第5条。可以先删除该条,记录是按date1排序的!

    所以最终的结果要为:

    id          date1                       date2                       

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

    1           2004-02-29 16:45:00.000     2004-02-29 20:45:00.000

    2           2004-02-29 18:45:00.000     2004-02-29 22:45:00.000

    6           2004-03-01 16:45:00.000     2004-03-01 19:15:00.000

    7           2004-03-01 17:45:00.000     2004-03-01 18:55:00.000

    解决思路:

    实例:

    --方案一:[作者:“victorycyz(中海,干活去了,不在CSDN玩。)”]

    select distinct a.*

    from #a a , #a b 

    where a.id<>b.id and

          a.date1<b.date2 and a.date2>b.date1 and

          (case when a.date2>b.date2 then b.date2 else a.date2 end)-

          (case when a.date1>b.date1 then a.date1 else b.date1 end)>1/24.0

    --方案二:[作者:“j9988(j9988)”]

    select * from #a A where 

    exists(select 1 from #a 

           where id<>A.id

            and dateadd(minute,60,date1)<date2

            and (date1 between A.date1 and A.date2) 

            and abs(datediff(minute,date1,A.date2))>=60)

    or 

    exists(select 1 

           from #a 

           where id<>A.id 

           and dateadd(minute,60,A.date1)<A.date2

           and (A.date1 between date1 and date2) 

           and abs(datediff(minute,A.date1,date2))>=60)    


    整理中
     


    上一篇:DES怎样用C语言来实现啊??????急啊!!!!!... 下一篇:别人收藏的Transact_SQL小手册,适合初学者

    计算机讨论版 © 版权所有

    提示:计算机讨论版致力于电脑信息的分享与传播,内容仅供参考,按此操作责任自负。