请原谅我的英文并提前感谢您!The following table is given:CREATE TABLE Test (idjob int, TeamName nvarchar(50), StartDate datetime, StopDate datetime)insert into Test (idjob, TeamName, StartDate, StopDate) values (1,'team 1','2019-02-20','2019-02-20')insert into Test (idjob, TeamName, StartDate, StopDate) values (2,'team 2','2019-02-21','2019-02-21')insert into Test (idjob, TeamName, StartDate, StopDate) values (3,'team 2','2019-02-22','2019-02-24')insert into Test (idjob, TeamName, StartDate, StopDate) values (4,'team 3','2019-02-22','2019-02-22')I need a query to return the following result:idjob | TeamName | Date1, team 1, '2019-02-20'2, team 2, '2019-02-21'2, team 2, '2019-02-22'2, team 2, '2019-02-23'3, team 2, '2019-02-24'4, team 3, '2019-02-22'a record for each day of the range StartDate and StopDatePls excuse my english and Thank you in advance!推荐答案CREATE TABLE Test (idjob int, TeamName nvarchar(50), StartDate datetime, StopDate datetime)insert into Test (idjob, TeamName, StartDate, StopDate)values (1,'team 1','2019-02-20','2019-02-20'), (2,'team 2','2019-02-21','2019-02-21'), (3,'team 2','2019-02-22','2019-02-24'), (4,'team 3','2019-02-22','2019-02-22')declare @startdate datedeclare @enddate dateSelect @startdate = min(StartDate),@enddate = max(StopDate) from test--**** create a Number table;WITH Num1 (n) AS (SELECT 1 as nUNION ALL SELECT n+1 as nFROM Num1 Where n <101),Num2 (n) AS (SELECT 1 FROM Num1 AS X, Num1 AS Y),Nums (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY n) FROM Num2) --,myidjobDate as(select idjob,dateadd(day,n-1,@startdate) dt from NumsCross join (Select Distinct idjob From test ) awhere dateadd(day,n-1,@startdate)<=@enddate)select t.idjob,TeamName, [Date] from test tcross apply (select idjob, dt from myidjobDate ) d(idjob, [Date])WHERE t.idjob=d.idjob and d.[Date] between StartDate and StopDatedrop TABLE Test 这篇关于我需要查询帮助......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!