select 在 sql db2 中从日期到日期之间的每一天 1 行
select 1 row for each day between from to until date in sql db2
我们的积分系统有一个古老的遗产 table,如下所示:
from date
until date
points monday
points tuesday
points wednesday
points thursday
points friday
6-12-2021
10-12-2021
10
30
20
15
5
13-12-2021
13-12-2021
10
0
0
0
0
现在对于 power bi 和我们的分析,我们想要创建一个 select 查询来生成如下结果:
date
points
6-12-2021
10
7-12-2021
30
8-12-2021
20
9-12-2021
15
10-12-2021
5
13-12-2021
10
在 db2 中如何完成这样的事情?
在此先感谢您的帮助!
您可以使用递归 CTE 列出所有日期,然后DECODE the result of DAYOFWEEK_ISO设置分数
with table1 (fromdate, untildate, monday, tuesday, wednesday, thursday, friday) as (
values
(date '2021-12-06', date '2021-12-10', 10, 30, 20, 15, 5),
(date '2021-12-13', date '2021-12-13', 10, 0, 0, 0, 0)
),
alldates (fromdate, untildate, monday, tuesday, wednesday, thursday, friday, points_date) as (
select table1.*, fromdate as points_date from table1
union all
select fromdate, untildate, monday, tuesday, wednesday, thursday, friday, points_date + 1 day from alldates where points_date < untildate
)
select
points_date, decode(dayofweek_iso(points_date), 1, monday, 2, tuesday, 3, wednesday, 4, thursday, 5, friday) points
from alldates
order by points_date
我们的积分系统有一个古老的遗产 table,如下所示:
from date | until date | points monday | points tuesday | points wednesday | points thursday | points friday |
---|---|---|---|---|---|---|
6-12-2021 | 10-12-2021 | 10 | 30 | 20 | 15 | 5 |
13-12-2021 | 13-12-2021 | 10 | 0 | 0 | 0 | 0 |
现在对于 power bi 和我们的分析,我们想要创建一个 select 查询来生成如下结果:
date | points |
---|---|
6-12-2021 | 10 |
7-12-2021 | 30 |
8-12-2021 | 20 |
9-12-2021 | 15 |
10-12-2021 | 5 |
13-12-2021 | 10 |
在 db2 中如何完成这样的事情?
在此先感谢您的帮助!
您可以使用递归 CTE 列出所有日期,然后DECODE the result of DAYOFWEEK_ISO设置分数
with table1 (fromdate, untildate, monday, tuesday, wednesday, thursday, friday) as (
values
(date '2021-12-06', date '2021-12-10', 10, 30, 20, 15, 5),
(date '2021-12-13', date '2021-12-13', 10, 0, 0, 0, 0)
),
alldates (fromdate, untildate, monday, tuesday, wednesday, thursday, friday, points_date) as (
select table1.*, fromdate as points_date from table1
union all
select fromdate, untildate, monday, tuesday, wednesday, thursday, friday, points_date + 1 day from alldates where points_date < untildate
)
select
points_date, decode(dayofweek_iso(points_date), 1, monday, 2, tuesday, 3, wednesday, 4, thursday, 5, friday) points
from alldates
order by points_date