単にGroup by
とMax
でJoin
を使用Min
は次のデモとして機能します。 -
デモ: -
Create table #LeaveMain ([user] nvarchar(10), RequestNo int)
insert into #LeaveMain values ('user1', 123456)
Create table #LeaveDetail (RequestNo int, [Date] date)
insert into #LeaveDetail values (123456, '01/01/2017')
insert into #LeaveDetail values (123456, '01/02/2017')
insert into #LeaveDetail values (123456, '01/03/2017')
select
Max(D.RequestNo) RequestNo ,
Max([User]) [User] ,
Convert(nvarchar(10),Min([Date])) + ' - '
+ Convert(nvarchar(10),Max([Date])) [Leave Date]
from #LeaveMain M inner join #LeaveDetail D
on M.RequestNo = D.RequestNo
group by D.RequestNo
結果: -
RequestNo | User | Leave Date
123456 | user1 | 01/01/2017 - 01/03/2017