日期:2014-05-16 浏览次数:20505 次
select emp_name as 姓名,row_number()over(partition by sex order by getdate()) as 性别序号 from tb
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-03-26 11:30:49
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([emp_id] int,[emp_name] varchar(1),[sex] varchar(2))
insert [tb]
select 1,'a','男' union all
select 2,'b','女' union all
select 3,'c','男' union all
select 4,'d','女'
--------------开始查询--------------------------
select emp_name as 姓名,row_number()over(partition by sex order by emp_id) as 性别序号 from tb ORDER BY emp_name
----------------结果----------------------------
/* 姓名 性别序号
---- --------------------
a 1
b 1
c 2
d 2
(4 行受影响)
*/
select emp_name as '姓名',row_number() over(order by emp_id) '性别序号' from 人员表 where sex = '男'
union all
select emp_name as '姓名',row_number() over(order by emp_id) '性别序号'