Sql n错误的结果。 ;WITH D AS ( SELECT @RangeStartDate DateValue UNION ALL SELECT DateValue + 1 FROM D WHERE DateValue + 1 <
n错误的结果。Sql n错误的结果。 ;WITH D AS ( SELECT @RangeStartDate DateValue UNION ALL SELECT DateValue + 1 FROM D WHERE DateValue + 1 < ,sql,sql-server,Sql,Sql Server,n错误的结果。 ;WITH D AS ( SELECT @RangeStartDate DateValue UNION ALL SELECT DateValue + 1 FROM D WHERE DateValue + 1 < @RangeEndDate ) SELECT D.DateValue, COUNT(ISNULL(BugID,0)) FROM D LEFT JOIN Bugs ON D.DateValue >= Bugs.CreatedDate
;WITH D AS
(
SELECT @RangeStartDate DateValue
UNION ALL
SELECT DateValue + 1
FROM D
WHERE DateValue + 1 < @RangeEndDate
)
SELECT D.DateValue, COUNT(ISNULL(BugID,0))
FROM D LEFT JOIN Bugs ON D.DateValue >= Bugs.CreatedDate
AND (D.DateValue <= Bugs.UpdatedDate OR Bugs.UpdatedDate IS NULL)
GROUP BY D.DateValue
WITH D AS
(
SELECT @RangeStartDate DateValue
UNION ALL
SELECT DateValue + 1
FROM D
WHERE DateValue + 1 < @RangeEndDate
)
Select D.DateValue, coalesce(count(BugID),0)
FROM D
LEFT JOIN Bugs B
on B.CreateDate <= D.Datevalue
and (B.UpdateDate >= D.DateValue or B.UpdateDate is null)
Group By D.DateValue
WITH D AS
(
SELECT @RangeStartDate DateValue
UNION ALL
SELECT DateValue + 1
FROM D
WHERE DateValue + 1 < @RangeEndDate
),
SELECT D.DateValue,
(SELECT COUNT(*)
FROM Bugs
WHERE CreatedDate <= D.DateValue
AND (UpdatedDate > D.DateValue
OR UpdatedDate IS NULL)) AS NumberOfBugs
FROM D
declare @bugs table(BUGID int,Createddate datetime,Updateddate datetime,Status char(1))
insert into @bugs
Select 1,'20140101',NULL,'I'
UNION Select 2,'20140102','20140110','U'
UNION Select 3,'20140103','20140110','C'
UNION Select 4,'20140104',NULL,'I'
UNION Select 5,'20140105','20140110','U'
UNION Select 6,'20140106','20140109','C'
UNION Select 10,'20140101','20140110','C'
declare @RangeStartDate datetime
declare @RangeEndDate datetime
select @RangeStartDate ='20140101'
select @RangeEndDate ='20140201'
;WITH D AS
(
SELECT @RangeStartDate DateValue
UNION ALL
SELECT DateValue + 1
FROM D
WHERE DateValue + 1 < @RangeEndDate
)
Select D.* ,
(Select SUM(dd) from
(Select 1 as DD from @bugs b where b.Createddate<=d.DateValue and ((b.Status<>'C') or (b.Status='C' and b.Updateddate>=d.DateValue)) ) a)
from D
DECLARE @RangeStartDate datetime2 = '20120201'
DECLARE @RangeEndDate datetime2 = '20120228'
;WITH D AS (
SELECT @RangeStartDate DateValue
UNION ALL
SELECT DATEADD(d, 1, DateValue)
FROM D
WHERE DATEADD(d, 1, DateValue) <= @RangeEndDate
)
SELECT DateValue
, BugsOpen = COUNT(CASE WHEN DateValue < COALESCE(UpdatedDate, '99991231')
THEN 1
ELSE NULL
END)
, BugsClose = COUNT(CASE WHEN DateValue >= COALESCE(UpdatedDate, '99991231')
THEN 1
ELSE NULL
END)
FROM D
LEFT JOIN Bugs ON DateValue >= CreatedDate
GROUP BY DateValue
if OBJECT_ID('tempdb..#Bugs') is not null
drop table #Bugs
create table #Bugs
(
BugID int identity not null primary key clustered,
BugName varchar(10) not null,
CreatedDate datetime not null default getdate(),
UpdatedDate datetime null,
IsClosed bit not null default 0
)
insert #Bugs(BugName, CreatedDate, UpdatedDate)
select 'Bug 1', GETDATE(), null union all
select 'Bug 2', GETDATE() - 3, null union all
select 'Bug 3', GETDATE() -5, GETDATE() -3 union all
select 'Bug 4', getdate()-8, GETDATE() - 7
select * from #Bugs
declare @RangeStart datetime = getdate() - 10;
WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select DATEADD(day, N, @RangeStart), *
from cteTally t
cross apply
(
select COUNT(*) as OpenBugCount
from #Bugs
where CreatedDate <= DATEADD(day, N, @RangeStart)
AND (UpdatedDate >= DATEADD(day, N, @RangeStart) OR UpdatedDate is null)
) b
where t.N <= DATEDIFF(day, @RangeStart, getdate());