/**
结果:
classid id dpname
1 1 办公室
1 2 财务室
1 3 经理室
2 1 软件部
2 2 硬件部
3 1 客服一部
3 2 客服二部想实现如下效果,相同的classid只在第一行显示
classid id dpname
1 1 办公室
2 财务室
3 经理室
2 1 软件部
2 硬件部
3 1 客服一部
2 客服二部
**/Declare @t table(classid int,code int,dpname varchar(20))
Insert into @t
Select 1,1,'办公室'
Union all
Select 1,2,'财务室'
Union all
Select 1,3,'经理室'
Union all
Select 2,1,'软件部'
Union all
Select 2,2,'硬件部'
Union all
Select 3,1,'客服一部'
Union all
Select 3,2,'客服二部'Select * From @t
2000 o5?
Insert into @t
Select 1,1,'办公室'
Union all
Select 1,2,'财务室'
Union all
Select 1,3,'经理室'
Union all
Select 2,1,'软件部'
Union all
Select 2,2,'硬件部'
Union all
Select 3,1,'客服一部'
Union all
Select 3,2,'客服二部'Select *,ID=IDENTITY(INT,1,1) INTO #T From @t
SELECT
CASE WHEN
NOT EXISTS(SELECT 1 FROM #T WHERE classid=T.classid AND ID<T.ID) THEN LTRIM(classid) ELSE '' END classid,
CODE,dpname
FROM #T Tclassid CODE dpname
------------ ----------- --------------------
1 1 办公室
2 财务室
3 经理室
2 1 软件部
2 硬件部
3 1 客服一部
2 客服二部(所影响的行数为 7 行)
id,dpname
from table1 a
order by classid,id
Insert into @t
Select 1,1,'办公室'
Union all
Select 1,2,'财务室'
Union all
Select 1,3,'经理室'
Union all
Select 2,1,'软件部'
Union all
Select 2,2,'硬件部'
Union all
Select 3,1,'客服一部'
Union all
Select 3,2,'客服二部'Select
case code when 1 then ltrim(classid) else '' end as classid,
code,
dpname
From @t------------------------1 1 办公室
2 财务室
3 经理室
2 1 软件部
2 硬件部
3 1 客服一部
2 客服二部