回答編集履歴
2
暦上の週数で行分割したSQLを追記
test
CHANGED
@@ -257,3 +257,125 @@
|
|
257
257
|
| 5 | 09/26 | 09/27 | 09/28 | 09/29 | 09/30 | 10/01 | 10/02 |
|
258
258
|
|
259
259
|
| 6 | 10/03 | 10/04 | 10/05 | 10/06 | 10/07 | 10/08 | |
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
日曜日を起点( 区切り )としながらも、暦上の週数で行を分割する例はこちら。
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
```SQL
|
270
|
+
|
271
|
+
WITH params AS
|
272
|
+
|
273
|
+
(
|
274
|
+
|
275
|
+
SELECT DATE '2021-09-01' _start_ -- 変更可
|
276
|
+
|
277
|
+
, DATE '2021-10-08' _end_ -- 変更可
|
278
|
+
|
279
|
+
)
|
280
|
+
|
281
|
+
SELECT row_number() over() row
|
282
|
+
|
283
|
+
, format_datetime( '%Y年%m月', min( ymd ) ) yymm
|
284
|
+
|
285
|
+
, div(
|
286
|
+
|
287
|
+
extract( day from ymd ) - extract( dayofweek from ymd ) + 13
|
288
|
+
|
289
|
+
, 7
|
290
|
+
|
291
|
+
) wnum
|
292
|
+
|
293
|
+
, max(
|
294
|
+
|
295
|
+
case extract( dayofweek from ymd ) when 1 then ymd end
|
296
|
+
|
297
|
+
) sun
|
298
|
+
|
299
|
+
, max(
|
300
|
+
|
301
|
+
case extract( dayofweek from ymd ) when 2 then ymd end
|
302
|
+
|
303
|
+
) mon
|
304
|
+
|
305
|
+
, max(
|
306
|
+
|
307
|
+
case extract( dayofweek from ymd ) when 3 then ymd end
|
308
|
+
|
309
|
+
) tue
|
310
|
+
|
311
|
+
, max(
|
312
|
+
|
313
|
+
case extract( dayofweek from ymd ) when 4 then ymd end
|
314
|
+
|
315
|
+
) wed
|
316
|
+
|
317
|
+
, max(
|
318
|
+
|
319
|
+
case extract( dayofweek from ymd ) when 5 then ymd end
|
320
|
+
|
321
|
+
) thu
|
322
|
+
|
323
|
+
, max(
|
324
|
+
|
325
|
+
case extract( dayofweek from ymd ) when 6 then ymd end
|
326
|
+
|
327
|
+
) fri
|
328
|
+
|
329
|
+
, max(
|
330
|
+
|
331
|
+
case extract( dayofweek from ymd ) when 7 then ymd end
|
332
|
+
|
333
|
+
) sat
|
334
|
+
|
335
|
+
FROM unnest(
|
336
|
+
|
337
|
+
generate_date_array(
|
338
|
+
|
339
|
+
( SELECT _start_ FROM params )
|
340
|
+
|
341
|
+
, ( SELECT _end_ FROM params )
|
342
|
+
|
343
|
+
, interval 1 day
|
344
|
+
|
345
|
+
)
|
346
|
+
|
347
|
+
) ymd
|
348
|
+
|
349
|
+
GROUP BY date_trunc( ymd, month )
|
350
|
+
|
351
|
+
, wnum
|
352
|
+
|
353
|
+
ORDER BY min( ymd ) ;
|
354
|
+
|
355
|
+
```
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
■結果
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
| row | yymm | wnum | sun | mon | tue | wed | thu | fri | sat |
|
366
|
+
|
367
|
+
|:---:|:----------:|:----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
|
368
|
+
|
369
|
+
| 1 | 2021年9月 | 1 | | | | 09/01 | 09/02 | 09/03 | 09/04 |
|
370
|
+
|
371
|
+
| 2 | 2021年9月 | 2 | 09/05 | 09/06 | 09/07 | 09/08 | 09/09 | 09/10 | 09/11 |
|
372
|
+
|
373
|
+
| 3 | 2021年9月 | 3 | 09/12 | 09/13 | 09/14 | 09/15 | 09/16 | 09/17 | 09/18 |
|
374
|
+
|
375
|
+
| 4 | 2021年9月 | 4 | 09/19 | 09/20 | 09/21 | 09/22 | 09/23 | 09/24 | 09/25 |
|
376
|
+
|
377
|
+
| 5 | 2021年9月 | 5 | 09/26 | 09/27 | 09/28 | 09/29 | 09/30 | | |
|
378
|
+
|
379
|
+
| 6 | 2021年10月 | 1 | | | | | | 10/01 | 10/02 |
|
380
|
+
|
381
|
+
| 7 | 2021年10月 | 2 | 10/03 | 10/04 | 10/05 | 10/06 | 10/07 | 10/08 | |
|
1
横持ちSQLを追記
test
CHANGED
@@ -135,3 +135,125 @@
|
|
135
135
|
| 5 | 2021/09/26 | 2021/10/02 | 7 | 日 - 土 | 2021/09/26 | 2021/10/02 |
|
136
136
|
|
137
137
|
| 6 | 2021/10/03 | 2021/10/08 | 6 | 日 - 金 | 2021/10/03 | 2021/10/09 |
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
### 追記
|
142
|
+
|
143
|
+
2021/08/23 08:13 に返信いただいたコメントから推理した
|
144
|
+
|
145
|
+
日付を横持ちにするだけの```SQL```です。
|
146
|
+
|
147
|
+
```SQL
|
148
|
+
|
149
|
+
WITH params AS
|
150
|
+
|
151
|
+
(
|
152
|
+
|
153
|
+
SELECT DATE '2021-09-01' _start_ -- 変更可
|
154
|
+
|
155
|
+
, DATE '2021-10-08' _end_ -- 変更可
|
156
|
+
|
157
|
+
)
|
158
|
+
|
159
|
+
SELECT row_number() over() row
|
160
|
+
|
161
|
+
, max(
|
162
|
+
|
163
|
+
case extract( dayofweek from ymd ) when 1 then ymd end
|
164
|
+
|
165
|
+
) sun
|
166
|
+
|
167
|
+
, max(
|
168
|
+
|
169
|
+
case extract( dayofweek from ymd ) when 2 then ymd end
|
170
|
+
|
171
|
+
) mon
|
172
|
+
|
173
|
+
, max(
|
174
|
+
|
175
|
+
case extract( dayofweek from ymd ) when 3 then ymd end
|
176
|
+
|
177
|
+
) tue
|
178
|
+
|
179
|
+
, max(
|
180
|
+
|
181
|
+
case extract( dayofweek from ymd ) when 4 then ymd end
|
182
|
+
|
183
|
+
) wed
|
184
|
+
|
185
|
+
, max(
|
186
|
+
|
187
|
+
case extract( dayofweek from ymd ) when 5 then ymd end
|
188
|
+
|
189
|
+
) thu
|
190
|
+
|
191
|
+
, max(
|
192
|
+
|
193
|
+
case extract( dayofweek from ymd ) when 6 then ymd end
|
194
|
+
|
195
|
+
) fri
|
196
|
+
|
197
|
+
, max(
|
198
|
+
|
199
|
+
case extract( dayofweek from ymd ) when 7 then ymd end
|
200
|
+
|
201
|
+
) sat
|
202
|
+
|
203
|
+
FROM unnest(
|
204
|
+
|
205
|
+
generate_date_array(
|
206
|
+
|
207
|
+
( SELECT _start_ FROM params )
|
208
|
+
|
209
|
+
, ( SELECT _end_ FROM params )
|
210
|
+
|
211
|
+
, interval 1 day
|
212
|
+
|
213
|
+
)
|
214
|
+
|
215
|
+
) ymd
|
216
|
+
|
217
|
+
GROUP BY date_add(
|
218
|
+
|
219
|
+
date_sub( ymd, interval extract( dayofweek from ymd ) day )
|
220
|
+
|
221
|
+
, interval 1 day
|
222
|
+
|
223
|
+
)
|
224
|
+
|
225
|
+
, date_add(
|
226
|
+
|
227
|
+
date_sub( ymd, interval extract( dayofweek from ymd ) day )
|
228
|
+
|
229
|
+
, interval 7 day
|
230
|
+
|
231
|
+
)
|
232
|
+
|
233
|
+
ORDER BY min( ymd ) ;
|
234
|
+
|
235
|
+
```
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
■結果
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
| row | sun | mon | tue | wed | thu | fri | sat |
|
246
|
+
|
247
|
+
|:---:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
|
248
|
+
|
249
|
+
| 1 | | | | 09/01 | 09/02 | 09/03 | 09/04 |
|
250
|
+
|
251
|
+
| 2 | 09/05 | 09/06 | 09/07 | 09/08 | 09/09 | 09/10 | 09/11 |
|
252
|
+
|
253
|
+
| 3 | 09/12 | 09/13 | 09/14 | 09/15 | 09/16 | 09/17 | 09/18 |
|
254
|
+
|
255
|
+
| 4 | 09/19 | 09/20 | 09/21 | 09/22 | 09/23 | 09/24 | 09/25 |
|
256
|
+
|
257
|
+
| 5 | 09/26 | 09/27 | 09/28 | 09/29 | 09/30 | 10/01 | 10/02 |
|
258
|
+
|
259
|
+
| 6 | 10/03 | 10/04 | 10/05 | 10/06 | 10/07 | 10/08 | |
|