aggregate-functions

Is it possible to perform a bitwise group function?

馋奶兔 提交于 2019-11-30 06:39:45
I have a field in a table which contains bitwise flags. Let's say for the sake of example there are three flags: 4 => read, 2 => write, 1 => execute and the table looks like this * : user_id | file | permissions -----------+--------+--------------- 1 | a.txt | 6 ( <-- 6 = 4 + 2 = read + write) 1 | b.txt | 4 ( <-- 4 = 4 = read) 2 | a.txt | 4 2 | c.exe | 1 ( <-- 1 = execute) I'm interested to find all users who have a particular flag set (eg: write) on ANY record. To do this in one query, I figured that if you OR'd all the user's permissions together you'd get a single value which is the "sum

Spark Scala - How to group dataframe rows and apply complex function to the groups?

女生的网名这么多〃 提交于 2019-11-30 05:55:07
i am trying to solve this super simple problem and i am already sick of it, I hope somebody can help my out with this. I have a dataframe of shape like this: --------------------------- | Category | Product_ID | |------------+------------+ | a | product 1 | | a | product 2 | | a | product 3 | | a | product 1 | | a | product 4 | | b | product 5 | | b | product 6 | --------------------------- How do i group these rows by category and apply complicated function in Scala? Maybe something like this: val result = df.groupBy("Category").apply(myComplexFunction) This myComplexFunction should produce

No non-missing arguments warning when using min or max in reshape2

旧城冷巷雨未停 提交于 2019-11-30 05:43:04
I get the following warning when I use min or max in the dcast function from the reshape2 package. What is it telling me? I can't find anything that explains the warning message and I'm a bit confused about why I get it when I use max but not when I use mean or other aggregate functions. Warning message: In .fun(.value[0], ...) : no non-missing arguments to min; returning Inf Here's a reproducible example: data(iris) library(reshape2) molten.iris <- melt(iris,id.var="Species") summary(molten.iris) str(molten.iris) #------------------------------------------------------------ # Both return

Why is there no “product()” aggregate function in SQL? [duplicate]

爱⌒轻易说出口 提交于 2019-11-30 05:31:57
This question already has an answer here: is there a PRODUCT function like there is a SUM function in Oracle SQL? 6 answers When there are Sum(), min(), max(), avg(), count() functions, can someone help understand why there is no product() built-in function. And what will be the most efficient user-implementation of this aggregate function ? Thanks, Trinity If you have exponential and log functions available, then: PRODUCT(TheColumn) = EXP(SUM(LN(TheColumn))) One can make a user-defined aggregate in SQL 2005 and up by using CLR . In Postgresql, you can do it in Postgres itself , likewise with

How to set correct attribute names to a json aggregated result with GROUP BY clause?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 05:06:46
I have a table temp defined like this: id | name | body | group_id ------------------------------- 1 | test_1 | body_1 | 1 2 | test_2 | body_2 | 1 3 | test_3 | body_3 | 2 4 | test_4 | body_4 | 2 I would like to produce a result grouped by group_id and aggregated to json. However, query like this: SELECT group_id, json_agg(ROW(id, name, body)) FROM temp GROUP BY group_id; Produces this result: 1;[{"f1":1,"f2":"test_1","f3":"body_1"}, {"f1":2,"f2":"test_2","f3":"body_2"}] 2;[{"f1":3,"f2":"test_3","f3":"body_3"}, {"f1":4,"f2":"test_4","f3":"body_4"}] The attributes in the json objects are named

MySQL - SUM of a group of time differences

送分小仙女□ 提交于 2019-11-30 04:44:20
问题 I want to sum all the time differences to show the total hours worked by a volunteer. Getting a result set of time differences is easy: Select timediff(timeOut, timeIn) FROM volHours WHERE username = 'skolcz' which gives the list of times by hours but then I want to sum it up to a grand total. So if the result set is: 12:00:00 10:00:00 10:00:00 08:00:00 It would just total 40 hours. This there a way to do something like: SELECT SUM(Select timediff(timeOut,timeIn) FROM volHours WHERE username

Weird behaviour of SUM and CONCAT in MySql

∥☆過路亽.° 提交于 2019-11-30 04:37:50
问题 If I want to make a sum of a specific numeric column in MySQL, I do SELECT SUM(MyColumn) FROM MyTable WHERE 1; This returns for example number 100. But I'd like to prepend some text to the sum value, so I do SELECT CONCAT('Sum is: ',SUM(MyColumn)) FROM MyTable WHERE 1; but instead of getting Sum is: 100 I get something like 546573743a20343030 . Is this a bug or a feature? What am I doing wrong? UPDATE SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS varchar(20))) FROM MyTable WHERE 1; Casting

BigQuery User Defined Aggregation Function?

我的未来我决定 提交于 2019-11-30 04:23:35
问题 I know I can define a User Defined Function in order to perform some custom calculation. I also know I can use the 'out-of-the-box' aggregation functions to reduce a collection of values to a single value when using a GROUP BY clause. Is it possible to define a custom user-defined, Aggregation Function to use with a GROUP BY clause? 回答1: Turns out that this IS possible (as long as the groups we seek to aggregate are of a reasonable size in memory) with a little bit of 'glue' - namely the

string concatenate in group by function with other aggregate functions

杀马特。学长 韩版系。学妹 提交于 2019-11-30 04:15:16
问题 Is it possible to concatenate strings with one or more of other group by function like sum, avg, count etc . Say I have the following table Id Name Order Value 1 a 1 100 2 b 2 200 3 c 1 300 4 d 1 100 5 e 2 300 Now if I want the result to be something of this sort Order Name Value Count 1 a,c,d 500 3 2 b,e 500 2 How can i achieve the same using a query on SQL server. 回答1: Sample table create table t123 (Id int, Name varchar(10), [Order] int, Value int) insert t123 select 1,'a','1',100 union

How to do a count on a union query

送分小仙女□ 提交于 2019-11-30 04:15:12
I have the following query: select distinct profile_id from userprofile_... union select distinct profile_id from productions_... How would I get the count of the total number of results? If you want a total count for all records, then you would do this: SELECT COUNT(*) FROM ( select distinct profile_id from userprofile_... union all select distinct profile_id from productions_... ) x you should use Union All if there are equals rows in both tables, because Union makes a distinct select count(*) from (select distinct profile_id from userprofile_... union ALL select distinct profile_id from