Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 具有多个条件的序列创建_Sql_Postgresql - Fatal编程技术网

Sql 具有多个条件的序列创建

Sql 具有多个条件的序列创建,sql,postgresql,Sql,Postgresql,如何生成一个系列,该系列将写入新列0001,并且在第28行之后仅更改为0002,然后在第35行之后更改为0003。然后将在第28行之后再次写入0004,以此类推 Example: Row_num New_Column 1 0001 2 0001 . . . . 28 0001 29 0002 . . . . 55 0002 56

如何生成一个系列,该系列将写入新列0001,并且在第28行之后仅更改为0002,然后在第35行之后更改为0003。然后将在第28行之后再次写入0004,以此类推

Example:

Row_num   New_Column

1         0001
2         0001
.          .
.          .
28        0001
29        0002
.          .
.          .
55        0002
56        0003
.          .
.          .
90        0003
91        0004

这是一个简单但有点肤浅的方法

insert into to_period(New_column)
case when row_num=>1 and row_num<29 then '0001' --28
when row_num=>29 and row_num<57 then "0002" --28
when row_num=>57 and row_num<92 then "0003" --35
when row_num=>92 and row_num<120 then "0004" --28
when row_num=>120 and row_num<148 then "0005" --28
when row_num=>148 and row_num<183 then "0006" --35
when row_num=>183 and row_num<211 then "0007" --28
when row_num=>211 and row_num<239 then "0008" --28
when row_num=>239 and row_num<274 then "0009" --35
when row_num=>274 and row_num<302 then "00010" --28
when row_num=>302 and row_num<330 then "00011" --28
when row_num=>330 and row_num<365 then "00012" --35
end;
插入到to_期间(新_列)

当row_num=>1和row_num29和row_num57和row_num92和row_num120和row_num148和row_num183和row_num211和row_num239和row_num274和row_num302和row_num330和row_num时,可以使用模运算。对于您提出的问题(在28和35处切换):

对于仅以28的倍数切换的问题:

select val, 1 + floor((val - 1) / 28)
from generate_series(1, 100, 1) gs(val) ;

您对所需内容的文本描述与示例数据不一致,这似乎意味着每隔28行更改
New\u列的值。请解释一下,我不这么认为。在将新_列的值从0003更改为0004之前,请参阅第_num 56行和第90行,这意味着有35行
select val, 1 + floor((val - 1) / 28)
from generate_series(1, 100, 1) gs(val) ;