sql server 2005 中的分区函数用法(partition by 字段)
2010-09-03 23:46
489 查看
以下例子根据Item 分组后.排序.
应用例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if exists(select 1 from sys.objects where object_id=object_id('tab'))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
drop table tab
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table tab(item int, date datetime, temp int)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
insert tab select 10,'2006-01-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 10,'2006-02-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 10,'2006-03-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 20,'2006-01-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 20,'2006-02-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-01-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-02-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-03-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-04-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-05-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select *,row_number() over(partition by item order by date ) as t from tab
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
item date temp t
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
----------- ----------------------- ----------- --------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
10 2006-01-01 00:00:00.000 0 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
10 2006-02-01 00:00:00.000 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
10 2006-03-01 00:00:00.000 0 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
20 2006-01-01 00:00:00.000 0 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
20 2006-02-01 00:00:00.000 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-01-01 00:00:00.000 0 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-02-01 00:00:00.000 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-03-01 00:00:00.000 0 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-04-01 00:00:00.000 0 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-05-01 00:00:00.000 0 5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(10 行受影响)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
应用例子如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if exists(select 1 from sys.objects where object_id=object_id('tab'))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
drop table tab
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table tab(item int, date datetime, temp int)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
insert tab select 10,'2006-01-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 10,'2006-02-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 10,'2006-03-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 20,'2006-01-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 20,'2006-02-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-01-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-02-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-03-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-04-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
union all select 30,'2006-05-01',0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select *,row_number() over(partition by item order by date ) as t from tab
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
item date temp t
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
----------- ----------------------- ----------- --------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
10 2006-01-01 00:00:00.000 0 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
10 2006-02-01 00:00:00.000 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
10 2006-03-01 00:00:00.000 0 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
20 2006-01-01 00:00:00.000 0 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
20 2006-02-01 00:00:00.000 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-01-01 00:00:00.000 0 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-02-01 00:00:00.000 0 2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-03-01 00:00:00.000 0 3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-04-01 00:00:00.000 0 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
30 2006-05-01 00:00:00.000 0 5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(10 行受影响)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
相关文章推荐
- sql server 2005 中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005 中的分区函数用法(partition by 字段)
- sql server 2005 中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005中的分区函数用法(partition by 字段)
- sql server 2005 中的列置换函数用法(pivot 字段)
- sqlserver中分区函数 partition by的用法
- SQL Server 2005 中 ROW_NUMBER() 函数的简单用法
- SQL Server 2005------分区表和分区函数
- sql server 2005 使用clr函数压缩ntext类型字段
- sqlserver中分区函数 partition by的用法
- 【转】SQL Server 2005------分区表和分区函数