Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am very poor in writing sql queryies please help me to get two columns as a result set like
OpenIssues ClosedIssues

50 1

By the following query but i am getting in rows like

OpenIssues
50
1

The query as follows :

SQL
select COUNT(*) as OpenIssues from HX_Issue where ProjectId=1 and ComponentId=13 and  CreatedOn between '2012/12/24' and '2013/01/30' and StatusId !=23
union all
select COUNT(*) as closed from HX_Issue where ProjectId=1 and ComponentId=13 and  CreatedOn between '2012/12/24' and '2013/01/30' and StatusId =23
Posted

1 solution

SQL
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"

SQL
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:
SQL
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

SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Zoltán Zörgő
-- Create date: 2013.01.31
-- =============================================
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:

SQL
EXEC    [dbo].[IssueReport]
        @ProjectId = 1,
        @ComponentId = 13,
        @FromDate = '2012.01.01',
        @ToDate = '2013.02.15'

GO
 
Share this answer
 
v4
Comments
Ankur\m/ 30-Jan-13 7:45am    
Yes, it is that simple... :) 5!
Madhugundi 30-Jan-13 23:05pm    
Thanks for reply i want to one more requirement here is that result set should be month report that i want to show in a Bar Chart;
Month OpenIssues closed
Jan 12 10
Feb 30 25
Zoltán Zörgő 31-Jan-13 0:37am    
Well, you should have put this requirement in the original post... Don't you think?
Madhugundi 31-Jan-13 2:17am    
Thanks to consider my comment and i need it as every month if January is no issues then it should give 0 like for each month.
Thanks
Zoltán Zörgő 31-Jan-13 2:49am    
Again: you should have put this requirement in the original post... Don't you think? And what are you doing to solve your problem? Ah, I know, you ask for help... Well, you could do a little bit more... Since you will get the money for it, not me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900