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
-以显示特定日期数据的
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
-显示直到运行总时间特定日期数据
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
我想显示运行总计直到日期和今天(当天)在一个SQL查询结果记录.....
作为连接有两个差的条件。
预期的结果应该是这样的
编辑:我可以用这些查询和加入他们实现这一目标的结果,但我希望做这个任务在一个单一的查询,而不是两个查询
我目前的方式,可以在这里查看。 对不起,这么长的问题,但我认为它必须要了解的问题其实..
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