如何在循环中将结果导出到 CSV
how to export results to CSV in a loop
我有一个 SQL 查询
select time,t_count,sum(t_count) over (order by time) as
cumulative_t_count ,cumulative_t_count/sum(t_count) as percentage_cumulative count,state,hour from (select distinct
time,count(distinct num_id) as t_count,state,hour from (select * from
public.table1 where day=1 and time>0 order by time)as A group by
time,hour,state order by hour,state )B where state=1 and hour=1;
这给了我以下格式的结果:
time t_count cumulative_t_count state hour
_____ _____ _________________ ____ ____
1 10 10 1 1
2 20 30 1 1
3 30 60 1 1
4 60 120 1 1
同样,我有 80 个州和 0-23.I 小时,想通过将状态从 1-80 更改为 1、7、14、[=22 小时,将所有结果导出到 CSV 文件=] 现在我在上面的代码中手动更改状态和小时并将结果导出为 CSV.But 看来我必须将状态 1 更改为 80 以及每个状态的小时数 1、7、14、19。
BEGIN
FOR i IN 1..80 LOOP
FOR i IN 1,7,14,19 LOOP
/copy ( select time,t_count,sum(t_count) over (order by time) as
cumulative_t_count ,cumulative_t_count/sum(t_count) as percentage_cumulative count,state,hour from (select distinct
time,count(distinct num_id) as t_count,state,hour from (select * from
public.table1 where day=1 and time>0 order by time)as A group by
time,hour,state order by hour,state )B where state=i and hour=j)
To '/tmp/state_i_hour_j.csv' With CSV
END LOOP
END LOOP
但这并没有work.I还想将每个结果集导出为 CSV format.Any帮助表示感谢。
如果您只需要执行一次,您可以使用以下内容创建您需要的 sql 语句。然后您可以复制输出记录并执行它们。
WITH states AS
(SELECT generate_series(1,80) as i),
hours AS
(SELECT j FROM (values (1),(14),(17),(19) ) s(j))
SELECT 'copy ( select time,t_count,sum(t_count) over (order by time) as
cumulative_t_count ,cumulative_t_count/sum(t_count) as percentage_cumulative count,state,hour from (select distinct
time,count(distinct num_id) as t_count,state,hour from (select * from
public.table1 where day=1 and time>0 order by time)as A group by
time,hour,state order by hour,state )B where state=' ||i || ' and hour=' ||j || ')
To ''/tmp/state_' ||i || '_hour_' ||j || '.csv'' With CSV;' from states,hours
我有一个 SQL 查询
select time,t_count,sum(t_count) over (order by time) as
cumulative_t_count ,cumulative_t_count/sum(t_count) as percentage_cumulative count,state,hour from (select distinct
time,count(distinct num_id) as t_count,state,hour from (select * from
public.table1 where day=1 and time>0 order by time)as A group by
time,hour,state order by hour,state )B where state=1 and hour=1;
这给了我以下格式的结果:
time t_count cumulative_t_count state hour
_____ _____ _________________ ____ ____
1 10 10 1 1
2 20 30 1 1
3 30 60 1 1
4 60 120 1 1
同样,我有 80 个州和 0-23.I 小时,想通过将状态从 1-80 更改为 1、7、14、[=22 小时,将所有结果导出到 CSV 文件=] 现在我在上面的代码中手动更改状态和小时并将结果导出为 CSV.But 看来我必须将状态 1 更改为 80 以及每个状态的小时数 1、7、14、19。
BEGIN
FOR i IN 1..80 LOOP
FOR i IN 1,7,14,19 LOOP
/copy ( select time,t_count,sum(t_count) over (order by time) as
cumulative_t_count ,cumulative_t_count/sum(t_count) as percentage_cumulative count,state,hour from (select distinct
time,count(distinct num_id) as t_count,state,hour from (select * from
public.table1 where day=1 and time>0 order by time)as A group by
time,hour,state order by hour,state )B where state=i and hour=j)
To '/tmp/state_i_hour_j.csv' With CSV
END LOOP
END LOOP
但这并没有work.I还想将每个结果集导出为 CSV format.Any帮助表示感谢。
如果您只需要执行一次,您可以使用以下内容创建您需要的 sql 语句。然后您可以复制输出记录并执行它们。
WITH states AS
(SELECT generate_series(1,80) as i),
hours AS
(SELECT j FROM (values (1),(14),(17),(19) ) s(j))
SELECT 'copy ( select time,t_count,sum(t_count) over (order by time) as
cumulative_t_count ,cumulative_t_count/sum(t_count) as percentage_cumulative count,state,hour from (select distinct
time,count(distinct num_id) as t_count,state,hour from (select * from
public.table1 where day=1 and time>0 order by time)as A group by
time,hour,state order by hour,state )B where state=' ||i || ' and hour=' ||j || ')
To ''/tmp/state_' ||i || '_hour_' ||j || '.csv'' With CSV;' from states,hours