declare @temp table (ddate datetime) insert @temp select DATEDIFF(d,0,CONVERT(smalldatetime,'09/30/2012') -Number) from master..spt_values where type='p' and number < DatePart(d,'09/30/2012') order by 1 DECLARE @DeptCode int =1 -- to display of particular date data
select ComplaintMedia_Abbri, ddate,COUNT(ComplaintMedia) as c from Complaint INNER JOIN @temp ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)=convert(datetime,convert(varchar(10),ddate,101),101) WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1 group by ComplaintMedia_Abbri,ddate order by ddate,ComplaintMedia_Abbri -- to display till date particular date data running total
select ComplaintMedia_Abbri,ddate, COUNT(ComplaintMedia_Abbri) as c from Complaint INNER JOIN @temp ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)<=convert(datetime,convert(varchar(10),ddate,101),101) WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1 group by ComplaintMedia_Abbri,ddate I want to show running total till date and today (that day ) records in one sql query results .....
as on join there is two difference conditions.
expected result should be look like this

Edit : I am able to achive this result by using these queries and joining them but i want to do this task in one single query instead of two queries
my current way can be check here . sorry for such long question but i think its necessary to understand question actually ..
declare @temp table (ddate datetime) insert @temp select DATEDIFF(d,0,CONVERT(smalldatetime,'09/30/2012') -Number) from master..spt_values where type='p' and number < DatePart(d,'09/30/2012') order by 1 --select * from @temp SELECT * FROM (select ddate,ISNULL(L,0) AS Letter, ISNULL(P,0) AS Phone, ISNULL(E,0) AS Email, ISNULL(W,0) AS WEB FROM ( select ComplaintMedia_Abbri, ddate,COUNT(ComplaintMedia) as c from Complaint INNER JOIN @temp ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)=convert(datetime,convert(varchar(10),ddate,101),101) WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1 group by ComplaintMedia_Abbri,ddate ) p pivot (SUM(c) FOR ComplaintMedia_Abbri IN (E,W,L,P)) AS pvt ) AS [A] INNER JOIN ( select ddate,ISNULL(L,0) AS LetterTot, ISNULL(P,0) AS PhoneTot, ISNULL(E,0) AS EmailTot, ISNULL(W,0) AS WEBTot FROM ( select ComplaintMedia_Abbri,ddate, COUNT(ComplaintMedia_Abbri) as c from Complaint INNER JOIN @temp ON convert(datetime,convert(varchar(10),ComplaintDate,101),101)<=convert(datetime,convert(varchar(10),ddate,101),101) WHERE isnull(Receivedby_Dept,Relatesto_Dept)=1 group by ComplaintMedia_Abbri,ddate ) p pivot (SUM(c) FOR ComplaintMedia_Abbri IN (E,W,L,P)) AS pvt ) AS [B] ON A.ddate=B.ddate order by A.ddate