2012-08-23 66 views
0

我現在有一個給定表的頂部X結果的列表,對於給定的一天,'頂部'被定義爲具有最高聚合計數的結果。我希望能夠看到的不僅僅是那一天的計數,而且它與昨天的計數以及上週計數的平均值相比如何。獲取上週每個結果的計數摘要

這裏是我當前的查詢:

SELECT foo, bar, SUM(count) as today 
FROM tablename 
WHERE col1 = 'asdf' and date = '2012-08-23' 
GROUP BY foo, bar 
ORDER BY count desc 
LIMIT 5 

結果看起來是這樣的:

|foo  | bar  | today | 
|something1 | something3 | 2345 | 
|something2 | something4 | 1234 | 
.... 

但我想有是這樣的:

|foo  | bar  | today | yesterday | week_avg | 
|something1 | something3 | 2345 | 2273  | 2193  | 
|something2 | something4 | 1234 | 935  | 1001  | 
.... 

至於獎金點我不介意在某種GROUP_CONCAT結果的最後20天的所有日期(所以我可以ea sily用它做一個迷你圖)

+0

你怎麼有2個不同的'week_avg'的,如果你正在尋找一個星期? – Kermit

回答

3

你可以從上週的所有行,然後使用條件聚合(通過CASE expressions)只總結當天的count值和昨天的count值:

SELECT foo, 
     bar, 
     SUM(CASE WHEN date = CURDATE() THEN count ELSE 0 END) AS today, 
     SUM(CASE WHEN date = CURDATE() - INTERVAL 1 DAY THEN count ELSE 0 END) AS yesterday, 
     AVG(count) AS week_avg 
FROM  tablename 
WHERE col1 = 'asdf' AND 
     date >= CURDATE() - INTERVAL 1 WEEK 
GROUP BY foo, 
     bar 
ORDER BY today DESC 
LIMIT 5 

最後,我們訂購通過今天的count值的總和並根據該順序選擇前五名。

+0

這看起來很簡單,並且接近我所需要的,除非我必須執行sum(count)而不是count,因爲我的表有額外的列,可能會或可能不會分組(例如foo,bar,baz等)。所以在上面的例子中,如果我不選擇和按baz分組,我的count!= sum(count)。 – jlivni

0

一旦你準備好了包含日期和日期金額的彙總表,並且假設你正在尋找與昨天的差異,那麼可以使用COALESCE。該tbl下表是對您彙總表這看起來是這樣的:

SELECT date, foo, bar, SUM(count) as today 
FROM tablename 
WHERE col1 = 'asdf' 
GROUP BY date, foo, bar 
ORDER BY count desc 
LIMIT 5 

查詢

SELECT a.ddate, a.amount AS today, 
COALESCE(a.amount - 
    (SELECT b.amount 
    FROM tbl b 
    WHERE b.ddate = a.ddate - INTERVAL 1 DAY), a.amount) AS yesterday 
FROM tbl a 

fiddle

0

您的第一個問題。試試這個:

SELECT day0.foo, day0.bar, 
     day0.addup as today, 
     day1.addup as yesterday, 
     week0.addup/7 as week_avg 
    FROM 
    ( 
    SELECT foo, bar, SUM(count) as addup 
     FROM tablename 
    WHERE col1 = 'asdf' 
     AND date >= CURDATE() 
     AND date < CURDATE() + INTERVAL 1 DAY 
    GROUP BY foo, bar 
) day0 
    JOIN ( 
    SELECT foo, bar, SUM(count) as addup 
     FROM tablename 
    WHERE col1 = 'asdf' 
     AND date >= CURDATE() - INTERVAL 1 DAY 
     AND date < CURDATE() 
    GROUP BY foo, bar 
) day1 ON (day0.foo = day1.foo AND day0.bar=day1.bar) 
    JOIN ( 
    SELECT foo, bar, SUM(count) as addup 
     FROM tablename 
    WHERE col1 = 'asdf' 
     AND date >= CURDATE() - INTERVAL 7 DAY 
     AND date < CURDATE() 
    GROUP BY foo, bar 
) week0 ON (day0.foo = week0.foo AND day0.bar = week0.bar) 
ORDER BY day0.addup desc 
    LIMIT 5 

它看起來很毛茸茸,但它是三個不同的彙總查詢的連接,每天一個。

你的第二個問題:

SELECT foo, bar, GROUP_CONCAT(addup) 
    FROM (
    SELECT foo, bar, 
      DATEDIFF(CURDATE(),date) AS daysago, 
      SUM(count) as addup 
     FROM tablename 
    WHERE col1 = 'asdf' 
     AND date >= CURDATE() - INTERVAL 20 DAY 
     AND date < CURDATE())A 
    GROUP BY foo, bar, date 
    ORDER BY foo, bar, date 
+0

第一個是詳細的,但我得到它,它的工作原理(我不得不切換到左外部聯接來處理結果,有些日期沒有計數)。仍在繼續努力...... – jlivni