27,580
社区成员
发帖
与我相关
我的任务
分享
declare @hh table (id int ,mysell nvarchar(200))
insert into @hh
select 1,'asdfsadf' union
select 3,'hfgfhfgh' union
select 5,'sdfsa' union
select 7,'asdfsdfsa' union
select 8,'nhgnfg'
SELECT row_number()
OVER (ORDER BY id )AS Row,*
FROM @hh
確定有都有開戶時:
declare @T table([month] int,[Type] nvarchar(2),[id] int)
Insert @T
select 6,N'开户',2 union all
select 6,N'暂停',4 union all
select 6,N'注销',8 union all
select 7,N'开户',2 union all
select 7,N'暂停',2 union all
select 7,N'注销',2 union all
select 7,N'删除',2
select
[month]=case when [Type]=N'开户' then rtrim([month]) else '' end,
[Type],
[id]
from
@T
month Type id
------------ ---- -----------
6 开户 2
暂停 4
注销 8
7 开户 2
暂停 2
注销 2
删除 2
(7 個資料列受到影響)
declare @T table([month] int,[Type] nvarchar(2),[id] int)
Insert @T
select 6,N'开户',2 union all
select 6,N'暂停',4 union all
select 6,N'注销',8 union all
select 7,N'开户',2 union all
select 7,N'暂停',2 union all
select 7,N'注销',2 union all
select 7,N'删除',2
;with C
as
(
Select *,row=row_number()over(partition by [month] order by [month]) from @T)
select
[month]=case when row=1 then rtrim([month]) else '' end,
[Type],
[id]
from
C
(7 個資料列受到影響)
month Type id
------------ ---- -----------
6 开户 2
暂停 4
注销 8
7 开户 2
暂停 2
注销 2
删除 2
(7 個資料列受到影響)
declare @mm table (month int,Type varchar(4),id int)
insert into @mm
select 6,'开户',2 union all
select 6,'暂停',4 union all
select 6,'注销',8 union all
select 7,'开户',2 union all
select 7,'暂停',2 union all
select 7,'注销',2 union all
select 7,'删除',2
--select * from @mm
UPDATE @mm
SET month=NULL
WHERE Type!='开户'
select * from @mm
/*
在你的结果上处理一下
*/
/*
month Type id
----------- ---- -----------
6 开户 2
NULL 暂停 4
NULL 注销 8
7 开户 2
NULL 暂停 2
NULL 注销 2
NULL 删除 2
*/