sql - Counting different ranges over the same dataset efficiently/simply -


I am writing a function in Postgresql which will return some metric, will be calculated for a specific time zone (input).

Sample results:

Enter image details here

The main issue is that it is only one metric I need to get another 9 metrics from other tables.

Any suggestions for a less verbose method of achieving this? Return the function dashboard _ metrics (destination_tyimon text) (metric text, count count) today $ func $ DECLARE today TIMESTAMP; Tomorrow TIMESTAMP; Tomorrow TIMESTAMP; The Israeli Timestamp; Last 7 days TIMESTAMP; Last 30days TIMESTAMP; Last 60days TIMESTAMP; Select today's 'Today' life nowadays in destination_timezone; Select (destination_timezone in 'tomorrow' time zone) tomorrow; Select (destination_timezone in 'tomorrow' time zone) tomorrow; SELECT (destination_time in the 'time' time zone) - Interval '1 day' in EARAL; SELECT (destination_tyimension in 'Today' time zone) - '7 days' duration in the last 7 days; Choose (destination_timezone on 'Today' time zone) - Last 30days in '30 days' interval; Select (destination_tyimension in 'Today' time zone) - '60 days interval in the last 60days; Return Query - Todd (SELECT 'ideastoday' :: TEXT AS Metric, COUNT (1) created from WHERE from ANNTX_Idess_arram-> today and created_on & lt; tomorrow and analytics_ideas.space_id = 1) UNION ALL (SELECT 'ideasy yesterday ':: Metric in the form of text, counted from antithesis_ides as COUNT (1) WHERE created on WHERE_On> = tomorrow and created_on & lt; today and analytics_deses.space_id = 1) UNION all (SELECT' ideasereyesterday ':: Texet Metric, COUNT (1) created from WHERE from AS Calculation Analytics_Edes on_on & gt; = Era and Created_O & lt; Tomorrow and Analytics_deses.Space_id = 1) Metrics in the form of UNIX All (SELECT 'ideaslast7days' :: Text) composed of COUNT (1) ANNTX_IDES, where it is built> Last 7 days and created_on & Lieutenant; Today and analytics_ideas.space_id = 1) Metrics in the form of SELECT 'ideaslastlast30days' :: Text, COUNT (1) ANNTX_Edes from WHERE built_on> = last 30days and made_on

At the higher level, this is what usually comes down: If you want to share your metric with logic , then you need to differentiate them Data with . You need a general data structure that separates one metric from the other. With it, you can write a generalized algorithm to process these structures. Dashboard _Matrix (destination_timogen text) Return Tables (metric text, counting count) AS $ func $ with metricidiff (metric, 'initial', -1, 0), ('Ideasieri ',', -2, -1), ('Ideology 7 days', -7, 0)), ('Ideaslast30days', -30, 0), ('ideaslast60days', -60, 0)) Select metricidief Metric, COUNT (*) AM Metricidief, created from Analytics_ides to WHERE_arram = ('Today's Destination_timezone') + Metric Diff.Stattaye * Interval '1 day' and made_on & lt ; (Destination_timogen in the 'time' time zone) + metricidief.exe * interval '1 day' and analytics_idas.space_id = 1 by group metricidief. Metric $ throw $ language static static;

Note that this will not place any order on the output. If this is important, then you should include the DisplayOrder field in MetricDef , and add a ORDER BY anywhere in the main query.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -