Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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,我有下表 CREATE TABLE public.af01 ( id integer NOT NULL DEFAULT nextval('af01_id_seq'::regclass), idate timestamp without time zone, region text, city text, vtype text, vmake text, vmodel text, vregno text, intime time without time zone

我有下表

CREATE TABLE public.af01
(
  id integer NOT NULL DEFAULT nextval('af01_id_seq'::regclass),
  idate timestamp without time zone,
  region text,
  city text,
  vtype text,
  vmake text,
  vmodel text,
  vregno text,
  intime time without time zone,
  otime time without time zone,
  vstatus boolean,
  remarks text,
  vowner text
);
我需要向其中添加数据。此数据应为1年,(数据从
01-01-2016
31-12-2016
)。在一个日期中可以有5个条目,
区域
列必须有3个值(
中部、西部、东部
),
City
列必须有3个值(
City1
City2
City3

v类型
列是车辆类型,例如
重型、轻型、其他

vmake
列是制造商
奥迪、日产、丰田、现代、GMC

vregno
此列用于车辆注册号,它应该是唯一的(例如注册号
CFB 4587
)。
intime
一天中任意时间(“上午10:15”)
otime
此列应为
intime
+5或10或15或20。
vstatus
列应具有
True
false

最后,我使用这个select查询生成日期行

select '2013-01-01'::date + (n || ' days')::interval days
  from generate_series(0, 365) n;

生成车辆注册号的第一部分

   SELECT substring(string_agg (substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil (random() * 62)::integer, 1), ''),1,3) t
    FROM   generate_series(0,45);
预期产出

id idate      region  city  vtype          vmake  vmodel vregno   intime              otime               vstatus remarks vowner 
-- ---------- ------- ----- -------------- ------ ------ -------- ------------------- ------------------- ------- ------- ------ 
1  2016-01-01 Central City1 Heavy Vechicle Nissan Model1 NGV 4578 12:15:00  12:30:00 1       NULL    Tom    
2  2016-01-01 Western City1 Light          Audi   S3     BFR 4587 10:20:00 10:40:00 1       NULL    Jerry  

r_日期关系只是在范围内生成日期的简单方法

其他常数和最大常数分别是总体的数组及其长度。region[(random()*region_max)::int2+1]-随机选择数组中的元素

INSERT INTO af01 (idate, region, city, vtype, vmake, vregno, intime, otime, vstatus) 
SELECT cd, r, c, v, vm, rn, intime, intime + len as otime, status
FROM (
      WITH r_dates AS (
           SELECT generate_series('2013-01-01'::date, '2013-12-31'::date, '1 day'::interval) as cd
      ), other_const AS  (
           SELECT '{Central,Western,Eastern}'::text[] AS region,
                  '{City1,City2,City3}'::text[] as cities,
                  '{Heavy,light,Other}'::text[] as vehicles,
                  '{Audi,Nissan,Toyota,Hyundai,GMC}'::text[] as vmakes,
                  '{5,10,15,20}'::int4[] AS lengths,
                  'ABCDEFGHIJKLMNOPQRSTUVWXYZ'::text AS regnosrc
      ), max_const AS  (
           SELECT array_upper(region, 1) - 1 AS region_max,
                  array_upper(cities, 1) - 1 AS cities_max,
                  array_upper(vehicles, 1) - 1 AS vehicles_max,
                  array_upper(vmakes, 1) - 1 AS vmakes_max,
                  array_upper(lengths, 1) - 1 AS lengths_max
        FROM other_const
      ) 
      SELECT cd,
             region[(random() * region_max)::int2 + 1] AS r,
             cities[(random() * cities_max)::int2 + 1] AS c,
             vehicles[(random() * vehicles_max)::int2 + 1] AS v,
             vmakes[(random() * vmakes_max)::int2 + 1] AS vm,
             (
                 SELECT string_agg(s, '')
                   FROM (
                         SELECT substr(regnosrc, (random() * (length(regnosrc) - 1))::int4 + 1, 1) AS s
                           FROM generate_series(1, 3)
                       ) AS a
               )
                  || lpad(((random() * 9999)::int8)::text, 4, '0') AS rn,
              '00:00:00'::time + (((random() * 24 * 60)::int8)::text || 'min')::interval AS intime,
              ((lengths[(random() * lengths_max)::int2 + 1])::text || 'min')::interval AS len,
              random() > 0.5 AS status
         FROM r_dates, other_const, max_const, generate_series(1, 5)
   ) AS A

是否与地区和城市有关系?例如:城市1必须在西部地区如果你有城市表,你可以:
从城市中随机选择城市名称()限制1
我没有自动取款机表,但我会创建一个,然后这样做