假设有查询到的数据为
ID
1
1
1
2
2
2
3
3
3
.
.
.
如何把查询到的数据变为
1
2
3
.
.
.
1
2
3
.
.
.
请知道的朋友不吝赐教啊SQL 排序数据
ID
1
1
1
2
2
2
3
3
3
.
.
.
如何把查询到的数据变为
1
2
3
.
.
.
1
2
3
.
.
.
请知道的朋友不吝赐教啊SQL 排序数据
解决方案 »
- 对你们来说简单的,对我来说难的问题!
- 很简单的遍历语句,请高手指点~
- shinesky(找天的人) talantlee(風) 各50分
- mysql显示乱码(php+mysql) 在线等!
- 一个简单的存储过程的问题
- 还是如何替换text 字段
- SQL SERVER7.0 的标准版和桌面版有什么区别?
- SQL SERVER 2005安装错误提示:性能监视器计数器要求
- EXEC sp_executesql @tablesql 和 EXEC(@tablesql)的差别?
- 跨数据库访问,update语句无法正确执行
- 四表联合查询写报表求sql语句
- 奇怪为什么此语句在查询分析器执行计划里面显示的是index scan,而不是index seek?
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-14 14:09:05
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ID] int)
insert [huang]
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3
--------------开始查询--------------------------select ROW_NUMBER()OVER (PARTITION BY id ORDER BY id)id
from [huang]
----------------结果----------------------------
/*
id
--------------------
1
2
3
1
2
3
1
2
3
*/
insert into #tab
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3select ROW_NUMBER()over(partition by id order by id)as num
,* from #tab-----------------------------------------------------------------
num id
-------------------- -----------
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3(9 行受影响)
看看这样行不行。
create table lh(ID int)insert into lh
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3
select ID,
row_number() over(partition by ID order by getdate()) 'New_ID'
from lh/*
ID New_ID
----------- --------------------
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3(9 row(s) affected)
*/
declare @tb table
(
id int
)insert into @tb
select 1 union all
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3
select id
from
(
select *,
ROW_NUMBER() over(partition by id order by id) as rownum
from @tb
)a
order by rownum,id
/*
id
1
2
3
1
2
3
1
2
3
*/
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-14 15:21:41
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int)
insert [huang]
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3 union all
select 4 union all
select 4 union all
select 4 union all
select 4 union all
select 4 union all
select 5 union all
select 5
--------------开始查询--------------------------select *-- ,ROW_NUMBER() OVER (PARTITION BY id ORDER BY id)
from [huang]
ORDER BY ROW_NUMBER() OVER (PARTITION BY id ORDER BY id),id
----------------结果----------------------------
/*
id
-----------
1
2
3
4
5
1
2
3
4
5
3
4
4
4
*/
declare @tb table
(
id int
)insert into @tb
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3 union all
select 4 union all
select 4 union all
select 4 union all
select 4 union all
select 4 union all
select 5 union all
select 5
/*
id
1
2
3
4
5
1
2
3
4
5
3
4
4
4
*/
select id
from
(
select *,
ROW_NUMBER() over(partition by id order by id) as rownum
from @tb
)a
order by rownum,id
/*
id
1
2
3
4
5
1
2
3
4
5
3
4
4
4
*/