表结构:
cid type
1 sh
2 sh
3 gj
4 gj
5 bj
6 bj
--------------------------------
需要的效果
cid type
1 sh
1 sh
2 gj
2 gj
3 bj
3 bj
cid type
1 sh
2 sh
3 gj
4 gj
5 bj
6 bj
--------------------------------
需要的效果
cid type
1 sh
1 sh
2 gj
2 gj
3 bj
3 bj
解决方案 »
- 在DTS导入文本数据时,只能导入999行?
- 我有一个存储过程是统计数据,每次统计的时候,CPU的使用率就是100%,但统计完后就恢复正常...
- sql 2005服务器名称问题!(在线)
- 连接数据库,用odbc和用oledb到底有何本质区别呀?有经验的说说.我试了试,没发现任何区别呀?
- 禁用了 Xp_regread 之后无法用Sql代理新建作业,有什么办法?
- 多字段同时过滤筛选的SQL语句咋写?
- 看看该存储过程??问题在哪???????在线等待!小弟没分了!!抱歉!!!!急急
- 底子好的同志来看一下,SQL参数字符串连接的问题.
- 关于datediff的问题
- 请教存储过程中输入参数用getdate()作默认值问题
- sql2005里的表,存储过程,视图怎么迁移到db2里面
- 请问下实现数据从mysql到mssql的定时自动传输有什么好的解决方案
update a
set a.cid = isnull((select min(cid) from tb where [type] = a.[type]),a.cid)
from tb a
declare @t table (cid int,type varchar(2))
insert into @t
select 1,'sh' union all
select 2,'sh' union all
select 3,'gj' union all
select 4,'gj' union all
select 5,'bj' union all
select 6,'bj';with maco as
(
select row_number() over (order by min(cid)) as rid,type from @t group by type
)
select b.* from @t a left join maco b on a.type=b.type
/*
rid type
-------------------- ----
1 sh
1 sh
2 gj
2 gj
3 bj
3 bj
*/
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-08-05 13:02:08
-- 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 Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([cid] int,[type] varchar(2))
insert [tb]
select 1,'sh' union all
select 2,'sh' union all
select 3,'gj' union all
select 4,'gj' union all
select 5,'bj' union all
select 6,'bj'
--------------开始查询--------------------------
select
(select COUNT(distinct type) from tb where cid<=t.cid) as cid,TYPE
from
tb t
----------------结果----------------------------
/* cid TYPE
----------- ----
1 sh
1 sh
2 gj
2 gj
3 bj
3 bj(6 行受影响)
*/