27,579
社区成员
发帖
与我相关
我的任务
分享
UPDATE T
SET typehao= M.typehao
FROM #TEST T
LEFT JOIN
(
SELECT mxid,mon,type,ROW_NUMBER()OVER(PARTITION BY mon ORDER BY GETDATE())typehao
FROM #test
)M
ON T.MXID=M.mxid
SELECT * FROM #TEST
mxid mon type typehao
-------------------- ---- -------------------- -----------
1 01 收 1
2 01 收 2
3 01 收 3
4 01 收 4
5 02 付 1
6 02 付 2
7 02 付 3
8 02 付 4
(8 row(s) affected)
--sql 2000
select mxid,mon,[type],typehao=(select COUNT(1)+1 from [test] a where a.[type]=[test].[type] and a.mxid<[test].mxid) from [test]
mxid mon type typehao
-------------------- ---- -------------------- --------------------
5 02 付 1
6 02 付 2
7 02 付 3
8 02 付 4
1 01 收 1
2 01 收 2
3 01 收 3
4 01 收 4
(8 行受影响)
select mxid,mon,[type],typehao=ROW_NUMBER()over(partition by type order by mxid asc) from [test]
mxid mon type typehao
-------------------- ---- -------------------- --------------------
5 02 付 1
6 02 付 2
7 02 付 3
8 02 付 4
1 01 收 1
2 01 收 2
3 01 收 3
4 01 收 4
(8 行受影响)
----2005以上可用
SELECT mxid,mon,type,ROW_NUMBER()OVER(PARTITION BY mon ORDER BY GETDATE())typehao
FROM #test
mxid mon type typehao
-------------------- ---- -------------------- --------------------
1 01 收 1
2 01 收 2
3 01 收 3
4 01 收 4
5 02 付 1
6 02 付 2
7 02 付 3
8 02 付 4
(8 row(s) affected)
update a set
typehao=b.typehao
from test as a
join (select mxid,typehao=(select count(*) from test
where a.mon=mon and mxid<=a.mxid)
from test as a) as b
on a.mxid=b.mxid