嘗試這個
create table #temp
(
REF_CORP_ID varchar(20),
DEPARTMENT varchar(20),
IS_APPROVED bit,
APPROVED_BY smallint,
APPROVED_DATETIME datetime,
APPROVAL_FROM smallint,
CREATED_BY smallint
)
create table #temp2
( ROW int,
REF_CORP_ID varchar(20),
DEPARTMENT varchar(20),
IS_APPROVED bit,
APPROVED_BY smallint,
APPROVED_DATETIME datetime,
APPROVAL_FROM smallint,
CREATED_BY smallint
)
insert into #temp (REF_CORP_ID, DEPARTMENT, IS_APPROVED, APPROVED_BY, APPROVED_DATETIME, APPROVAL_FROM, CREATED_BY)
select '2', 'Sales', 1, 1 , '2013-07-05 18:19:31.917', 2 , 1
insert into #temp
select '2', 'Sales', 1, 1 , '2013-07-05 18:19:31.917', 2 , 1
insert into #temp
select '2', 'Sales', 1, 1 , '2013-07-05 18:19:31.917', 2 , 1
insert into #temp
select '1', 'IT', 1, 1 , '2013-07-05 18:05:21.170', 2 , 1
insert into #temp
select '1', 'IT', 1, 1 , '2013-07-05 18:05:21.170', 2 , 1
insert into #temp
select '1', 'IT', 1, 1 , '2013-07-05 18:05:21.170', 2 , 1
insert into #temp
select '3', 'Testing', 0, 1 , '2013-07-05 18:32:02.207', 2 , 1
insert into #temp
select '3', 'Testing', 0, 1 , '2013-07-05 18:32:02.207', 2 , 1
insert into #temp
select '4', 'HR', 1, 1 , '2013-07-08 11:14:42.817', 2 , 1
insert into #temp
select '4', 'HR', 1, 1 , '2013-07-08 11:14:42.817', 2 , 1
insert into #temp2
select ROW_NUMBER() OVER(PARTITION BY REF_CORP_ID ORDER BY REF_CORP_ID DESC) AS Row,
REF_CORP_ID,
DEPARTMENT
,IS_APPROVED
,APPROVED_BY
,APPROVED_DATETIME
,APPROVAL_FROM
,CREATED_BY
from #temp
select
(CASE WHEN row >1 THEN '' ELSE REF_CORP_ID END)REF_CORP_ID,
(CASE WHEN row >1 THEN '' ELSE DEPARTMENT END)DEPARTMENT,
IS_APPROVED
,APPROVED_BY
,APPROVED_DATETIME
,APPROVAL_FROM
,CREATED_BY
from #temp2
drop table #temp
drop table #temp2
查詢的格式化應該像 – user1920832