2015-02-06 64 views
0

我已經圍着周圍看到很多關於分組的話題。我需要在週六結束的兩週之前進行分組。 http://sqlfiddle.com/#!6/e2fea/1/0星期六結束時間爲2週期間組

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是週六,我想從啓動。我想總計總數,然後在前2周顯示結束日期的分組,如果週六結束還沒有結束,則顯示總計到當前日期。

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

這只是一個月,輸出實際上會持續到當前日期。

+0

這些是你的類型嗎? varchar和日期? – 2015-02-06 17:34:52

+0

不,我很抱歉,錯過了當我創建SQL小提琴表...它實際上是數字(9,2)和日期是日期。 – Viperwolfmn 2015-02-06 17:39:05

+0

和你的SQL版本在小提琴上是否正確? – 2015-02-06 17:42:12

回答

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返回主導週六的日期。否則,它會獲取最新條目的日期。我列入了「每個採樣週期的天數」指標,以便輕鬆識別完整的兩週採樣。

+0

正是我在找什麼......上個星期五我有一個大腦崩潰的原因。謝謝。 – Viperwolfmn 2015-02-10 13:54:42

0

這不會給正是你要找的答案,但它可能會幫助你找到它:

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

'code' SELECT sum(total) ,DateAdd(day,-1 * datepart(dw,created) ,創建) FROM contacts GROUP BY DateAdd(day,-1 * datepart(dw,created),created)'code'這是一個開始,但它按一週和一週的開始分組。我需要將它逆轉並按2周的範圍進行分組。 – Viperwolfmn 2015-02-06 19:26:45

+0

我的代碼執行了兩個星期,但不允許你調整它從哪一天算起。我敢肯定,有一個設置允許的地方,但我不知道它在哪裏... – SarekOfVulcan 2015-02-06 19:32:38

+0

我看到你在說什麼,但是當你開始在幾年內添加時,這是行不通的...嗯 – Viperwolfmn 2015-02-06 21:08:02

相關問題