标签: recursive-cte

递归查询中的数字溢出:Teradata

我是teradata的新手.我想在表格中插入1到1000的数字test_seq,如下所示.

create table test_seq(
    seq_id integer
);
Run Code Online (Sandbox Code Playgroud)

在这个网站上搜索后,我想出了recusrive查询来插入数字.

insert into test_seq(seq_id)
with recursive cte(id) as (
    select 1 from test_dual
    union all
    select id + 1 from cte
    where id + 1 <= 1000
    )
select id from cte;
Run Code Online (Sandbox Code Playgroud)

test_dual创建如下,它只包含一个值.(像Oracle中的DUAL)

create table test_dual(
    test_dummy varchar(1)
);

insert into test_dual values ('X');
Run Code Online (Sandbox Code Playgroud)

但是,当我运行insert语句时,我收到错误, Failure 2616 Numeric overflow occurred during computation.

我在这做错了什么?是不是integer数据类型足以容纳数值1000?另外,有没有办法编写查询,以便我可以取消test_dual表?

sql teradata recursive-cte

3
推荐指数
1
解决办法
3018
查看次数

使用像while循环一样的递归公用表表达式

我有下表

   Log

 Date            date
 Description     varchar
 ID              integer
Run Code Online (Sandbox Code Playgroud)

给定日期作为参数,我必须找到否.或者每天从开始日期到使用递归cte后的一个月记录的日志数. 有些天可能没有任何日志,所以我必须将计数打印为0.

例如:

  select * from Log
Run Code Online (Sandbox Code Playgroud)

回报

  1   insert      2011-01-17
  2   blah blah   2011-01-23
  3   blah        2011-07-07
Run Code Online (Sandbox Code Playgroud)

对于2011-01-17作为输入,输出应该是

   2011-01-17    1
   2011-01-18    0
   2011-01-19    0
   ....
   2011-01-23    1
   .....
   2011-02-17    0
Run Code Online (Sandbox Code Playgroud)

我必须使用递归 cte来做那个.我不知道如何在每次递归中将日期递增1以及如何停止\终止递归.

这是我到目前为止所做的事情:

 with cte as    (
    select '2011-01-17' as dat,count(*) as count 
from log group by date 
having date='2011-01-17' 

union all 

select dateadd(day,1,dat) as dat,count(*) as count 
from log,cte 
group by date 
having date=dateadd(day,1,dat) 
where dat<'2011-02-17' …
Run Code Online (Sandbox Code Playgroud)

sql t-sql common-table-expression sql-server-2008 recursive-cte

2
推荐指数
1
解决办法
2373
查看次数

修改CTE的SQLite查询

这是我的查询:

    WITH desc_table(counter, hourly, current_weather_description, current_icons, time_stamp) AS (
Select count(*) AS counter, CASE WHEN  strftime('%M',  'now') < '30' 
                THEN strftime('%H', 'now')  
                ELSE strftime('%H', time_stamp, '+1 hours') END as hourly, 
                current_weather_description,
                current_icons,
                time_stamp
                From weather_events
                GROUP BY strftime('%H',  time_stamp, '+30 minutes'), current_weather_description
                UNION ALL
                Select count(*) as counter, hourly - 1, current_weather_description, current_icons, time_stamp
                From weather_events
                GROUP BY strftime('%H',  time_stamp, '+30 minutes'), current_weather_description
                Order By counter desc limit 1
                ),
        avg_temp_table(avg_temp, hour_seg, time_stamp) AS (
        select avg(current_temperatures) as avg_temp, CASE …
Run Code Online (Sandbox Code Playgroud)

sql sqlite android common-table-expression recursive-cte

2
推荐指数
1
解决办法
424
查看次数

对按公共键分组的 n 行数据使用 string_agg()

我想string_agg(column_name,',')在 SQL 结果集中的列中使用一些 ID。我将按单个键进行分组,但也想确保每个分组集都会导致 n 行被输入到函数string_agg(column_name,',')

例子:

create table #temp (first_name varchar(20), data_id char(3))
insert into #temp (first_name, data_id) values('jeff', 'XX1')
insert into #temp (first_name, data_id) values('jeff', 'X23')
insert into #temp (first_name, data_id) values('jeff', 'X87')
insert into #temp (first_name, data_id) values('jeff', 'X09')
insert into #temp (first_name, data_id) values('jeff', 'X15')

insert into #temp (first_name, data_id) values('bob', 'X76')
insert into #temp (first_name, data_id) values('bob', 'X17')
insert into #temp (first_name, data_id) values('bob', 'X98')
insert into #temp (first_name, data_id) values('bob', …
Run Code Online (Sandbox Code Playgroud)

sql sql-server common-table-expression recursive-cte

2
推荐指数
1
解决办法
673
查看次数