Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
Postgresql Postgres根据年数返回多个相同的结果_Postgresql_Date_Data Generation - Fatal编程技术网

Postgresql Postgres根据年数返回多个相同的结果

Postgresql Postgres根据年数返回多个相同的结果,postgresql,date,data-generation,Postgresql,Date,Data Generation,我有一个Postgres查询,其中包含子查询,只输出12行数据,这正是我希望它的外观。这些行中的每一行表示数据库中所有年份数据的月平均值,每个月一行。 查询: SELECT to_char(to_timestamp(to_char(subquery2.month, '999'), 'MM'), 'Mon') AS Month, subquery2.month AS Month_num, round(AVG(subquery2.all_use_tot), 2) AS Average_W

我有一个Postgres查询,其中包含子查询,只输出12行数据,这正是我希望它的外观。这些行中的每一行表示数据库中所有年份数据的月平均值,每个月一行。 查询:

SELECT
  to_char(to_timestamp(to_char(subquery2.month, '999'), 'MM'), 'Mon') AS Month,
  subquery2.month AS Month_num,
  round(AVG(subquery2.all_use_tot), 2) AS Average_Withdrawals,
  round(AVG(subquery2.all_cu_tot), 2) AS Average_Consumptive_Use
FROM
(SELECT
  subquery1.month,
  subquery1.year,
  sum(subquery1.liv_tot) AS all_use_tot,
  sum(subquery1.CU_total) AS all_cu_tot
FROM
    (SELECT 
        EXTRACT('Month' FROM withdrawals.begintime) AS month,
        EXTRACT(YEAR FROM withdrawals.begintime)::int AS year,
        tdx_ic_use_type.use_type_code AS Use_type,
        sum(withdrawals.withdrawalvalue_mgd) AS liv_tot,
        (tdx_ic_use_type.cufactor)*(sum(withdrawals.withdrawalvalue_mgd)) AS CU_total
    FROM 
        medford.tdx_ic_use_type,
        medford.withdrawalsites
        JOIN medford.subshed2 ON ST_Contains(medford.subshed2.the_geom, medford.withdrawalsites.the_geom)
        JOIN medford.withdrawals ON medford.withdrawals.ic_site_id = medford.withdrawalsites.ic_site_id
    WHERE 
        subshed2.id = '${param.shed_id}' AND
        withdrawalsites.ic_pr_use_type_id = tdx_ic_use_type.use_type_code AND
        withdrawals.intervalcodes_id = 2

    GROUP BY
        year, 
        extract('Month' from withdrawals.begintime),
        tdx_ic_use_type.cufactor,
        Use_type
    ORDER BY
        year, extract('Month' from withdrawals.begintime) ASC
        ) AS subquery1
GROUP BY
  subquery1.year,
  subquery1.month
  ) AS subquery2
GROUP BY
  subquery2.month
ORDER BY
  subquery2.month
输出是什么样子的:

"Jan"  1  1426.50  472.65
"Feb"  2  1449.00  482.10
"Mar"  3  1459.50  485.55
"Apr"  4  1470.00  489.00
"May"  5  1480.50  492.45
"Jun"  6  1491.00  495.90
"Jul"  7  1489.50  493.35
"Aug"  8  1512.00  502.80
"Sep"  9  1510.50  500.25
"Oct" 10  1533.00  509.70
"Nov" 11  1543.50  513.15
"Dec" 12  1542.00  510.60
我从数据库中的datetime列中提取的月份列。我正在使用这个查询的结果并将其存储在一个数组中,但我想每年对表中存在的这些精确结果重复一次。因此,如果数据库中有2年的数据,那么应该有24个输出行,或者,12个相同的行重复两次。如果数据库中有3年的数据,那么应该有36个输出行,或者12个相同的行重复3次。
我如何在查询中实现这一点?是否有方法根据列中的值(即datetime字段中存在的年数)循环查询

您应该在查询中将
按月分组
更改为
按月分组
。 (但这将只提供有给定月份数据的结果。)

对于几个月的迭代:

select a::date
from generate_series(
    '2011-06-01'::date,
    '2012-06-01',
    '1 month'
) s(a)

(基于此:)

谢谢您的想法。我已经用实际的查询更新了我的问题。我用几种不同的方法尝试了你的建议,但这些值在不同的年份会发生变化,因此相同的12个月数据集不会重复,而是在变化。有什么方法可以基于列中的值循环查询吗?我添加了一个示例来生成月份。你应该加入这个结果集。