aggregate-functions

Compute percents from SUM() in the same SELECT sql query

痴心易碎 提交于 2019-11-26 06:08:17
问题 In the table my_obj there are two integer fields: (value_a integer, value_b integer); I try to compute how many time value_a = value_b , and I want to express this ratio in percents. This is the code I have tried: select sum(case when o.value_a = o.value_b then 1 else 0 end) as nb_ok, sum(case when o.value_a != o.value_b then 1 else 0 end) as nb_not_ok, compute_percent(nb_ok,nb_not_ok) from my_obj as o group by o.property_name; compute_percent is a stored_procedure that simply does (a * 100)

GROUP BY + CASE statement

拟墨画扇 提交于 2019-11-26 06:01:54
问题 I have a working query that is grouping data by hardware model and a result, but the problem is there are many \"results\" . I have tried to reduce that down to \"if result = 0 then keep as 0, else set it to 1\" . This generally works, but I end up having: day | name | type | case | count ------------+----------------+------+------+------- 2013-11-06 | modelA | 1 | 0 | 972 2013-11-06 | modelA | 1 | 1 | 42 2013-11-06 | modelA | 1 | 1 | 2 2013-11-06 | modelA | 1 | 1 | 11 2013-11-06 | modelB | 1

Postgres window function and group by exception

梦想与她 提交于 2019-11-26 05:37:01
问题 I\'m trying to put together a query that will retrieve the statistics of a user (profit/loss) as a cumulative result, over a period of time. Here\'s the query I have so far: SELECT p.name, e.date, sum(sp.payout) OVER (ORDER BY e.date) - sum(s.buyin) OVER (ORDER BY e.date) AS \"Profit/Loss\" FROM result r JOIN game g ON r.game_id = g.game_id JOIN event e ON g.event_id = e.event_id JOIN structure s ON g.structure_id = s.structure_id JOIN structure_payout sp ON g.structure_id = sp.structure_id

How to define a custom aggregation function to sum a column of Vectors?

[亡魂溺海] 提交于 2019-11-26 05:29:31
问题 I have a DataFrame of two columns, ID of type Int and Vec of type Vector ( org.apache.spark.mllib.linalg.Vector ). The DataFrame looks like follow: ID,Vec 1,[0,0,5] 1,[4,0,1] 1,[1,2,1] 2,[7,5,0] 2,[3,3,4] 3,[0,8,1] 3,[0,0,1] 3,[7,7,7] .... I would like to do a groupBy($\"ID\") then apply an aggregation on the rows inside each group by summing the vectors. The desired output of the above example would be: ID,SumOfVectors 1,[5,2,7] 2,[10,8,4] 3,[7,15,9] ... The available aggregation functions

Naming returned columns in Pandas aggregate function? [duplicate]

送分小仙女□ 提交于 2019-11-26 05:18:48
问题 This question already has answers here : Multiple aggregations of the same column using pandas GroupBy.agg() (3 answers) Closed 5 months ago . I\'m having trouble with Pandas\' groupby functionality. I\'ve read the documentation, but I can\'t see to figure out how to apply aggregate functions to multiple columns and have custom names for those columns. This comes very close, but the data structure returned has nested column headings: data.groupby(\"Country\").agg( {\"column1\": {\"foo\": sum(

Create a pivot table with PostgreSQL

佐手、 提交于 2019-11-26 04:56:19
问题 Suppose I have a table in Postgres called listings that looks like this: id neighborhood bedrooms price 1 downtown 0 256888 2 downtown 1 334000 3 riverview 1 505000 etc. How do I write a crosstab query that shows the average price per bedrooms as the columns and neighborhoods as the rows? The output of the query should look something like this (numbers are made up, columns are the bedrooms): 0 1 2 3 riverton 250000 300000 350000 - downtown 189000 325000 - 450000 回答1: First compute the average

Return as array of JSON objects in SQL (Postgres)

隐身守侯 提交于 2019-11-26 04:45:57
问题 I have the following table MyTable : id │ value_two │ value_three │ value_four ────┼───────────┼─────────────┼──────────── 1 │ a │ A │ AA 2 │ a │ A2 │ AA2 3 │ b │ A3 │ AA3 4 │ a │ A4 │ AA4 5 │ b │ A5 │ AA5 I want to query an array of objects { value_three, value_four } grouped by value_two . value_two should be present on its own in the result. The result should look like this: value_two │ value_four ───────────┼─────────────────────────────────────────────────────────────────────────────────

Is there ANY_VALUE capability for mysql 5.6?

こ雲淡風輕ζ 提交于 2019-11-26 04:28:12
问题 currently im working with mysql 5.7 in development, and 5.6 in production. Each time i run a query with a group by in development i get some error like \"Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY \" Here is the query. SELECT c.id, c.name, i.* FROM countries c, images i WHERE i.country_id = c.id GROUP BY c.id; Fixed for 5.7; SELECT c.id, c.name, ANY_VALUE(i.url) url, ANY_VALUE(i.lat) lat, ANY_VALUE(i.lng) lng FROM countries c, images i WHERE i.country_id = c.id GROUP BY

Concatenate multiple result rows of one column into one, group by another column [duplicate]

∥☆過路亽.° 提交于 2019-11-26 04:07:28
问题 This question already has an answer here: How to concatenate strings of a string field in a PostgreSQL 'group by' query? 14 answers I\'m having a table like this Movie Actor A 1 A 2 A 3 B 4 I want to get the name of a movie and all actors in that movie, and I want the result to be in a format like this: Movie ActorList A 1, 2, 3 How can I do it? 回答1: Simpler with the aggregate function string_agg() (Postgres 9.0 or later): SELECT movie, string_agg(actor, ', ') AS actor_list FROM tbl GROUP BY

PostgreSQL: running count of rows for a query 'by minute'

人走茶凉 提交于 2019-11-26 03:16:40
问题 I need to query for each minute the total count of rows up to that minute. The best I could achieve so far doesn\'t do the trick. It returns count per minute, not the total count up to each minute: SELECT COUNT(id) AS count , EXTRACT(hour from \"when\") AS hour , EXTRACT(minute from \"when\") AS minute FROM mytable GROUP BY hour, minute 回答1: Only return minutes with activity Shortest SELECT DISTINCT date_trunc('minute', "when") AS minute , count(*) OVER (ORDER BY date_trunc('minute', "when"))