我正在获取第一张图片但需要第二张图片的语法。
**Yvalue Xvalue**
**380** 04/21/2014 * # of People on 4/21/2014
487 04/21/2014 *Total
**68** 04/28/2014 * # of People on 4/28/2014
487 04/28/2014 *Total
**25** 05/05/2014 * # of People on 5/5/2014
487 05/05/2014 *Total
**Yvalue Xvalue**
**380** 04/21/2014 * # of People through 4/21/2014
487 04/21/2014 *Total
**448** 04/28/2014 * # of People through 4/28/2014
487 04/28/2014 *Total
**473** 05/05/2014 * # of People through 5/5/2014
487 05/05/2014 *Total
Yvalue和Xvalue由以下代码派生。我目前正在接收某一天给出的人数。我希望记录某一天的人数。我想要StartDate和Xvalue日期之间的所有人的总和。
select COUNT(contact_username) AS Yvalue ,
convert(varchar(10) , det.sent_to_print_shop, 101 ) AS Xvalue
FROM FWIA_Bundler_Log_Bucket_Details det
WHERE convert(varchar(10),det.sent_to_print_shop,101) >= '++StartDate++'
AND convert(varchar(10),det.sent_to_print_shop,101) <= '++EndDate++'
GROUP BY convert(varchar(10),det.sent_to_print_shop,101)
答案 0 :(得分:0)
如果您使用的是SQLServer 2012或更高版本,则可以使用SUM() OVER (ORDER BY)
SELECT SUM(1) OVER(ORDER BY det.sent_to_print_shop) AS Yvalue ,
convert(varchar(10) , det.sent_to_print_shop, 101 ) AS Xvalue
FROM FWIA_Bundler_Log_Bucket_Details det
WHERE convert(varchar(10),det.sent_to_print_shop,101) >= '++StartDate++'
AND convert(varchar(10),det.sent_to_print_shop,101) <= '++EndDate++'
GROUP BY convert(varchar(10),det.sent_to_print_shop,101)
如果您使用较旧版本的SQLServer来执行运行总计,则需要自行加入
SELECT COUNT(c.contact_username) AS Yvalue ,
convert(varchar(10) , det.sent_to_print_shop, 101 ) AS Xvalue
FROM FWIA_Bundler_Log_Bucket_Details det
INNER JOIN FWIA_Bundler_Log_Bucket_Details c
ON det.sent_to_print_shop >= c.det.sent_to_print_shop
WHERE convert(varchar(10),det.sent_to_print_shop,101) >= '++StartDate++'
AND convert(varchar(10),det.sent_to_print_shop,101) <= '++EndDate++'
GROUP BY convert(varchar(10),det.sent_to_print_shop,101)
这个想法很合理,但查询未经测试,可能需要进行一些调整
答案 1 :(得分:0)
如果您运行的是SQL Server 2012,则可以使用窗口函数执行此操作。鉴于此表:
id dtSubmitted userName cnt
-- ----------------------- -------- ---
1 2014-01-01 14:23:00.000 john 10
2 2014-01-01 15:23:00.000 sally 15
3 2014-01-01 16:45:00.000 abe 5
4 2014-01-02 10:00:00.000 mac 25
5 2014-01-02 12:43:00.000 hector 5
6 2014-01-02 17:54:00.000 emily 75
7 2014-01-03 14:42:00.000 emily 125
8 2014-01-03 19:58:00.000 meg 75
你可以说
select DateSubmitted = convert(date,t.dtSubmitted) ,
Total = sum( t.cnt ) ,
RunningTotal = sum( sum(t.cnt) ) over (
order by convert(date,t.dtSubmitted)
rows between unbounded preceding and current row
)
from foo t
group by convert(date,t.dtSubmitted)
order by 1
要看到这个:
DateSubmitted Total RunningTotal
------------- ----- ------------
2014-01-01 30 30
2014-01-02 105 135
2014-01-03 200 335
在您的情况下,查询将如下所示:
declare @StartDate date = '++StartDate++'
declare @EndDate date = '++EndDate++'
select [Date] = convert(date,t.sent_to_print_shop) ,
Total = count( t.contact_user_name ) ,
RunningTotal = sum(count( t.contact_user_name )) over (
order by convert(date,t.sent_to_print_shop)
rows between unbounded preceding and current row
)
from FWIA_Bundler_Log_Bucket_Details t
where t.sent_to_print_shop >= @StartDate
and t.sent_to_print_shop < @EndDate
group by convert(date,t.sent_to_print_shop)
编辑注意...... *
据说如果你有SQL Server 2008 R2,那么下面的东西应该可以工作(强调应该,因为我没有2008 R2实例可以使用):
select DateSubmitted = d.DateSubmitted ,
Total = d.Total ,
RunningTotal = sum( d.Total ) over ( order by convert(date,t.dtSubmitted) )
from ( select DateSubmitted = convert(date,t.dtSubmitted) ,
Total = sum( t.cnt )
from foo t
group by convert(date,t.dtSubmitted)
) d
order by d.DateSubmitted
另一种技术是使用相关的子查询,如下所示:
with
daily_summary as
(
select DateSubmitted = convert(date,dtSubmitted) ,
Total = sum(cnt)
from foo
)
select DateSubmitted = curr.DateSubmitted ,
Total = curr.Total ,
Rollup = sum( prev.Total )
from daily_summary curr
join daily_summary prev on prev.DateSubmitted <= curr.DateSubmitted
group by curr.DateSubmitted , curr.Total
order by curr.DateSubmitted
祝你好运!