zoukankan      html  css  js  c++  java
  • 自动创建临时表,并给临时表添加一个自动增长的行号字段

      create table #pageindex(id int identity(1,1) not null,nid int)
      set rowcount @PageUpperBound
      --读取符合记录的id放到临时表中
      INSERT INTO #pageindex(nid)
      SELECT C.ConID
      FROM tbl_EmpContract as C INNER JOIN
                 tbl_Employee as E ON
                 C.EmpID = E.EmpID INNER JOIN
                 tbl_DepartmtList as D ON E.DepID = D.DepID
      where
                     (C.ConState = '未审核')


    select IDENTITY(int, 1,1) AS ID_Num,gdsid,ordersn into #gds_day_sell_temp
     from gds_day_sell

    select * from #gds_day_sell_temp

    还可以这样写:ID_Num = IDENTITY(int, 1, 1)

    也可以是其他类型:
    IDENTITY(smallint, 100, 1) AS job_num


    加上这个 去掉 重复
    FROM (Select TOP 100 Percent * FROM [BarginSaleArea] ) [BarginSaleArea]

    新写sql server的分页sql如下:
    declare @PageLowerBound int
    declare @PageUpperBound int
    set @PageLowerBound=(@pageindex-1)*@pagesize
    set @PageUpperBound=@PageLowerBound+@pagesize
    set rowcount @PageUpperBound
    SELECT IDENTITY(int, 1,1) AS id,g.gdsmst_gdsid as nid into #pageindex
      FROM tbl_EmpContract as C INNER JOIN
                 tbl_Employee as E ON
                 C.EmpID = E.EmpID INNER JOIN
                 tbl_DepartmtList as D ON E.DepID = D.DepID
      where
                     (C.ConState = '未审核')
                  -- and (E.EmpState = '在岗')
       AND (D.DepSN LIKE @DepSN+'%')
       AND (E.EmpName LIKE '%'+@EmpName+'%')--查询
       AND (E.EmpIDSN LIKE '%'+@EmpIDSN+'%')--查询
       AND (D.DepName LIKE '%'+@DepName+'%')--查询
      --读取记录完结
    SELECT
           C.ConID,
           C.ConName,
           C.ConNumber,
           C.ConState,
           C.ConKinds,
           C.ConBeginDate,
           C.ConEndDate,
           C.ConSignCompany,
           C.ConSignDate,
           C.ConTemplet,
           C.ConTryWorkDate,
           C.ConExeg,
           E.EmpIDSN,
           E.EmpName,
           dbo.GetDepAllName(E.DepID) as DepName,
           E.EmpSex,
           E.EmpBirthday,
           E.EmpFunction,
           D.DepSN,
           cast(datediff(yy,E.EmpBirthday,getDate())+1 as int) EmpAge,
     C.ConSort
    FROM tbl_EmpContract as C INNER JOIN
          tbl_Employee as E ON
          C.EmpID = E.EmpID INNER JOIN
          tbl_DepartmtList as D ON E.DepID = D.DepID,#pageindex as p
      where (C.ConID = p.nid)
       and (p.id>@PageLowerBound)
                   and (p.id<=@PageUpperBound)
                      order by p.id
     END
    SET NOCOUNT OFF
    GO


    GROUP BY 的几种用法

    Group by 是SQL Server 中常用的一种语法,语法如下:

    [ GROUP BY [ ALL ] group_by_expression [ ,...n ]
        [ WITH { CUBE | ROLLUP } ]
    ] 
    1、最常用的就是这种语法,如下:
    Select CategoryID, AVG(UnitPrice), COUNT(UnitPrice) 
    FROM dbo.Products Where UnitPrice > 30
    GROUP BY CategoryID
    ORDER BY CategoryID DESC
    这个语句查询出,所有产品分类的产品平均单价,单价计数。并且单价在 30 以上的记录。
    2、再看看这种语法,如下:
    Select CategoryID, AVG(DISTINCT UnitPrice), COUNT(DISTINCT UnitPrice) 
    FROM dbo.Products Where UnitPrice > 30
    GROUP BY CategoryID
    ORDER BY CategoryID DESC
    使用 DISTINCT 的时候,将会去除重复的价格平均单价。
    3、如果希望在分类统计之后,再使用条件过滤,下面的语句可以做为参数:
    Select CategoryID, SUM(UnitPrice) AS SumPrice
    FROM dbo.Products
    GROUP BY CategoryID
    HAVING SUM(UnitPrice) > 300
    HAVING 与 Where 语句类似,Where 是在分类之前过滤,而 HAVING 是在分类之后过滤。
    它和 Where 一样使用 AND、OR、NOT、LIKE 组合使用。
    4、如果希望再在分类统计中,添加汇总行,可以使用以下语句:
    Select CategoryID, SUM(UnitPrice), GROUPING(CategoryID) AS 'Grouping'
    FROM dbo.Products
    GROUP BY CategoryID WITH ROLLUP
    Grouping 这一列用于标识出哪一行是汇总行。
    它使用 ROLLUP 操作添加汇总行。
    5、如果使用 WITH CUBE 将会产生一个多维分类数据集,如下:
    Select CategoryID, SupplierID, SUM(UnitPrice) AS SumPrice
    FROM dbo.Products
    GROUP BY CategoryID, SupplierID WITH CUBE
    它会产生一个交叉表,产生所有可能的组合汇总。
    6、使用 ROLLUP CUBE 会产生一个 NULL 空值,可以使用以下语法解决,如下:
    Select CASE WHEN (GROUPING(SupplierID) = 1) THEN '-1'
                ELSE SupplierID
           END AS SupplierID,
           SUM(UnitPrice) AS QtySum
    FROM dbo.Products
    GROUP BY SupplierID WITH CUBE
    它首先检查当前行是否为汇总行,如果是就可以设置一个值,这里设置为 '-1' 。

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood
    zoj 2913 Bus Pass(BFS)
    ZOJ 1008 Gnome Tetravex(DFS)
    POJ 1562 Oil Deposits (DFS)
    zoj 2165 Red and Black (DFs)poj 1979
    hdu 3954 Level up
    sgu 249 Matrix
    hdu 4417 Super Mario
    SPOJ (BNUOJ) LCM Sum
    hdu 2665 Kth number 划分树
  • 原文地址:https://www.cnblogs.com/anan/p/548237.html
Copyright © 2011-2022 走看看