質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -167,3 +167,45 @@
|
|
167
167
|
しかし、現状は、現在の日付(今年)は正常に計算しているのですが、昨年の計算処理は全て昨年の最初の日付で全て計算してしまいます。
|
168
168
|
|
169
169
|
(例:2021-06-08と2020-06-08で計算。2021-06-10と2020-06-08で計算。やりたいことは、2021-06-10は2020-06-10で計算。)
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
追記
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
amtは金額
|
180
|
+
|
181
|
+
foreach ($lastDate as $val) {
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
$result = DB::table('posts')
|
186
|
+
|
187
|
+
->when(($val->prev_date == $row->date), function ($query) use ($val) {
|
188
|
+
|
189
|
+
return $query->selectRaw('DATE_FORMAT(created_at, "%Y%m%d") as date')
|
190
|
+
|
191
|
+
->selectRaw('SUM(amt) AS amount')
|
192
|
+
|
193
|
+
->selectRaw('SUM(amt) / SUBDATE(SUM(amt), SUBDATE(DATE_FORMAT(created_at, "%Y%m%d"), INTERVAL 1 YEAR)) AS amount_ratio');
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
})
|
198
|
+
|
199
|
+
->when((!isset($val->prev_date)), function ($query) {
|
200
|
+
|
201
|
+
return $query->selectRaw('DATE_FORMAT(created_at, "%Y%m%d") as date')
|
202
|
+
|
203
|
+
->selectRaw('SUM(amt) AS total_amount')
|
204
|
+
|
205
|
+
->selectRaw('SUM(amt) / 0 AS amount_ratio');
|
206
|
+
|
207
|
+
)}
|
208
|
+
|
209
|
+
->get();
|
210
|
+
|
211
|
+
```
|