aggregate-functions

What can an aggregate function do in the ORDER BY clause?

妖精的绣舞 提交于 2019-11-30 11:59:53
Lets say I have a plant table: id fruit 1 banana 2 apple 3 orange I can do these SELECT * FROM plant ORDER BY id; SELECT * FROM plant ORDER BY fruit DESC; which does the obvious thing. But I was bitten by this, what does this do? SELECT * FROM plant ORDER BY SUM(id); SELECT * FROM plant ORDER BY COUNT(fruit); SELECT * FROM plant ORDER BY COUNT(*); SELECT * FROM plant ORDER BY SUM(1) DESC; All these return just the first row (which is with id = 1). What's happening underhood? What are the scenarios where aggregate function will come in handy in ORDER BY ? Your results are more clear if you

Is Aggregate fatally flawed because each into clause is executed separately?

南楼画角 提交于 2019-11-30 11:58:59
Is VB.NET's Aggregate query fatally flawed when used as the first (outer) clause of a Linq expression with multiple Into clauses because each Into clause is executed separately? The "obvious" answer to SELECT MIN(ZoneMin), MAX(ZoneMin) FROM Plant in LINQ to SQL is Dim limits = Aggregate p In Plants Select p.ZoneMin Into Min(), Max() However, this answer actually retrieves each of Min and Max (and if you include other aggregate functions like Count and Average ) in separate SQL queries. This can be easily seen in LINQPad. Is there a transaction (or something else making these queries atomic)

Is it possible to use Aggregate function in a Select statment without using Group By clause?

早过忘川 提交于 2019-11-30 11:58:24
So far I have written Aggregate function followed by Group By clause to find the values based on SUM, AVG and other Aggregate functions. I have a bit confusion in the Group By clause. When we use Aggregate functions what are the columns I need to specify in the Group By clause. Otherwise Is there any way to use Aggregate functions without using Group By clause. All columns in the SELECT clause that do not have an aggregate need to be in the GROUP BY Good: SELECT col1, col2, col3, MAX(col4) ... GROUP BY col1, col2, col3 Also good: SELECT col1, col2, col3, MAX(col4) ... GROUP BY col1, col2, col3

How can I use SUM for bit columns?

谁说我不能喝 提交于 2019-11-30 10:54:28
How can use the function SUM() for bit columns in T-SQL? When I try do it as below: SELECT SUM(bitColumn) FROM MyTable; I get the error: Operand data type bit is invalid for sum operator. SELECT SUM(CAST(bitColumn AS INT)) FROM dbo.MyTable need to cast into number or another solution - SELECT COUNT(*) FROM dbo.MyTable WHERE bitColumn = 1 You could consider 0 as nulls and simply count the remaining values: SELECT count(nullif(bitColumn, 0)) FROM MyTable; You can achieve by using CONVERT , SELECT SUM(CONVERT(INT, bitColumn)) FROM MyTable SELECT SUM(bitColumn * 1) FROM dbo.MyTable Converts the

Spark custom aggregation : collect_list+UDF vs UDAF

寵の児 提交于 2019-11-30 09:51:12
I often have the need to perform custom aggregations on dataframes in spark 2.1, and used these two approaches : using groupby/collect_list to get all the values in a single row, then apply an UDF to aggregate the values Writing a custom UDAF (User defined aggregate function) I generally prefer the first option as its easier to implement and more readable than the UDAF implementation. But I would assume that the first option is generally slower, because more data is sent around the network (no partial aggregation), but my experience shows that UDAF are generally slow. Why is that? Concrete

Aggregate hstore column in PostreSQL

守給你的承諾、 提交于 2019-11-30 09:21:19
I have a table like this: Table "public.statistics" id | integer | not null default nextval('statistics_id_seq'::regclass) goals | hstore | items: |id |goals | |30059 |"3"=>"123" | |27333 |"3"=>"200", "5"=>"10" | What I need to do for aggregate all values by key in hash? I want to get result like this: select sum(goals) from statistics return |goals | |"3"=>"323", "5"=>"10" | Building on Laurence's answer, here's a pure SQL way to aggregate the summed key/value pairs into a new hstore using array_agg and the hstore(text[], text[]) constructor. http://sqlfiddle.com/#!1/9f1fb/17 SELECT hstore

Returning Multiple Arrays from User-Defined Aggregate Function (UDAF) in Apache Spark SQL

╄→гoц情女王★ 提交于 2019-11-30 09:08:41
I am trying to create a user-defined aggregate function (UDAF) in Java using Apache Spark SQL that returns multiple arrays on completion. I have searched online and cannot find any examples or suggestions on how to do this. I am able to return a single array, but cannot figure out how to get the data in the correct format in the evaluate() method for returning multiple arrays. The UDAF does work as I can print out the arrays in the evaluate() method, I just can't figure out how to return those arrays to the calling code (which is shown below for reference). UserDefinedAggregateFunction

SQL: tuple comparison

送分小仙女□ 提交于 2019-11-30 07:25:56
问题 In my current application, I need to be able to do this type of query: SELECT MIN((colA, colB, colC)) FROM mytable WHERE (colA, colB, colC) BETWEEN (200, 'B', 'C') AND (1000, 'E', 'F') and get the answer of (333, 'B', 'B') , given this data: +------+------+------+ | colA | colB | colC | +------+------+------+ | 99 | A | A | | 200 | A | Z | | 200 | B | B | | 333 | B | B | | 333 | C | D | | 333 | C | E | | 333 | D | C | | 1000 | E | G | | 1000 | F | A | +------+------+------+ What is the most

mysql grouping by week

二次信任 提交于 2019-11-30 07:15:38
I have a table with the following fields: id amount_sale the_date (unix timestamp integer) payment_type (can be Cash, or Account) I am trying to create a query that will group all sales by each week of the year, and then split the sum of amount_sales for each week on my page. Example: week 1 = $26.00 week 2 = $35.00 week 3 = $49.00 etc. I'm using this query but it's not working: SELECT SUM(`amount_sale`) as total FROM `sales` WHERE `payment_type` = 'Account' GROUP BY WEEK(`the_date`) If you store the_date as integer, you first need to convert it to datetime using FROM_UNIXTIME function: SELECT

Postgres CASE in ORDER BY using an alias

↘锁芯ラ 提交于 2019-11-30 06:41:55
I have the following query which works great in Postgres 9.1: SELECT users.id, GREATEST( COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'), COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117') ) AS latest_interaction FROM users LEFT JOIN messages ON users.id = messages.user_id LEFT JOIN phone_calls ON users.id = phone_calls.user_id GROUP BY users.id ORDER BY latest_interaction DESC LIMIT 5; But what I want to do is something like this: SELECT users.id, GREATEST( COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'), COALESCE(MAX(phone_calls.created_at),