我在prestodb
中有这种表格,其中包含start_date
字段和增量计数器。
| num | start_date | val |
|-----|------------|-----|
| 2 | 2017-03-01 | 100 |
| 3 | 2017-03-05 | 233 |
如何使用presto
sql
将其转换为此类表?
| date | val |
|------------|-----|
| 2017-03-01 | 100 |
| 2017-03-02 | 100 |
| 2017-03-05 | 233 |
| 2017-03-06 | 233 |
| 2017-03-07 | 233 |
谢谢!
答案 0 :(得分:2)
select date_add ('day',s.n,t.start_date) as date
,t.val
from mytable t
cross join unnest(sequence(0,num-1)) s (n)
;
date | val
------------+-----
2017-03-01 | 100
2017-03-02 | 100
2017-03-05 | 233
2017-03-06 | 233
2017-03-07 | 233
答案 1 :(得分:-1)
试试Sql Server
Declare @s_id int = 1
;WITH dates AS
(
SELECT start_date as Date,@s_id as times,num as value,val
from @t
UNION ALL
SELECT DATEADD(d,1,[Date]),times+1 as times,value,val
FROM dates
WHERE times < value
)
select Date,val from dates
order by val,date
请注意,可以使用...
创建此语句的测试表declare @t ( num int,start_date date,val int)
insert into @t values (2,'2017-03-01',100)
insert into @t values (3,'2017-03-05',233)
在针对您的表测试主要语句时,请将主语句中的@t
替换为您的表名。
下图显示了针对测试表运行主语句的结果......