2016-09-14 98 views
-1

我有一個查詢,我在一個範圍內每天運行多次。我想這減少到單個查詢:MYSQL Group按天數總和每天

SELECT SUM(amount) AS all_time_revenue 
FROM charges WHERE DATE(`charges`.`created_at`) <= '2015-03-01' 

我遇到一個問題,試圖將其轉換成一個單一的查詢 - 我不能GROUP BY「created_at」因爲我之前拍攝的所有費用和日期。 (不只是那天)。不知道這是否可能,但我敢肯定,如果這裏有一位天才能解決這個問題。注意:如果我可以爲每一行返回myDate以及總和也會很好。

下面是我在做什麼一個完整的例子:

$start_time = new DateTime('2016-08-01'); 
$end_time = new DateTime('2016-08-14'); 
$data = []; 
for ($start_time; $start_time < $end_time; $start_time = $start_time->modify('+1 day')) { 
    $date = $start_time->format("Y-m-d"); 

    $results = DB::SELECT("SELECT SUM(amount) AS all_time_revenue 
          FROM charges 
          WHERE `charges`.`created_at` <= ? AND `charges`.`sandbox`=0", 
          ", [$date]); 
    $data[$date] = $results[0]->all_time_revenue; 
} 

所以最好我想運行可以用日期範圍或天間隔的一個日期和數量,工作單查詢有它吐出像 行:

row 1: ['2016-08-01', 4000.00] 
row 2: ['2016-08-02', 4500.00] 
... 
row 14:['2016-08-14', 15000.00] 
+2

請參閱http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-查詢 – Strawberry

+0

這並不清楚'created_at'的數據類型是...是DATE嗎? DATETIME? $ mydate提供了哪些價值範圍? – spencer7593

+0

謝謝,因爲我不知道@Strawberry在抱怨什麼。 –

回答

0

我覺得下面的查詢表示你在找什麼。根據需要,它可在日期範圍上運行。這是一個生成累計和的單個查詢(從日期範圍中給出的開始日期總結)。如果你想從時代開始完成總結,那也可以完成。

create table tt (dt date, i integer); 

insert into tt values("2016-01-01",1); 

insert into tt values("2016-01-02",1); 
insert into tt values("2016-01-02",2); 

insert into tt values("2016-01-03",1); 
insert into tt values("2016-01-03",2); 
insert into tt values("2016-01-03",3); 

insert into tt values("2016-01-04",1); 
insert into tt values("2016-01-04",2); 
insert into tt values("2016-01-04",3); 
insert into tt values("2016-01-04",4); 

insert into tt values("2016-01-05",1); 
insert into tt values("2016-01-05",2); 
insert into tt values("2016-01-05",3); 
insert into tt values("2016-01-05",4); 
insert into tt values("2016-01-05",5); 

insert into tt values("2016-01-06",1); 
insert into tt values("2016-01-06",2); 
insert into tt values("2016-01-06",3); 
insert into tt values("2016-01-06",4); 
insert into tt values("2016-01-06",5); 
insert into tt values("2016-01-06",6); 

set @csum := 0; 
select date_format(dt,'%Y-%m-%d') dt,@csum:[email protected]+i as csum 
from (
    select dt,sum(i) as i 
    from tt 
    where dt between date('2016-01-02') and date('2016-01-05') 
    group by dt 
) as t1 
order by 1; 

|   dt | csum | 
|------------|------| 
| 2016-01-02 | 3 | 
| 2016-01-03 | 9 | 
| 2016-01-04 | 19 | 
| 2016-01-05 | 34 | 

如果你想獲得累加和從時代開始,...

set @csum := 0; 
select date_format(dt,'%Y-%m-%d') dt,@csum:[email protected]+i as csum 
from (
    select dt,sum(i) as i 
    from tt 
    where dt<=date('2016-01-05') 
    group by dt 
) as t1 
where dt<=date('2016-01-05') and date('2016-01-05') 
order by 1; 

|   dt | csum | 
|------------|------| 
| 2016-01-01 | 1 | 
| 2016-01-02 | 4 | 
| 2016-01-03 | 10 | 
| 2016-01-04 | 20 | 
| 2016-01-05 | 35 | 
+0

再次感謝,但我相信它還是有點關閉。鑑於上述數據和日期範圍。 2016-01-03 - > 2016-01-05。我想要返回的金額如下:'['2016-01-03'=> 10,'2016-01-04'=> 20,'2016-01-05'=> 35]' –

+0

我是認爲我可以通過將其分解爲2個查詢來做到這一點。例如** 2016-01-03 - > 2016-01-05 **的日期範圍**:第一個會在** 2016-01-03 **之前及之前獲得總費用。第二個會使用您的累計和技術,用於** 2016-01-04 - 2016-01-05 **並使用一點PHP來追加正確的總數。這仍然比我在大日期範圍內進行的方式更有效率。 –

+0

如果您有依賴項,可以通過將它分解爲兩個查詢來更好地滿足,那很好。您將使用第一個查詢計算到2016-01-03的總和;然後使用該值作爲第二個查詢中計算累計和的引導程序。我認爲你正朝着正確的方向前進。 – blackpen