aggregate-functions

DISTINCT with two array_agg (or one array_agg with tuple inside)?

巧了我就是萌 提交于 2019-12-10 14:59:11
问题 I've got the following query: SELECT DISTINCT ON (ps.p) m.groundtruth, ps.p, ARRAY_AGG(m.anchor_id), ARRAY_AGG(m.id) FROM measurement m JOIN (SELECT unnest(point_array) AS p) AS ps ON ST_DWithin(ps.p, m.groundtruth, distance) GROUP BY ps.p, m.groundtruth ORDER BY ps.p, RANDOM() The output looks like that: groundtruth | p | anchor_array | id_array ------------------------------------------------------ G1 | P1 | {1,3,3,3,4} | {1,2,3,4,5} G2 | P1 | {1,5,7} | {6,7,8} G1 | P2 | {1,3,3,3,4} | {1,2

Need more details on Enumerable.Aggregate function

南笙酒味 提交于 2019-12-10 14:20:49
问题 Can you help me to understand, words.Aggregate((workingSentence, next) => + next + " " + workingSentence); from below code snippet? and it would be great if someone explain me to achive this in C# 1.1. (Snippet From MS)- string sentence = "the quick brown fox jumps over the lazy dog"; // Split the string into individual words. string[] words = sentence.Split(' '); // Prepend each word to the beginning of the // new sentence to reverse the word order. string reversed = words.Aggregate(

How can I apply aggregate functions to data extracted from JSON in Google BigQuery?

梦想的初衷 提交于 2019-12-10 13:47:25
问题 I am extracting JSON data out of a BigQuery column using JSON_EXTRACT . Now I want to extract lists of values and run aggregate functions (like AVG ) against them. Testing the JsonPath expression .objects[*].v succeeds on http://jsonpath.curiousconcept.com/. But the query: SELECT JSON_EXTRACT(json_column, "$.id") as id, AVG(JSON_EXTRACT(json_column, "$.objects[*].v")) as average_value FROM [tablename] throws a JsonPath parse error on BigQuery. Is this possible on BigQuery? Or do I need to

SELECT COUNT(*) - return 0 along with grouped fields if there are no matching rows

白昼怎懂夜的黑 提交于 2019-12-10 13:24:56
问题 I have the following query: SELECT employee,department,count(*) AS sum FROM items WHERE ((employee = 1 AND department = 2) OR (employee = 3 AND department = 4) OR (employee = 5 AND department = 6) OR ([more conditions with the same structure])) AND available = true GROUP BY employee, department; If there are no items for a pair "employee-department", then the query returns nothing. I'd like it to return zero instead: employee | department | sum ---------+------------+-------- 1 | 2 | 0 3 | 4

Calculate average from JSON column

血红的双手。 提交于 2019-12-10 12:23:41
问题 I have a table with a column of JSON data that I want to extract information from. Specifically I just want to get the average value. Example of what I have: id speed_data 391982 [{"speed":1.3,"speed":1.3,"speed":1.4,"speed":1.5... 391983 [{"speed":0.9,"speed":0.8,"speed":0.8,"speed":1.0... Example of what I want: id speed_data 391982 1.375 391982 0.875 Any suggestions on how to get this query to work? select t.*, avg(x.speed) from tbl t, json_array_elements(a->'speed') x order by random()

Why aggregate functions can not be used with DISTINCT ON(…)?

我与影子孤独终老i 提交于 2019-12-10 11:55:31
问题 The question was: How to get row which was selected by aggregate function? The question was answered and partially resolve my problem. But I still can not replace GROUP BY with DISTINCT ON because of next reason: I need both: Select id of aggregated row (may be resolved with DISTINCT ON ) Sum the ratio column (may be resolved with GROUP BY ) Some amount of resource is consumed by user. One part of day 10h user consumed 8 another part of day 10h user consumed 3 and 4h he do not consume

Sustract two queries from same table

时间秒杀一切 提交于 2019-12-10 11:35:36
问题 Let's say I have 2 tables: table device is a set of measuring devices, table data is a set of values of different types measured by the devices. Table data = (no, id,value_type,value, dayhour) no is PK, id refer to table device Table device = (id, name) id is PK I currently have a query that will obtain the sum of all values of a specific value_type generated by an id on a specific date, something like: SELECT SUM(cast(a.value as int)),b.name FROM data a INNER JOIN device b ON a.id=b.id AND a

SQL-style GROUP BY aggregate functions in jq (COUNT, SUM and etc)

这一生的挚爱 提交于 2019-12-10 11:16:41
问题 Similar questions asked here before: Count items for a single key: jq count the number of items in json by a specific key Calculate the sum of object values: How do I sum the values in an array of maps in jq? Question How to emulate the COUNT aggregate function which should behave similarly to its SQL original? Let's extend this question even more to include other regular SQL functions: COUNT SUM / MAX/ MIN / AVG ARRAY_AGG The last one is not a standard SQL function - it's from PostgreSQL but

multiply(num) aggregate function in postgresql

≯℡__Kan透↙ 提交于 2019-12-10 09:54:02
问题 This could be incredibly simple by the documentation is quite on it. Is there a way to aggregate columns via multiplication operator in postgresql. I know i can do count(column) or sum(column), but is there a multiply(column) or product(column) function that i can use. If not, any ideas how to achieve it. I'm using postgres 9.1 regards, Hassan 回答1: Sure, just define an aggregate over the base multiplication function. E.g. for bigint: CREATE AGGREGATE mul(bigint) ( SFUNC = int8mul, STYPE

Create array for values from list of columns extracted in Postgres

纵然是瞬间 提交于 2019-12-10 09:28:11
问题 I have following set of table: id column_a column_b column_c 1 t f t 2 t f f 3 f t f Which when queried by: SELECT bool_or(column_a) AS column_a , bool_or(column_b) AS column_b , bool_or(column_c) AS column_c FROM tbl WHERE id IN (1,2); gives result as: column_a column_b column_c t f t I wanted to get the array from the result as: [t,f,t] in Postgres. Please have reference to the earlier stack question over here. 回答1: Use an ARRAY constructor: SELECT ARRAY [bool_or(column_a) , bool_or(column