aggregate-functions

must appear in the GROUP BY clause or be used in an aggregate function

跟風遠走 提交于 2019-11-26 02:26:50
问题 I have a table that looks like this caller \'makerar\' cname | wmname | avg --------+-------------+------------------------ canada | zoro | 2.0000000000000000 spain | luffy | 1.00000000000000000000 spain | usopp | 5.0000000000000000 And I want to select the maximum avg for each cname. SELECT cname, wmname, MAX(avg) FROM makerar GROUP BY cname; but I will get an error, ERROR: column \"makerar.wmname\" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT cname,

MySQL “Group By” and “Order By”

随声附和 提交于 2019-11-26 02:17:51
问题 I want to be able to select a bunch of rows from a table of e-mails and group them by the from sender. My query looks like this: SELECT `timestamp`, `fromEmail`, `subject` FROM `incomingEmails` GROUP BY LOWER(`fromEmail`) ORDER BY `timestamp` DESC The query almost works as I want it — it selects records grouped by e-mail. The problem is that the subject and timestamp don\'t correspond to the most recent record for a particular e-mail address. For example, it might return: fromEmail: john

SQL Query to get aggregated result in comma separators along with group by column in SQL Server

丶灬走出姿态 提交于 2019-11-26 02:13:49
问题 I need to write a sql query on the table such that the result would have the group by column along with the aggregated column with comma separators. My table would be in the below format |`````````|````````| | ID | Value | |_________|________| | 1 | a | |_________|________| | 1 | b | |_________|________| | 2 | c | |_________|________| Expected result should be in the below format |`````````|````````| | ID | Value | |_________|________| | 1 | a,b | |_________|________| | 2 | c | |_________|___

SPARK SQL replacement for mysql GROUP_CONCAT aggregate function

谁都会走 提交于 2019-11-26 01:49:30
问题 I have a table of two string type columns (username, friend) and for each username, I want to collect all of it\'s friends on one row, concatenated as strings (\'username1\', \'friends1, friends2, friends3\'). I know MySql does this by GROUP_CONCAT, is there any way to do this with SPARK SQL? Thanks 回答1: Before you proceed: This operations is yet another another groupByKey . While it has multiple legitimate applications it is relatively expensive so be sure to use it only when required. Not

How to define and use a User-Defined Aggregate Function in Spark SQL?

▼魔方 西西 提交于 2019-11-26 01:47:36
问题 I know how to write a UDF in Spark SQL: def belowThreshold(power: Int): Boolean = { return power < -40 } sqlContext.udf.register(\"belowThreshold\", belowThreshold _) Can I do something similar to define an aggregate function? How is this done? For context, I want to run the following SQL query: val aggDF = sqlContext.sql(\"\"\"SELECT span, belowThreshold(opticalReceivePower), timestamp FROM ifDF WHERE opticalReceivePower IS NOT null GROUP BY span, timestamp ORDER BY span\"\"\") It should

How to define and use a User-Defined Aggregate Function in Spark SQL?

被刻印的时光 ゝ 提交于 2019-11-26 01:36:38
I know how to write a UDF in Spark SQL: def belowThreshold(power: Int): Boolean = { return power < -40 } sqlContext.udf.register("belowThreshold", belowThreshold _) Can I do something similar to define an aggregate function? How is this done? For context, I want to run the following SQL query: val aggDF = sqlContext.sql("""SELECT span, belowThreshold(opticalReceivePower), timestamp FROM ifDF WHERE opticalReceivePower IS NOT null GROUP BY span, timestamp ORDER BY span""") It should return something like Row(span1, false, T0) I want the aggregate function to tell me if there's any values for

Function to Calculate Median in SQL Server

我的梦境 提交于 2019-11-26 01:22:02
问题 According to MSDN, Median is not available as an aggregate function in Transact-SQL. However, I would like to find out whether it is possible to create this functionality (using the Create Aggregate function, user defined function, or some other method). What would be the best way (if possible) to do this - allow for the calculation of a median value (assuming a numeric data type) in an aggregate query? 回答1: 2019 UPDATE: In the 10 years since I wrote this answer, more solutions have been

LISTAGG in Oracle to return distinct values

白昼怎懂夜的黑 提交于 2019-11-26 01:15:54
问题 I am trying to use the LISTAGG function in Oracle. I would like to get only the distinct values for that column. Is there a way in which I can get only the distinct values without creating a function or a procedure? col1 col2 Created_by 1 2 Smith 1 2 John 1 3 Ajay 1 4 Ram 1 5 Jack I need to select col1 and the LISTAGG of col2 (column 3 is not considered). When I do that, I get something like this as the result of LISTAGG : [2,2,3,4,5] I need to remove the duplicate \'2\' here; I need only the

How can I simplify this game statistics query?

谁说胖子不能爱 提交于 2019-11-26 00:19:41
问题 This code works as expected, but I it\'s long and creepy. select p.name, p.played, w.won, l.lost from (select users.name, count(games.name) as played from users inner join games on games.player_1_id = users.id where games.winner_id > 0 group by users.name union select users.name, count(games.name) as played from users inner join games on games.player_2_id = users.id where games.winner_id > 0 group by users.name) as p inner join (select users.name, count(games.name) as won from users inner

Does T-SQL have an aggregate function to concatenate strings? [duplicate]

蓝咒 提交于 2019-11-26 00:17:52
问题 Possible Duplicates: Implode type function in SQL Server 2000? Concatenate row values T-SQL I have a view which I\'m querying that looks like this: BuildingName PollNumber ------------ ---------- Foo Centre 12 Foo Centre 13 Foo Centre 14 Bar Hall 15 Bar Hall 16 Baz School 17 I need to write a query that groups BuildingNames together and displays a list of PollNumbers like this: BuildingName PollNumbers ------------ ----------- Foo Centre 12, 13, 14 Bar Hall 15, 16 Baz School 17 How can I do