aggregate-functions

SQL aggregation query, grouping by entries in junction table

耗尽温柔 提交于 2019-12-13 01:07:49
问题 I have TableA in a many-to-many relationship with TableC via TableB. That is, TableA TableB TableC id | val fkeyA | fkeyC id | data I wish the do select sum(val) on TableA, grouping by the relationship(s) to TableC. Every entry in TableA has at least one relationship with TableC. For example, TableA 1 | 25 2 | 30 3 | 50 TableB 1 | 1 1 | 2 2 | 1 2 | 2 2 | 3 3 | 1 3 | 2 should output 75 30 since rows 1 and 3 in Table have the same relationships to TableC, but row 2 in TableA has a different

Division with Aggregate Functions in SQL Not Behaving as Expected

依然范特西╮ 提交于 2019-12-12 21:18:29
问题 I'm trying to do some crosstabs in SQL Server 2008 R2. That part is alright, however, if I try to get percentages for each cell, I run into a problem. Here is a distilled use case: A survey where people give their favorite color and their favorite fruit. I'd like to know how many like a given fruit AND a given color: with survey as ( select 'banana' fav_fruit, 'yellow' fav_color union select 'banana', 'red' union select 'apple', 'yellow' union select 'grape', 'red' union select 'apple', 'blue

Executing queries dynamically in PL/pgSQL

不想你离开。 提交于 2019-12-12 19:19:07
问题 I have found solutions (I think) to the problem I'm about to ask for on Oracle and SQL Server, but can't seem to translate this into a Postgres solution. I am using Postgres 9.3.6. The idea is to be able to generate "metadata" about the table content for profiling purposes. This can only be done (AFAIK) by having queries run for each column so as to find out, say... min/max/count values and such. In order to automate the procedure, it is preferable to have the queries generated by the DB,

Aggregations for Timedelta values in the Python DataFrame

百般思念 提交于 2019-12-12 18:44:12
问题 I have big DataFrame (df) which looks like: Acc_num date_diff 0 29 0:04:43 1 29 0:01:43 2 29 2:22:45 3 29 0:16:21 4 29 0:58:20 5 30 0:00:35 6 34 7:15:26 7 34 4:40:01 8 34 0:56:02 9 34 6:53:44 10 34 1:36:58 ..... Acc_num int64 date_diff timedelta64[ns] dtype: object I need to calculate 'date_diff' mean (in timedelta format) for each account number. df.date_diff.mean() works correctly. But when I try next: df.groupby('Acc_num').date_diff.mean() it raises an exception: "DataError: No numeric

How can I sum up data in tree-like structure in SQL from children to parent?

寵の児 提交于 2019-12-12 18:13:27
问题 I have a query for selecting amounts per department in a tree-like structure. I want to display the sum amount of the children on their respective parent. Is it possible to archive this in a query without using a cursor? Below is a resultset of the data to sum up. A full sample can also be found on sqlfiddle. Results : | DEPARTMENT_ID | PARENT_DEP_ID | DEPARTMENT | AMOUNT | |---------------|---------------|----------------|-----------------| | 1 | 0 | 1 | 0 | | 7 | 1 | 11 | 0 | | 34 | 7 | 111

FIRST aggregate function which I can use with HAVING clause

我的梦境 提交于 2019-12-12 16:08:22
问题 I have a weird requirement which I need to use inside my Stored Procedure in SQL Server 2008 R2. I need a FIRST aggregate function which returns the first element of a sequence and I will use that with HAVING clause. Let me give you an example: DECLARE @fooTable AS TABLE( ID INT, CategoryName NVARCHAR(100), Name NVARCHAR(100), MinAllow INT, Price DECIMAL(18,2) ); INSERT INTO @fooTable VALUES(1, 'Cat1', 'Product1', 2, 112.2); INSERT INTO @fooTable VALUES(2, 'Cat2', 'Product2', 4, 12.34);

Limit result set in sql window function

六眼飞鱼酱① 提交于 2019-12-12 14:27:57
问题 Assume I would like to rewrite the following aggregate query select id, max(hittime) from status group by id using an aggregate windowing function like select id, max(hittime) over(partition by id order by hittime desc) from status How can I specify, that I am only interested in the first result within the partition? EDIT: I was thinking that there might be a solution with [ RANGE | ROWS ] BETWEEN frame_start AND frame_end. What to get not only max(hittime) but also the second, third ... 回答1:

PostgreSQL: sort an array of elements using some sorting condition

Deadly 提交于 2019-12-12 14:02:53
问题 Suppose you need to sort an array of numranges by, say, descending left boundary. Is the following approach the simplest: unnest the array into a table, sort the table, array_agg it back into an array. How would that look in code? Here is my non-working attempt: DO $$ DECLARE x numrange[] := '{"[0, 3]", "[0, 1]", "[3, 5]", "[3, 8]"}'; BEGIN x := ( WITH x AS ( SELECT xrow FROM unnest(x) AS xrow ) SELECT array_agg(xrow) FROM x ORDER BY lower(xrow) DESC ); RAISE NOTICE '%', x; END; $$; 回答1: You

Strange behavior of HSQLDB and group by

萝らか妹 提交于 2019-12-12 13:15:55
问题 I am using HSQLDB for writing junits and my query is like this: String queryStr = "from ManualUrlBatchModel where status IN(:status) group by batchUser order by creationTime"; Query query = getSession(requestType).createQuery(queryStr); query.setParameterList("status", status); I am retrieving one batch per user in the given status (depending on creation time FIFO order). It runs fine for end to end testing but fails while writing junits. Exception says: Caused by: java.sql.SQLException: Not

Grouped aggregations with Yii STAT?

蹲街弑〆低调 提交于 2019-12-12 12:43:01
问题 I have a Yii STAT Relation that's defined to provide a grouped SUM result, however when I access the relation in my View , the only value is the latest single value rather than each value . For example, here's my relation: 'total_salaries_by_job' => array( self::STAT, 'Employee', 'department_id', 'select' => 'job_type_id, SUM(salary)', 'group'=>"job_type_id" ) This generates the following SQL: SELECT department_id AS c , job_type_id , SUM(salary) AS s FROM Employee AS t WHERE t.department_id