aggregate-functions

Aggregate functions over arrays

旧街凉风 提交于 2019-12-03 10:42:37
I have a table like this: +-----+----------------+ | ID | array300 | +-----+----------------+ | 100 | {110,25,53,..} | | 101 | {56,75,59,...} | | 102 | {65,93,82,...} | | 103 | {75,70,80,...} | +-----+----------------+ array300 column is an array of 300 elements. I need to have arrays of 100 elements with every element representing the average of 3 elements of array300 . For this example the answer will be like: array100 {62.66,...} {63.33,...} {80,...} {78.33,...} Try something like this: SELECT id, unnest(array300) as val, ntile(100) OVER (PARTITION BY id) as bucket_num FROM your_table This

SQLITE: merging rows into single row if they share a column

孤街醉人 提交于 2019-12-03 08:53:01
From a previous post, I have the following view in sqlite3: CREATE View AttendeeTableView AS SELECT (LastName || " " || FirstName) as AttendeeName, CompanyName, PhotoURI, CompanyAttendeeRelation.CompanyId, CompanyAttendeeRelation.AttendeeId FROM Attendee JOIN CompanyAttendeeRelation on CompanyAttendeeRelation.AttendeeId = Attendee.AttendeeId ORDER BY LastName; Now, since the data is generated from a many-to-many relation between Attendee and Company , I can get results such as: Doe John | company A | johnPic.png | 1 | 15 Doe John | company B | johnPic.png | 2 | 15 What I'd like to do is, in

How to count NULL values in MySQL?

一曲冷凌霜 提交于 2019-12-03 08:52:24
问题 I want to know how can i find all the values that are NULL in the MySQL database for example I'm trying to display all the users who don't have an average yet. Here is the MySQL code. SELECT COUNT(average) as num FROM users WHERE user_id = '$user_id' AND average IS_NULL 回答1: SELECT COUNT(*) as num FROM users WHERE user_id = '$user_id' AND average IS NULL 回答2: A more generic version (that doesn't depend on the where clause and hence limits your overall results): SELECT SUM(CASE WHEN average IS

Code first Entity Framework 6.1 Custom Aggregate Function

我怕爱的太早我们不能终老 提交于 2019-12-03 08:46:31
I have a custom CLR Aggregate function on SQL Server to calculate percentiles. Is it possible to call my custom aggregate function through Entity Framework? How is the mapping configured to allow this? I have tried using codefirstfunctions similar to what is described on Entity Framework 6 Code First Custom Functions , however the functions seem to only be allowed to take scaler parameters, where my function is an aggregate function so will need to take a list of items (similar to how Sum, Averagg and Count work). The Aggregate functions has the following signature, taking in the value we want

Is there a better way to calculate the median (not average)

跟風遠走 提交于 2019-12-03 07:16:50
问题 Suppose I have the following table definition: CREATE TABLE x (i serial primary key, value integer not null); I want to calculate the MEDIAN of value (not the AVG). The median is a value that divides the set in two subsets containing the same number of elements. If the number of elements is even, the median is the average of the biggest value in the lowest segment and the lowest value of the biggest segment. (See wikipedia for more details.) Here is how I manage to calculate the MEDIAN but I

MAX() and MAX() OVER PARTITION BY produces error 3504 in Teradata Query

一曲冷凌霜 提交于 2019-12-03 07:04:41
I am trying to produce a results table with the last completed course date for each course code, as well as the last completed course code overall for each employee. Below is my query: SELECT employee_number, MAX(course_completion_date) OVER (PARTITION BY course_code) AS max_course_date, MAX(course_completion_date) AS max_date FROM employee_course_completion WHERE course_code IN ('M910303', 'M91301R', 'M91301P') GROUP BY employee_number This query produces the following error: 3504 : Selected non-aggregate values must be part of the associated group If I remove the MAX() OVER (PARTITION BY...)

Postgres - aggregate two columns into one item

大城市里の小女人 提交于 2019-12-03 06:42:03
问题 I would like to aggregate two columns into one "array" when grouping. Assume a table like so: friends_map: ================================= user_id friend_id confirmed ================================= 1 2 true 1 3 false 2 1 true 2 3 true 1 4 false I would like to select from this table and group by user_id and get friend_id and confirmed as a concatenated value separated by a comma. Currently I have this: SELECT user_id, array_agg(friend_id) as friends, array_agg(confirmed) as confirmed

Oracle - RETURNING combined with aggregate functions

随声附和 提交于 2019-12-03 06:32:19
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); BEGIN DELETE FROM t RETURNING MAX(id), MIN(id), LISTAGG(id, ',') WITHIN GROUP(ORDER BY id) INTO l_max, l

Django Aggregation - Expression contains mixed types. You must set output_field

狂风中的少年 提交于 2019-12-03 03:12:21
I'm trying to achive an Aggregation Query and that's my code: TicketGroup.objects.filter(event=event).aggregate( total_group=Sum(F('total_sold')*F('final_price'))) I have 'total_sold' and 'final_price' in TicketGroup object and all what I want to do is sum and multiply values to get the total sold of all TicketGroups together. All I get is this error: Expression contains mixed types. You must set output_field What I am doing wrong, since I'm calling 'total_group' as my output field? Thanks! By output_field Django means to provide field type for the result of the Sum . from django.db.models

SQL frequency distribution query to count ranges with group-by and include 0 counts

浪尽此生 提交于 2019-12-03 00:08:13
Given: table 'thing': age --- 3.4 3.4 10.1 40 45 49 I want to count the number of things for each 10-year range, e.g., age_range | count ----------+------- 0 | 2 10| 1 20| 0 30| 0 40| 3 This query comes close: SELECT FLOOR(age / 10) as age_range, COUNT(*) FROM thing GROUP BY FLOOR(age / 10) ORDER BY FLOOR(age / 10); Output: age_range | count -----------+------- 0 | 1 1 | 2 4 | 3 However, it doesn't show me the ranges which have 0 counts. How can I modify the query so that it also shows the ranges in between with 0 counts? I found similar stackoverflow questions for counting ranges, some for 0