我有这样的查询,很好地生成两个给定日期之间的一系列日期:
select date '2004-03-07' + j - i as AllDate
from generate_series(0, extract(doy from date '2004-03-07')::int - 1) as i,
generate_series(0, extract(doy from date '2004-08-16')::int - 1) as j
Run Code Online (Sandbox Code Playgroud)
它在2004-03-07和之间生成162个日期2004-08-16,这就是我想要的.这段代码的问题是,它不会得到正确的答案时,这两个日期都不同年份,例如,当我尝试2007-02-01和2008-04-01.
有更好的解决方案吗?
我的应用程序有一个Events带有时间戳事件的表.
我需要在每个最近的N时间间隔内报告事件的数量.对于不同的报告,间隔可以是"每周"或"每天"或"每小时"或"每15分钟间隔".
例如,用户可以显示他们每周,每天,每小时或每季度收到的订单数量.
1)我的偏好是动态地执行单个SQL查询(我正在使用Postgres)按任意时间间隔进行分组.有没有办法做到这一点?
2)一种简单但丑陋的暴力方法是对按时间戳排序的开始/结束时间帧内的所有记录执行单个查询,然后使用方法按任意间隔手动构建计数.
3)另一种方法是在事件表中为每个区间添加单独的字段并静态存储一个the_week the_day,the_hour和the_quarter_hour字段,这样我就可以在创建记录时(一次)进行"点击",而不是每次报告该字段时.
这里有什么最好的做法,因为我可以根据需要修改模型和预先存储间隔数据(尽管只需要将表格宽度加倍);
sql postgresql ruby-on-rails aggregate-functions generate-series
在PostgreSQL中,是否可以生成一系列重复数字?例如,我想生成数字1到10,每个数字重复3次:
1
1
1
2
2
2
3
3
3
.. and so on.
Run Code Online (Sandbox Code Playgroud) 我需要进行查询并加入一年中的所有日子,但在我的数据库中没有日历表.
google-ing后我generate_series()在PostgreSQL中找到了.MySQL有类似的东西吗?
我的实际表格如下:
date qty
1-1-11 3
1-1-11 4
4-1-11 2
6-1-11 5
Run Code Online (Sandbox Code Playgroud)
但我的查询必须返回:
1-1-11 7
2-1-11 0
3-1-11 0
4-1-11 2
and so on ..
Run Code Online (Sandbox Code Playgroud) 我想在redshift中使用generate系列函数,但还没有成功.
redshift文档说它不受支持.以下代码确实有效:
select *
from generate_series(1,10,1)
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
...
10
Run Code Online (Sandbox Code Playgroud)
我想对约会做同样的事情.我尝试了很多变种,包括:
select *
from generate_series(date('2008-10-01'),date('2008-10-10 00:00:00'),1)
Run Code Online (Sandbox Code Playgroud)
踢出去:
ERROR: function generate_series(date, date, integer) does not exist
Hint: No function matches the given name and argument types.
You may need to add explicit type casts. [SQL State=42883]
Run Code Online (Sandbox Code Playgroud)
还尝试过:
select *
from generate_series('2008-10-01 00:00:00'::timestamp,
'2008-10-10 00:00:00'::timestamp,'1 day')
Run Code Online (Sandbox Code Playgroud)
并试过:
select *
from generate_series(cast('2008-10-01 00:00:00' as datetime),
cast('2008-10-10 00:00:00' as datetime),'1 day')
Run Code Online (Sandbox Code Playgroud)
两个都踢了出来:
ERROR: function generate_series(timestamp without time zone, timestamp without time zone, …Run Code Online (Sandbox Code Playgroud) 我正在使用Postgres(PL/pgSQL)开发一个算法,我需要计算两个时间戳之间的工作小时数,考虑到周末不工作,剩下的时间只计算在上午8点到下午15点.
例子:
从12月3日下午14点到12月4日上午9点应该计算2个小时:
3rd = 1, 4th = 1
Run Code Online (Sandbox Code Playgroud)从12月3日下午15点到12月7日上午8点,应该计算8小时:
3rd = 0, 4th = 8, 5th = 0, 6th = 0, 7th = 0
Run Code Online (Sandbox Code Playgroud)考虑小时分数也会很棒.
我想将以下SQL代码从MS SQL-Server移植到PostgreSQL.
DECLARE @iStartYear integer
DECLARE @iStartMonth integer
DECLARE @iEndYear integer
DECLARE @iEndMonth integer
SET @iStartYear = 2012
SET @iStartMonth = 4
SET @iEndYear = 2016
SET @iEndMonth = 1
;WITH CTE
AS
(
SELECT
--@iStartYear AS TheStartYear
@iStartMonth AS TheRunningMonth
,@iStartYear AS TheYear
,@iStartMonth AS TheMonth
UNION ALL
SELECT
--CTE.TheStartYear AS TheStartYear
--@iStartYear AS TheStartYear
CTE.TheRunningMonth + 1 AS TheRunningMonth
--,CTE.TheStartYear + (CTE.TheRunningMonth / 12) AS TheYear
,@iStartYear + (CTE.TheRunningMonth / 12) AS TheYear
,(CTE.TheMonth + 1 -1) % …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用generate_series函数在PostgreSQL中生成一个系列.从2008年1月到current month + 12(一年外),我需要一系列的月份.我正在使用并限制为PostgreSQL 8.3.14(所以我没有8.4中的时间戳系列选项).
我知道如何获得一系列日子:
select generate_series(0,365) + date '2008-01-01'
Run Code Online (Sandbox Code Playgroud)
但我不知道怎么做几个月.
sql postgresql time-series generate-series set-returning-functions
我有不均匀分布的数据(wrt日期)几年(2003-2008).我想查询一组给定的开始和结束日期的数据,按PostgreSQL 8.3中任何支持的时间间隔(日,周,月,季,年)对数据进行分组(http://www.postgresql.org/docs /8.3/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC).
问题是某些查询会在所需的时间段内提供连续的结果,如下所示:
select to_char(date_trunc('month',date), 'YYYY-MM-DD'),count(distinct post_id)
from some_table where category_id=1 and entity_id = 77 and entity2_id = 115
and date <= '2008-12-06' and date >= '2007-12-01' group by
date_trunc('month',date) order by date_trunc('month',date);
to_char | count
------------+-------
2007-12-01 | 64
2008-01-01 | 31
2008-02-01 | 14
2008-03-01 | 21
2008-04-01 | 28
2008-05-01 | 44
2008-06-01 | 100
2008-07-01 | 72
2008-08-01 | 91
2008-09-01 | 92
2008-10-01 | 79
2008-11-01 | 65
(12 rows)
Run Code Online (Sandbox Code Playgroud)
但有些人因为没有数据而错过了一些间隔,因为这个:
select …Run Code Online (Sandbox Code Playgroud) 我想是从generate_series,每个月的统计和计数的ID在每月的总和.这个SQL适用于PostgreSQL 9.1:
SELECT (to_char(serie,'yyyy-mm')) AS year, sum(amount)::int AS eintraege FROM (
SELECT
COUNT(mytable.id) as amount,
generate_series::date as serie
FROM mytable
RIGHT JOIN generate_series(
(SELECT min(date_from) FROM mytable)::date,
(SELECT max(date_from) FROM mytable)::date,
interval '1 day') ON generate_series = date(date_from)
WHERE version = 1
GROUP BY generate_series
) AS foo
GROUP BY Year
ORDER BY Year ASC;
Run Code Online (Sandbox Code Playgroud)
这是我的输出
"2006-12" | 4
"2007-02" | 1
"2007-03" | 1
Run Code Online (Sandbox Code Playgroud)
但我想得到的是这个输出(1月份的"0"值):
"2006-12" | 4
"2007-01" | 0
"2007-02" | 1
"2007-03" | 1
Run Code Online (Sandbox Code Playgroud)
因此,如果有一个月没有id,那么它应该被列出.任何想法如何解决这个问题?
以下是一些示例数据: …
generate-series ×10
postgresql ×9
sql ×8
time-series ×2
database ×1
date ×1
join ×1
left-join ×1
mysql ×1
plpgsql ×1
python ×1
range-types ×1