aggregate-functions

How to create a MySQL stored aggregate function?

对着背影说爱祢 提交于 2019-12-07 18:35:20
问题 I need to write a stored function that does an aggregate operation on a column. Let's say a median (but actually, there are many different summary functions I want to implement). Ideally, I would like to have something like medBy( col1 col2 col3 ) which then for each row returns a median of the col1 values of all the rows that have the same col2 and col3 value as this one. That way, I wouldn't have to do GROUP BY on the whole query, just have a repeating value in that one column. This

Create two aggregate columns by Group By Pandas

老子叫甜甜 提交于 2019-12-07 17:55:24
问题 I'm new to DataFrames and I want to group multiple columns and then sum and keep a count on the last column. e.g. s = pd.DataFrame(np.matrix([[1, 2,3,4], [3, 4,7,6],[3,4,5,6],[1,2,3,7]]), columns=['a', 'b', 'c', 'd']) a b c d 0 1 2 3 4 1 3 4 7 6 2 3 4 5 6 3 1 2 3 7 I want to group on a , b and c but then sum on d and count the elements within the group. I can count by s = s.groupby(by=["a", "b", "c"])["d"].count() a b c 1 2 3 2 3 4 5 1 7 1 And I can sum by s = s.groupby(by=["a", "b", "c"])["d

Aggregation by timestamp

家住魔仙堡 提交于 2019-12-07 17:04:28
问题 SEO > SEO > Paid 1 Paid > Paid > Affiliate > Paid 1 SEO > Affiliate 1I have a query that results in a data containing customer id numbers, marketing channel, timestamp, and purchase date. So, the results might look something like this. id marketingChannel TimeStamp Transaction_date 1 SEO 5/18 23:11:43 5/18 1 SEO 5/18 24:12:43 5/18 1 Paid 5/18 24:13:43 5/18 2 Paid 5/18 24:12:43 5/18 2 Paid 5/18 24:14:43 5/18 2 Affiliate 5/18 24:20:43 5/18 2 Paid 5/18 24:22:43 5/18 3 SEO 5/18 24:10:43 5/18 3

Count the number of rows that contain a letter/number

谁说我不能喝 提交于 2019-12-07 14:49:43
问题 What I am trying to achieve is straightforward, however it is a little difficult to explain and I don't know if it is actually even possible in postgres. I am at a fairly basic level. SELECT, FROM, WHERE, LEFT JOIN ON, HAVING , e.t.c the basic stuff. I am trying to count the number of rows that contain a particular letter/number and display that count against the letter/number. i.e How many rows have entries that contain an "a/A" (Case insensitive) The table I'm querying is a list of film

Daily average for the month (needs number of days in month)

孤人 提交于 2019-12-07 14:48:32
问题 I have a table as follow: CREATE TABLE counts ( T TIMESTAMP NOT NULL, C INTEGER NOT NULL ); I create the following views from it: CREATE VIEW micounts AS SELECT DATE_TRUNC('minute',t) AS t,SUM(c) AS c FROM counts GROUP BY 1; CREATE VIEW hrcounts AS SELECT DATE_TRUNC('hour',t) AS t,SUM(c) AS c,SUM(c)/60 AS a FROM micounts GROUP BY 1; CREATE VIEW dycounts AS SELECT DATE_TRUNC('day',t) AS t,SUM(c) AS c,SUM(c)/24 AS a FROM hrcounts GROUP BY 1; The problem now comes in when I want to create the

How to add TimeSpans together using LINQ when in a Dictionary collection

夙愿已清 提交于 2019-12-07 10:06:56
问题 Is it possible to group up all equal keys in a Dictionary and add their corresponding TimeSpans together using LINQ? Something like this (but this doesnt work) var p = myDictionaryStringTimeStamp.Sum(x => x.Value.Add(myTimeStamp); 回答1: You can do it with the Aggregate LINQ method like this: var totalTime = myDictionaryStringTimeStamp .Values .Aggregate(new TimeSpan(0), (p, v) => p.Add(v)); 来源: https://stackoverflow.com/questions/18455756/how-to-add-timespans-together-using-linq-when-in-a

Can not ORDER BY an AVG value with certain GROUP BY criteria in MySQL

☆樱花仙子☆ 提交于 2019-12-07 07:37:08
问题 I have a table data_summaries . It has such columns as item_id INT(11) , user_grouping TEXT and value DECIMAL(10,2) . If I try to make a query that groups the results by user_grouping and orders them by the AVG of value , that fails: SELECT user_grouping, AVG(value) AS avg_value, SUM(value) AS sum_value FROM data_summaries GROUP BY user_grouping ORDER BY avg_value +---------------+-----------+-----------+ | user_grouping | avg_value | sum_value | +---------------+-----------+-----------+ |

Terms Aggregation for nested field in Elastic Search

佐手、 提交于 2019-12-07 05:45:16
问题 I have next mapping for field in Elastic Search (definition in YML): my_analyzer: type: custom tokenizer: keyword filter: lowercase products_filter: type: "nested" properties: filter_name: {"type" : "string", analyzer: "my_analyzer"} filter_value: {"type" : "string" , analyzer: "my_analyzer"} Each document has a lot of filters and it looks like: "products_filter": [ { "filter_name": "Rahmengröße", "filter_value": "33,5 cm" } , { "filter_name": "color", "filter_value": "gelb" } , { "filter

Cumulative sum of values by month, filling in for missing months

泪湿孤枕 提交于 2019-12-07 05:34:29
问题 I have this data table and I'm wondering if is possible create a query that get a cumulative sum by month considering all months until the current month . date_added | qty ------------------------------------ 2015-08-04 22:28:24.633784-03 | 1 2015-05-20 20:22:29.458541-03 | 1 2015-04-08 14:16:09.844229-03 | 1 2015-04-07 23:10:42.325081-03 | 1 2015-07-06 18:50:30.164932-03 | 1 2015-08-22 15:01:54.03697-03 | 1 2015-08-06 18:25:07.57763-03 | 1 2015-04-07 23:12:20.850783-03 | 1 2015-07-23 17:45

Is there any MySQL Aggregate Function for “CONTAINS”?

最后都变了- 提交于 2019-12-07 03:28:53
问题 Say I have this data set user | group --------+------- a@a.com | A a@a.com | B b@b.com | A c@c.com | B d@d.com | A d@d.com | B d@d.com | C I want to convert this into a table like this: user | IN_A | IN_B | IN_C --------+-------+-------+------- a@a.com | TRUE | TRUE | FALSE b@b.com | TRUE | FALSE | FALSE c@c.com | FALSE | TRUE | FALSE d@d.com | TRUE | TRUE | TRUE I've got: SELECT user, IF(LOCATE('A', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_A, IF(LOCATE('B', GROUP_CONCAT(group)) > 0, TRUE