select
(select COUNT(*) from HX_Issue where ProjectId=1 and ComponentId=13 and CreatedOn between '2012/12/24' and '2013/01/30' and StatusId !=23) as OpenIssues
,
(select COUNT(*) from HX_Issue where ProjectId=1 and ComponentId=13 and CreatedOn between '2012/12/24' and '2013/01/30' and StatusId =23) as closed
Update: based on "new requirement"
select
distinct DATEPART(month, exq.CreatedOn)
(select COUNT(*) from HX_Issue inq where inq.ProjectId=1 and inq.ComponentId=13 and DATEPART(month, inq.CreatedOn)=DATEPART(month, exq.CreatedOn) and inq.CreatedOn between '2012/12/24' and '2013/01/30' and StatusId !=23) as OpenIssues,
(select COUNT(*) from HX_Issue inq where inq.ProjectId=1 and inq.ComponentId=13 and DATEPART(month, inq.CreatedOn)=DATEPART(month, exq.CreatedOn) inq.CreatedOn between '2012/12/24' and '2013/01/30' and StatusId =23) as ClosedIssues,
from HX_Issue exq
where ProjectId=1 and ComponentId=13 and exq.CreatedOn between '2012/12/24' and '2013/01/30'
Or even better:
select
DATEPART(month, CreatedOn) as month,
COUNT(case when StatusId!=23 then 1 else null end) as OpenIssues,
COUNT(case when StatusId=23 then 1 else null end) as ClosedIssues
from HX_Issue where ProjectId=1 and ComponentId=13
and CreatedOn between '2012/12/24' and '2013/01/30'
group by DATEPART(month, CreatedOn)
Update2: based on even newer requirements
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[IssueReport]
@ProjectId int,
@ComponentId int,
@FromDate Date,
@ToDate Date
AS
BEGIN
SET NOCOUNT ON;
with DateCte as
(
select cast(@FromDate as datetime) DateValue
union all
select DateValue + 1
from DateCte
where DateValue + 1 <= @ToDate
)
select
DATEPART(year, DateValue) as year,
DATEPART(month, DateValue) as month,
(select COUNT(*) from HX_Issue where ProjectId=@ProjectId and ComponentId=@ComponentId and DATEPART(month, CreatedOn)=DATEPART(month, DateValue) and DATEPART(year, CreatedOn)=DATEPART(year, DateValue) and StatusId !=23) as OpenIssues,
(select COUNT(*) from HX_Issue where ProjectId=@ProjectId and ComponentId=@ComponentId and DATEPART(month, CreatedOn)=DATEPART(month, DateValue) and DATEPART(year, CreatedOn)=DATEPART(year, DateValue) and StatusId =23) as ClosedIssues
from DateCte
group by DATEPART(year, DateValue), DATEPART(month, DateValue)
OPTION (MAXRECURSION 0)
END
And here is the usage sample:
EXEC [dbo].[IssueReport]
@ProjectId = 1,
@ComponentId = 13,
@FromDate = '2012.01.01',
@ToDate = '2013.02.15'
GO