aggregate-functions

Left outer join acting like inner join

跟風遠走 提交于 2019-12-02 00:41:41
Summary My goal is to find every user who has ever been assigned to a task, and then generate some statistics over a particular date range, and associate the stats with the original set of users. When no statistics exist for a particular user, I want a row in the output for the user but NULL values for the stats. I have a complex SQL query that looks like this (actual query at the bottom): SELECT user_name, changeday, project_name sum(hour_delta) AS hours, FROM ( … ) tasked_users LEFT OUTER JOIN ( … ) a ON tasked_users.id=a.assignee_id WHERE (changeday IS NULL) OR (changeday >= … AND changeday

Grouping based on sequence of rows

假如想象 提交于 2019-12-02 00:29:05
I have a table of orders with a column denoting whether it's a buy or a sell, with the rows typically ordered by timestamp. What I'd like to do is operate on groups of consecutive buys, plus their sell. e.g. B B S B S B B S -> (B B S) (B S) (B B S) Example: order_action | timestamp -------------+--------------------- buy | 2013-10-03 13:03:02 buy | 2013-10-08 13:03:02 sell | 2013-10-10 15:58:02 buy | 2013-11-01 09:30:02 buy | 2013-11-01 14:03:02 sell | 2013-11-07 10:34:02 buy | 2013-12-03 15:46:02 sell | 2013-12-09 16:00:03 buy | 2013-12-11 13:02:02 sell | 2013-12-18 15:59:03 I'll be running

Multiple averages over evenly spaced intervals

天涯浪子 提交于 2019-12-02 00:27:54
问题 I'm trying to learn SQL so be patient with me. I'm using PostgreSQL 9.3 I want to average a column based on a window of dates. I've been able to write window functions that accomplish this with a set interval but I'd like to be able to be able to do this with a growing interval . By this I mean: average values from date_0 to date_1 average values from date_0 to date_2 average values from date_0 to date_3 ..... so date date_0 stays the same and date_x grows and creates a larger sample I'm

Getting daily counts for events that don't happen every day

雨燕双飞 提交于 2019-12-01 23:59:52
问题 I have a customer table in which a new row is inserted when a customer signup occurs. Problem I want to know the total number of signup per day for a given date range. For example, find the total number of signup each day from 2015-07-01 to 2015-07-10 customer table sample data [relevant columns shown] customerid username created 1 mrbean 2015-06-01 2 tom 2015-07-01 3 jerry 2015-07-01 4 bond 2015-07-02 5 superman 2015-07-10 6 tintin 2015-08-01 7 batman 2015-08-01 8 joker 2015-08-01 Required

Custom PostgreSQL aggregate for circular average

99封情书 提交于 2019-12-01 23:36:55
I'm trying to implement a custom aggregate function in Postgres which will average directions in degrees - i.e. I want to able to do: SELECT circavg(direction) FROM sometable; This can be done using the formula: xbar = atan2(sum(sin(xi), sum(cos(xi))) I think I need to define an sfunc which will take a direction, and add the sine and cosine of that into two accumulators. The final function then converts the two components back into a direction using atan2. I can't work out how to define the sfunc so that the current state consists of two components e.g. (float, float) . The documentation is a

Select a dynamic set of columns from a table and get the sum for each

旧街凉风 提交于 2019-12-01 22:34:44
Is it possible to do the following in Postgres: SELECT column_name FROM information_schema WHERE table_name = 'somereport' AND data_type = 'integer'; SELECT SUM(coulmn_name[0]),SUM(coulmn_name[1]) ,SUM(coulmn_name[3]) FROM somereport; In other words I need to select a group of columns from a table depending on certain criteria, and then sum each of those columns in the table. I know I can do this in a loop, so I can count each column independently, but obviously that requires a query for each column returned from the information schema query. Eg: FOR r IN select column_name from information

Getting daily counts for events that don't happen every day

点点圈 提交于 2019-12-01 21:27:25
I have a customer table in which a new row is inserted when a customer signup occurs. Problem I want to know the total number of signup per day for a given date range. For example, find the total number of signup each day from 2015-07-01 to 2015-07-10 customer table sample data [relevant columns shown] customerid username created 1 mrbean 2015-06-01 2 tom 2015-07-01 3 jerry 2015-07-01 4 bond 2015-07-02 5 superman 2015-07-10 6 tintin 2015-08-01 7 batman 2015-08-01 8 joker 2015-08-01 Required Output created signup 2015-07-01 2 2015-07-02 1 2015-07-03 0 2015-07-04 0 2015-07-05 0 2015-07-06 0 2015

SPARQL - How do you use count?

荒凉一梦 提交于 2019-12-01 21:21:47
问题 I have this query SELECT ?s WHERE {?a <http://xmlns.com/foaf/0.1/topic_interest> ?s} which returns aaa aaa aaa bbb bbb ccc However, I want to display it as aaa | 3 bbb | 2 ccc | 1 I am using dotnetrdf. This is what i tried SELECT (COUNT(*) AS ?s) WHERE {?a <http://xmlns.com/foaf/0.1/topic_interest> ?s} and this just gives me the number of rows there are which is 3080. Can you tell me how to make it right? Thanks 回答1: This is because COUNT(*) simply counts result rows for each group If there

Is it possible to have an SQL query that uses AGG functions in this way?

痞子三分冷 提交于 2019-12-01 21:02:54
Assuming I have the following aggregate functions: AGG1 AGG2 AGG3 AGG4 Is it possible to write valid SQL (in a db agnostic way) like this: SELECT [COL1, COL2 ....], AGG1(param1), AGG2(param2) FROM [SOME TABLES] WHERE [SOME CRITERIA] HAVING AGG3(param2) >-1 and AGG4(param4) < 123 GROUP BY COL1, COL2, ... COLN ORDER BY COL1, COLN ASC LIMIT 10 Where COL1 ... COLN are columns in the tables being queried, and param1 ... paramX are parameters passed to the AGG funcs. Note: AGG1 and AGG2 are returned in the results as columns (but do not appear in the HAVING CLAUSE, and AGG3 and AGG4 appear in the

Multiple averages over evenly spaced intervals

為{幸葍}努か 提交于 2019-12-01 20:48:54
I'm trying to learn SQL so be patient with me. I'm using PostgreSQL 9.3 I want to average a column based on a window of dates. I've been able to write window functions that accomplish this with a set interval but I'd like to be able to be able to do this with a growing interval . By this I mean: average values from date_0 to date_1 average values from date_0 to date_2 average values from date_0 to date_3 ..... so date date_0 stays the same and date_x grows and creates a larger sample I'm assuming there is a better way than running a query for each range I'd like to average. Any advice is