• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
安安的BLOG
安安目前专注电子商务解决方案^_^
博客园    首页    新随笔    联系   管理    订阅  订阅

自动创建临时表,并给临时表添加一个自动增长的行号字段

  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' 。

posted @ 2006-11-02 18:32  安安  阅读(3243)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3