星期六结束两周的时间结束

时间:2015-02-06 17:08:47

标签: sql tsql

我已经四处乱逛,看到很多关于按周分组的话题。我需要分组在星期六结束的2周。 http://sqlfiddle.com/#!6/e2fea/1/0

CREATE TABLE contacts (
         Total Numeric(9,2)
         created DATE
       );

INSERT INTO contacts (Total,created) VALUES('100','2009-01-01');
INSERT INTO contacts (Total,created) VALUES('50','2009-01-02');
INSERT INTO contacts (Total,created) VALUES('100','2009-01-03');
INSERT INTO contacts (Total,created) VALUES('50','2009-01-04');
INSERT INTO contacts (Total,created) VALUES('75','2009-01-05');
INSERT INTO contacts (Total,created) VALUES('150','2009-01-06');
INSERT INTO contacts (Total,created) VALUES('170','2009-01-07');
INSERT INTO contacts (Total,created) VALUES('111','2009-01-08');
INSERT INTO contacts (Total,created) VALUES('125','2009-01-09');
INSERT INTO contacts (Total,created) VALUES('151','2009-01-10');
INSERT INTO contacts (Total,created) VALUES('128','2009-01-11');
INSERT INTO contacts (Total,created) VALUES('175','2009-01-12');
INSERT INTO contacts (Total,created) VALUES('100','2009-01-13');
INSERT INTO contacts (Total,created) VALUES('50','2009-01-14');
INSERT INTO contacts (Total,created) VALUES('75','2009-01-15');
INSERT INTO contacts (Total,created) VALUES('150','2009-01-16');
INSERT INTO contacts (Total,created) VALUES('170','2009-01-17');
INSERT INTO contacts (Total,created) VALUES('111','2009-01-18');
INSERT INTO contacts (Total,created) VALUES('125','2009-01-19');
INSERT INTO contacts (Total,created) VALUES('151','2009-01-20');
INSERT INTO contacts (Total,created) VALUES('128','2009-01-21');
INSERT INTO contacts (Total,created) VALUES('175','2009-01-22');
INSERT INTO contacts (Total,created) VALUES('100','2009-01-23');
INSERT INTO contacts (Total,created) VALUES('50','2009-01-24');
INSERT INTO contacts (Total,created) VALUES('75','2009-01-25');
INSERT INTO contacts (Total,created) VALUES('150','2009-01-26');
INSERT INTO contacts (Total,created) VALUES('170','2009-01-27');
INSERT INTO contacts (Total,created) VALUES('111','2009-01-28');
INSERT INTO contacts (Total,created) VALUES('125','2009-01-29');
INSERT INTO contacts (Total,created) VALUES('151','2009-01-30');
INSERT INTO contacts (Total,created) VALUES('128','2009-01-31');
INSERT INTO contacts (Total,created) VALUES('111','2009-02-01');
INSERT INTO contacts (Total,created) VALUES('125','2009-02-02');
INSERT INTO contacts (Total,created) VALUES('151','2009-02-03');
INSERT INTO contacts (Total,created) VALUES('128','2009-02-04');

我知道01/03/2009是我希望从星期六开始的。我想总结总计,然后分组前两周显示结束日期,如果截止周六尚未过去,则显示截至当前日期的总数。

Total   Date
250     1/3/2009
1680    1/17/2009
1750    1/31/2009
515     2/4/2009

这只是一个月,输出实际上将持续到当前日期。

2 个答案:

答案 0 :(得分:0)

这并没有给出您正在寻找的答案,但它可能有助于您找到它:

SELECT sum(total), round(datepart(wk, created) / 2, 0)
    FROM contacts
    GROUP BY round(datepart(wk, created) / 2, 0)

答案 1 :(得分:0)

找到一种方法将每个日期放入两周忽略多年的丛中。知道引用两周或更少总数的日期也很好,就像你在预期产出中所显示的那样。

SELECT  Max( c.Created ) as ReferenceDate, Sum( c.Total ) SampleTotal,
        count(*) as DaysInSample
FROM    contacts c
group by DatePart( wk, c.Created ) / 2;

SQL Fiddle

如果它是一个完全过去的丛,Max将返回领先星期六的日期。否则,它将获取最近一次输入的日期。我在每个采样期间包括一个"天数"指标可以轻松识别完整的两周样本。