aggregate-functions

Missing 'Median' Aggregate Function in Django?

牧云@^-^@ 提交于 2019-12-04 03:09:42
The Development version of Django has aggregate functions like Avg, Count, Max, Min, StdDev, Sum, and Variance ( link text ). Is there a reason Median is missing from the list? Implementing one seems like it would be easy. Am I missing something? How much are the aggregate functions doing behind the scenes? Because median isn't a SQL aggregate. See, for example, the list of PostgreSQL aggregate functions and the list of MySQL aggregate functions . Mark Chackerian Here's your missing function. Pass it a queryset and the name of the column that you want to find the median for: def median_value

PostgreSQL equivalent for SQL Server GROUP BY WITH ROLLUP

只愿长相守 提交于 2019-12-04 02:46:13
I have an Sql Server Query that is using the ROLLUP clause while grouping. I want an equivalent query in Postgres. Query in SQl Server is: SELECT (CASE WHEN acnt_dba_name Is Null THEN 'Total' ELSE acnt_dba_name END) as account, (CASE WHEN evt_name Is Null THEN '' ELSE evt_name END) as event, COUNT(CASE reg_is_complete WHEN true THEN 1 ELSE Null END) as regsComplete, COUNT(CASE WHEN reg_frn_pro_id > 0 AND reg_is_complete = false THEN 1 ELSE Null END) as regsInComplete, COUNT(CASE WHEN reg_frn_pro_id > 0 THEN Null ELSE 1 END) as regsClicks FROM registrations_view LEFT JOIN events ON (evt_id =

how to group by and return sum row in Postgres

放肆的年华 提交于 2019-12-03 23:33:14
问题 I am stuck here. I have row in Postgres like this: id amount a 5000 a 1500 a 500 b 2000 b 1000 c 4000 How is the sql syntax to get result like this? id amount a 7000 b 3000 c 4000 回答1: SELECT id, SUM(amount) AS amount FROM yourtable GROUP BY id 来源: https://stackoverflow.com/questions/8004655/how-to-group-by-and-return-sum-row-in-postgres

Create two arrays for two fields, keeping sort order of arrays in sync (without subquery)

只愿长相守 提交于 2019-12-03 22:45:05
There is no rhyme or reason for this question other than I was curious about how one would go about doing this. Platform: while I was hoping for a SQL-Standard solution, my main concentration is with PostgreSQL 8.4+ . (I know 9.0+ has some array sorting functions.) SELECT id, group, dt FROM foo ORDER BY id; id | group | dt -------+-------+----------- 1 | foo | 2012-01-01 1 | bar | 2012-01-03 1 | baz | 2012-01-02 2 | foo | 2012-01-01 3 | bar | 2012-01-01 4 | bar | 2012-01-01 4 | baz | 2012-01-01 I know the following query is wrong, but the result is similar to what I'm after; a way to tie the

SparkSQL: conditional sum using two columns

醉酒当歌 提交于 2019-12-03 20:10:59
I hope you can help me with this. I have a DF as follows: val df = sc.parallelize(Seq( (1, "a", "2014-12-01", "2015-01-01", 100), (2, "a", "2014-12-01", "2015-01-02", 150), (3, "a", "2014-12-01", "2015-01-03", 120), (4, "b", "2015-12-15", "2015-01-01", 100) )).toDF("id", "prodId", "dateIns", "dateTrans", "value") .withColumn("dateIns", to_date($"dateIns") .withColumn("dateTrans", to_date($"dateTrans")) I would love to do a groupBy prodId and aggregate 'value' summing it for ranges of dates defined by the difference between the column 'dateIns' and 'dateTrans'. In particular, I would like to

Oracle - RETURNING combined with aggregate functions

邮差的信 提交于 2019-12-03 17:44:17
问题 Oracle supports RETURNING clause which could be very useful. For example for data: CREATE TABLE t(Id INT, Val varchar2(50)); INSERT INTO t(Id, Val) SELECT 10,'a' FROM dual UNION ALL SELECT 20,'b' FROM dual UNION ALL SELECT 30,'a' FROM dual UNION ALL SELECT 40,'b' FROM dual; Query: DECLARE l_cnt INT; BEGIN DELETE FROM t RETURNING COUNT(*) INTO l_cnt; DBMS_OUTPUT.put_line('l_cnt: ' || l_cnt); END; l_cnt: 4 It supports MIN/MAX/AVG/SUM/LISTAGG: DECLARE l_max INT; l_min INT; l_str VARCHAR2(100);

creating a pseudo linked list in sql

↘锁芯ラ 提交于 2019-12-03 17:34:01
I have a table that has the following columns table: route columns: id, location, order_id and it has values such as id, location, order_id 1, London, 12 2, Amsterdam, 102 3, Berlin, 90 5, Paris, 19 Is it possible to do a sql select statement in postgres that will return each row along with the id with the next highest order_id? So I want something like... id, location, order_id, next_id 1, London, 12, 5 2, Amsterdam, 102, NULL 3, Berlin, 90, 2 5, Paris, 19, 3 Thanks select id, location, order_id, lag(id) over (order by order_id desc) as next_id from your_table Creating testbed first: CREATE

Fetching Minimum/Maximum for each group in ActiveRecord

我只是一个虾纸丫 提交于 2019-12-03 13:08:03
问题 This is an age-old question where given a table with attributes 'type', 'variety' and 'price', that you fetch the record with the minimum price for each type there is. In SQL, we can do this by: select f.type, f.variety, f.price from ( select type, min(price) as minprice from table group by type ) as x inner join table as f on f.type = x.type and f.price = x.minprice;` We could perhaps imitate this by: minprices = Table.minimum(:price, :group => type) result = [] minprices.each_pair do |t, p|

SQL vs MySQL: Rules about aggregate operations and GROUP BY

大兔子大兔子 提交于 2019-12-03 12:05:11
In this book I'm currently reading while following a course on databases, the following example of an illegal query using an aggregate operator is given: Find the name and age of the oldest sailor. Consider the following attempt to answer this query: SELECT S.sname, MAX(S.age) FROM Sailors S The intent is for this query to return not only the maximum age but also the name of the sailors having that age. However, this query is illegal in SQL--if the SELECT clause uses an aggregate operation, then it must use only aggregate operations unless the query contains a GROUP BY clause! Some time later

aggregate of an empty result set

为君一笑 提交于 2019-12-03 11:57:16
I would like the aggregates of an empty result set to be 0. I have tried the following: SELECT SUM(COALESCE(capacity, 0)) FROM objects WHERE null IS NOT NULL; Result: sum ----- (1 row) Subquestion: wouldn't the above work in Oracle, using SUM(NVL(capacity, 0)) ? From the documentation page about aggregate functions: It should be noted that except for count , these functions return a null value when no rows are selected . In particular, sum of no rows returns null, not zero as one might expect. The coalesce function may be used to substitute zero for null when necessary. So, if you want to