我正在Postgresql中编写一个函数,它将返回一些针对特定时区(输入)计算的指标。
示例结果:
主要问题是这只是一个指标。我需要从其他表中获取其他9个指标。
是否有任何建议可以采用不那么冗长的方式来实现这一目标?
CREATE OR REPLACE FUNCTION dashboard_metrics(destination_timezone TEXT)
RETURNS TABLE(
metric TEXT,
count BIGINT
) AS
$func$
DECLARE
today TIMESTAMP;
tomorrow TIMESTAMP;
yesterday TIMESTAMP;
ereyesterday TIMESTAMP;
last7days TIMESTAMP;
last30days TIMESTAMP;
last60days TIMESTAMP;
BEGIN
SELECT 'today' AT TIME ZONE destination_timezone
INTO today;
SELECT ('tomorrow' AT TIME ZONE destination_timezone)
INTO tomorrow;
SELECT ('yesterday' AT TIME ZONE destination_timezone)
INTO yesterday;
SELECT ('yesterday' AT TIME ZONE destination_timezone) - INTERVAL '1 day'
INTO ereyesterday;
SELECT ('today' AT TIME ZONE destination_timezone) - INTERVAL '7 days'
INTO last7days;
SELECT ('today' AT TIME ZONE destination_timezone) - INTERVAL '30 days'
INTO last30days;
SELECT ('today' AT TIME ZONE destination_timezone) - INTERVAL '60 days'
INTO last60days;
RETURN QUERY
--TODAY
(SELECT
'ideastoday' :: TEXT AS metric,
COUNT(1) AS count
FROM analytics_ideas
WHERE created_on >= today AND created_on < tomorrow AND analytics_ideas.space_id = 1)
UNION ALL
(SELECT
'ideasyesterday' :: TEXT AS metric,
COUNT(1) AS count
FROM analytics_ideas
WHERE created_on >= yesterday AND created_on < today AND analytics_ideas.space_id = 1)
UNION ALL
(SELECT
'ideasereyesterday' :: TEXT AS metric,
COUNT(1) AS count
FROM analytics_ideas
WHERE created_on >= ereyesterday AND created_on < yesterday AND analytics_ideas.space_id = 1)
UNION ALL
(SELECT
'ideaslast7days' :: TEXT AS metric,
COUNT(1) AS count
FROM analytics_ideas
WHERE created_on >= last7days AND created_on < today AND analytics_ideas.space_id = 1)
UNION ALL
(SELECT
'ideaslast30days' :: TEXT AS metric,
COUNT(1) AS count
FROM analytics_ideas
WHERE created_on >= last30days AND created_on < today AND analytics_ideas.space_id = 1)
UNION ALL
(SELECT
'ideaslast60days' :: TEXT AS metric,
COUNT(1) AS count
FROM analytics_ideas
WHERE created_on >= last60days AND created_on < today AND analytics_ideas.space_id = 1);
END
$func$ LANGUAGE plpgsql;
答案 0 :(得分:1)
您提到的其他指标的细节可能会使事情变得复杂,但您提供的功能可以简化很多。
从较高层面来看,这通常归结为:如果您希望指标分享逻辑,则需要将其与数据区分开来。您需要一个通用的数据结构来封装将一个指标与另一个指标区分开来的所有内容。有了它,您可以编写一个通用算法来处理这些结构。
CREATE OR REPLACE FUNCTION dashboard_metrics(destination_timezone TEXT)
RETURNS TABLE(
Metric TEXT,
Count BIGINT
) AS
$func$
WITH MetricDef (Metric, StartDay, EndDay) AS (
VALUES
('ideastoday', 0, 1),
('ideasyesterday', -1, 0),
('ideasereyesterday', -2, -1),
('ideaslast7days', -7, 0),
('ideaslast30days', -30, 0),
('ideaslast60days', -60, 0)
)
SELECT
MetricDef.Metric,
COUNT(*) AS Count
FROM
MetricDef,
analytics_ideas
WHERE
created_on >= ('today' AT TIME ZONE destination_timezone) + MetricDef.StartDay * INTERVAL '1 day' AND
created_on < ('today' AT TIME ZONE destination_timezone) + MetricDef.EndDay * INTERVAL '1 day' AND
analytics_ideas.space_id = 1
GROUP BY
MetricDef.Metric
$func$
LANGUAGE SQL STABLE;
请注意,这不会对输出施加任何排序。如果这很重要,则应在DisplayOrder
中添加MetricDef
字段,并在主查询中的某处添加ORDER BY
。