zoukankan      html  css  js  c++  java
  • SQL Sever 学习系列之三

    SQL Sever 学习系列之三

      SQL Server 学习系列之一(薪酬方案+基础)

      SQL Server 学习系列之二(日期格式问题)

            五、经理今天刚谈到with的用法(with的类似用法在delphi中有所体现),在资料文档看到了这一段如下主要体现一个关键字pivot(这是首次在武汉三佳医疗有限公司面试开发岗时遇到的,虽过去一段时间还是记忆犹新),摘抄于此,备查:
     1 use master
     2 --九九乘法表
     3 with cte1 as
     4 (
     5   select top 9 ROW_NUMBER()over(order by getdate())as RN from sysobjects
     6 )
     7 ,cte2 as
     8 (
     9 select A.RN,B.Va,ROW_NUMBER()over(PARTITION by RN order by getdate())as RC 
    10 from 
    11 (select * from cte1)as A 
    12    outer apply(select case when A.RN<=RN 
    13    then 
    14      (ltrim(A.RN)+'X'+ltrim(RN)+'='+LTRIM(A.RN*RN))
    15    else '' end as Va 
    16    from cte1 ) as B
    17 )
    18 select * from cte2 a
    19 pivot
    20 (
    21    max(Va)
    22    for RN in([1],[2],[3],[4],[5],[6],[7],[8],[9])
    23 )as B
    View Code

          结果如图:

             六、以下程序:1)比较select和print的区别;

                      2)看出用isnull的好处(确切地说保证计算的正确性——将“未赋值”(不是指赋空值)转换为相应的值)(类似还有一个关键字coalesce)
     1 declare 
     2 @a int,
     3 @w decimal(2,1),
     4 @m decimal(2,1),
     5 @q char(3)
     6 set @a=null
     7 set @w=3.12
     8 set @q=null
     9 set @m=3.15
    10 print @m   --3.2
    11 select @m   --3.2
    12 select @a,@w,@q   --null 3.1 null
    13 print @a   --
    14 select @a+@w  --null
    15 select ISNULL(@a,0)+ISNULL(@w,0)  --3.1
    16 print @a+@w  --null
    17 print ISNULL(@a,0)+ISNULL(@w,0)  --3.1
    View Code

                 结果如图:

     

           七、领悟count(*)和count(字段)的含义,null和‘’还是很有区别的。
     1 select COUNT(*) from mchk where psfx not like ''                            --9564
     2 select COUNT(*) from mchk                                                             --12182                 -----三数相差637(psfx为null)
     3 select COUNT(*) from mchk where psfx is null                           --637
     4 select COUNT(*) from mchk where psfx is not null                         --11545
     5 select COUNT(*) from mchk                                               --12182
     6  
     7 select COUNT(psfx) from mchk where psfx like ''                            --1981
     8 select COUNT(psfx) from mchk where psfx not like ''                            --9564
     9 select COUNT(psfx) from mchk                                               --11545
    10 
    11 select COUNT(psfx) from mchk where psfx is null                          --0
    12 select COUNT(psfx) from mchk where psfx is not null                         --11545
    13 select COUNT(psfx) from mchk                                               --11545
    View Code

    上述说明psfx没有赋任何值的有637(12182-11545)条记录。这个结果验证可以用isnull()或者coalesce()函数。(截图略)

    八、sql语句查看一个数据库的位置或者查看有多少实例?

    1 --sql语句查看一个数据库的位置
    2 select name,filename,crdate,cmptlevel,version 
    3 from master.dbo.sysdatabases
    View Code

           结果如图:

  • 相关阅读:
    linux查看进程命令
    linux修改时区的命令
    深度学习VGG16模型核心模块拆解
    Python之format()函数
    os.path.isfile()的正确用法(正确用法)
    Python time.time()方法
    tf.train.Saver()
    Python3字典update()方法
    图解Numpy的tile函数
    池化层的作用和种类
  • 原文地址:https://www.cnblogs.com/zhangbc/p/3433143.html
Copyright © 2011-2022 走看看