aggregate-functions

replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)

假如想象 提交于 2019-11-27 03:37:28
问题 for SQL Server 2008 R2 I have a resultset that looks like this (note [price] is numeric, NULL below represents a NULL value, the result set is ordered by product_id and timestamp) product timestamp price ------- ---------------- ----- 5678 2008-01-01 12:00 12.34 5678 2008-01-01 12:01 NULL 5678 2008-01-01 12:02 NULL 5678 2008-01-01 12:03 23.45 5678 2008-01-01 12:04 NULL I want to transform that to a result set that (essentially) copies a non-null value from the latest preceding row, to produce

Aggregate a single column in query with many columns

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:23:42
Is there a proper way to aggregate a single column when I have many other columns in the query? I've tried this answer which works, but my query has become a lot more verbose. My current query looks like this: SELECT t1.foo1, t1.foo2, t2.foo3, t2.foo4, string_agg(t3.aggregated_field, ', ') FROM tbl1 t1 LEFT JOIN tbl2 t2 ON t1.id = t2.fkeyid LEFT JOIN tbl3 t3 ON t2.id = t3.fkeyid GROUP BY t1.foo1, t1.foo2, t2.foo3, t2.foo4, t2.foo5, t2.foo6 ORDER BY t2.foo5, t2.foo6 The query has many more fields and LEFT JOIN s, the important part is that all these fields have 1 to 1 or 1 to 0 relationship

How to find mean of grouped Vector columns in Spark SQL?

寵の児 提交于 2019-11-27 02:02:25
I have created a RelationalGroupedDataset by calling instances.groupBy(instances.col("property_name")) : val x = instances.groupBy(instances.col("property_name")) How do I compose a user-defined aggregate function to perform Statistics.colStats().mean on each group? Thanks! user6910411 Spark >= 2.4 You can use Summarizer : import org.apache.spark.ml.stat.Summarizer val dfNew = df.as[(Int, org.apache.spark.mllib.linalg.Vector)] .map { case (group, v) => (group, v.asML) } .toDF("group", "features") dfNew .groupBy($"group") .agg(Summarizer.mean($"features").alias("means")) .show(false) +-----+---

Aggregate function in MySQL - list (like LISTAGG in Oracle)

青春壹個敷衍的年華 提交于 2019-11-27 01:57:41
I need function, that returns list of strings. I have data in table like this: Id MyString ------------------------ 1 First 2 Second 3 Third 4 Fourth I need function like this (something like this works in oracle): select LISTAGG(MyString, ', ') as myList where id < 4 That returns something like this: myList ------------------------ First, Second, Third Any ideas? You're looking for GROUP_CONCAT() Try this: select group_concat(MyString separator ', ') as myList from table where id < 4 Of course, you can group by the results. As of MySQL 5.7.22 you can also use two JSON aggregation functions:

Grouped string aggregation / LISTAGG for SQL Server

时光毁灭记忆、已成空白 提交于 2019-11-27 01:35:59
I'm sure this has been asked but I can't quite find the right search terms. Given a schema like this: | CarMakeID | CarMake ------------------------ | 1 | SuperCars | 2 | MehCars | CarMakeID | CarModelID | CarModel ----------------------------------------- | 1 | 1 | Zoom | 2 | 1 | Wow | 3 | 1 | Awesome | 4 | 2 | Mediocrity | 5 | 2 | YoureSettling I want to produce a dataset like this: | CarMakeID | CarMake | CarModels --------------------------------------------- | 1 | SuperCars | Zoom, Wow, Awesome | 2 | MehCars | Mediocrity, YoureSettling What do I do in place of 'AGG' for strings in SQL

Why can't you mix Aggregate values and Non-Aggregate values in a single SELECT?

≡放荡痞女 提交于 2019-11-27 01:35:20
I know that if you have one aggregate function in a SELECT statement, then all the other values in the statement must be either aggregate functions, or listed in a GROUP BY clause. I don't understand why that's the case. If I do: SELECT Name, 'Jones' AS Surname FROM People I get: NAME SURNAME Dave Jones Susan Jones Amy Jones So, the DBMS has taken a value from each row, and appended a single value to it in the result set. That's fine. But if that works, why can't I do: SELECT Name, COUNT(Name) AS Surname FROM People It seems like the same idea, take a value from each row and append a single

MYSQL how to select data where a field has a min value

ⅰ亾dé卋堺 提交于 2019-11-27 00:43:37
问题 Please I want to select data from a table, where a specific field has the min value, I've tried this : SELECT * FROM pieces where min(price) I'm not good with MySQL, please any help ? Thanks 回答1: this will give you result that has the minimum price on all records. SELECT * FROM pieces WHERE price = ( SELECT MIN(price) FROM pieces ) SQLFiddle Demo 回答2: This is how I would do it (assuming I understand the question) SELECT * FROM pieces ORDER BY price ASC LIMIT 1 If you are trying to select

Best way to count records by arbitrary time intervals in Rails+Postgres

守給你的承諾、 提交于 2019-11-27 00:32:06
My app has a Events table with time-stamped events. I need to report the count of events during each of the most recent N time intervals. For different reports, the interval could be "each week" or "each day" or "each hour" or "each 15-minute interval". For example, a user can display how many orders they received each week, day, or hour, or quarter-hour. 1) My preference is to dynamically do a single SQL query (I'm using Postgres) that groups by an arbitrary time interval. Is there a way to do that? 2) An easy but ugly brute force way is to do a single query for all records within the start

PostgreSQL - GROUP BY clause

a 夏天 提交于 2019-11-26 23:37:18
问题 I want to search by tags, and then list all articles with that tag, and also how many of given tags they match. So for example I might have: Page1 - 2 (has css and php tag) Page2 - 1 (has only css tag) Query: SELECT COUNT(t.tag) FROM a_tags t JOIN w_articles2tag a2t ON a2t.tag = t.id JOIN w_article a ON a.id = a2t.article WHERE t.tag = 'css' OR t.tag = 'php' GROUP BY t.tag LIMIT 9 When I only put COUNT(t.tag) the query works, and I get okay results. But if I append e.g. ID of my article I get

GROUP BY without aggregate function

纵饮孤独 提交于 2019-11-26 21:41:54
I am trying to understand GROUP BY (new to oracle dbms) without aggregate function. How does it operate? Here is what i have tried. EMP table on which i will run my SQL. SELECT ename , sal FROM emp GROUP BY ename , sal SELECT ename , sal FROM emp GROUP BY ename; Result ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause: *Action: Error at Line: 397 Column: 16 SELECT ename , sal FROM emp GROUP BY sal; Result ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause: *Action: Error at Line: 411 Column: 8 SELECT empno , ename , sal