34,590
社区成员
发帖
与我相关
我的任务
分享
-sql2005 修改一下
select BillNo ,BH_ID ,SortCode ,row_number() over (partition by billNo,BH_ID order by BillNo asc) order1
from tb
--sql2005
select BillNo ,BH_ID ,SortCode ,row_number() over (partiton by billNo,BH_ID order by BillNo asc) order1
from tb
partiton
--sql2000
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([BillNo] int,[BH_ID] varchar(2),[SortCode] int,[Order1] sql_variant)
insert [tb]
select 3,'C2',1001,null union all
select 3,'C2',1003,null union all
select 3,'C2',1004,null union all
select 3,'C2',1006,null union all
select 10,'C5',1002,null union all
select 10,'C5',1003,null union all
select 10,'C5',1004,null union all
select 35,'C2',1006,null union all
select 35,'C2',1007,null union all
select 35,'C2',1009,null union all
select 35,'C2',1013,null union all
select 35,'C2',1016,null
select BillNo ,BH_ID ,SortCode,
(select count(*)+1 from tb where [BillNo]=t1.[BillNo] and [BH_ID] =t1.[BH_ID] and [SortCode]<t1.[SortCode]) as order1
from tb t1
/*
BillNo,BH_ID,SortCode,order1
3,C2,1001,1
3,C2,1003,2
3,C2,1004,3
3,C2,1006,4
10,C5,1002,1
10,C5,1003,2
10,C5,1004,3
35,C2,1006,1
35,C2,1007,2
35,C2,1009,3
35,C2,1013,4
35,C2,1016,5
(12 行受影响)
select BillNo ,BH_ID ,SortCode,(select count(*)+1 from tb where [BillNo]=t1.[BillNo] and [SortCode]<t1.[SortCode]) as order1
from tb t1
/*
BillNo,BH_ID,SortCode,order1
3,C2,1001,1
3,C2,1003,2
3,C2,1004,3
3,C2,1006,4
10,C5,1002,1
10,C5,1003,2
10,C5,1004,3
35,C2,1006,1
35,C2,1007,2
35,C2,1009,3
35,C2,1013,4
35,C2,1016,5
(12 行受影响)
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-23 10:32:42
-- Verstion:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([BillNo] int,[BH_ID] varchar(2),[SortCode] int,[Order1] sql_variant)
insert [tb]
select 3,'C2',1001,null union all
select 3,'C2',1003,null union all
select 3,'C2',1004,null union all
select 3,'C2',1006,null union all
select 10,'C5',1002,null union all
select 10,'C5',1003,null union all
select 10,'C5',1004,null union all
select 35,'C2',1006,null union all
select 35,'C2',1007,null union all
select 35,'C2',1009,null union all
select 35,'C2',1013,null union all
select 35,'C2',1016,null
--------------开始查询--------------------------
select *, Order1=(select count(BillNo) from tb where BillNo=t.BillNo and BH_ID=t.BH_ID and SortCode<=t.SortCode) from tb t
----------------结果----------------------------
/*
(12 行受影响)
BillNo BH_ID SortCode Order1 Order1
----------- ----- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
3 C2 1001 NULL 1
3 C2 1003 NULL 2
3 C2 1004 NULL 3
3 C2 1006 NULL 4
10 C5 1002 NULL 1
10 C5 1003 NULL 2
10 C5 1004 NULL 3
35 C2 1006 NULL 1
35 C2 1007 NULL 2
35 C2 1009 NULL 3
35 C2 1013 NULL 4
35 C2 1016 NULL 5
(12 行受影响)
*/
select *, Order1=(select count(distinct BillNo)+1 from tb where BillNo=t.BillNo and SortCode<=t.SortCode) from tb t
select BillNo ,BH_ID ,SortCode ,row_number() over (partiton by billNo,BH_ID order by BillNo asc) order1
from tb