aggregate-functions

how to group by and return sum row in Postgres

浪子不回头ぞ 提交于 2019-12-01 02:23:36
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 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

Percentile aggregate for SQL Server 2008 R2

扶醉桌前 提交于 2019-12-01 01:43:52
I'm using SQL Server 2008 R2. I need to compute a percentile value per group, something like: SELECT id, PCTL(0.9, x) -- for the 90th percentile FROM my_table GROUP BY id ORDER BY id For example, given this DDL ( fiddle ) --- CREATE TABLE my_table (id INT, x REAL); INSERT INTO my_table VALUES (7, 0.164595), (5, 0.671311), (7, 0.0118385), (6, 0.704592), (3, 0.633521), (3, 0.337268), (0, 0.54739), (6, 0.312282), (0, 0.220618), (7, 0.214973), (6, 0.410768), (7, 0.151572), (7, 0.0639506), (5, 0.339075), (1, 0.284094), (2, 0.126722), (2, 0.870079), (3, 0.369366), (1, 0.6687), (5, 0.199456), (5, 0

How sum with case conditional statement works in sql

天大地大妈咪最大 提交于 2019-12-01 01:28:55
The other day, I gave an answer to this question but then other user solved that problem with sum + case conditional statement to add one edge condition in result. So, the question came to my mind, how statement sum(case when jobname = 'Analyst' then 1 else 0 end) in the below query works select d.* from (select deptno, sum(case when jobname = 'Analyst' then 1 else 0 end) as numAnalysts from employees group by deptno order by numAnalysts asc ) d where rownum = 1;` and return the number of employees over a department. Also, I would like to understand the performance of this query. Before

Aggregate variables in list of data frames into single data frame

眉间皱痕 提交于 2019-12-01 01:23:36
I am performing a per policy life insurance valuation in R. Monthly cash flow projections are performed per policy and returns a data frame in the following format (for example): Policy1 = data.frame(ProjM = 1:200, Cashflow1 = rep(5,200), Cashflow2 = rep(10,200)) My model returns a list (using lapply and a function which performs the per policy cashflow projection - based on various per policy details, escalation assumptions and life contingencies). I want to aggregate the cash flows across all policies by ProjM . The following code does what I want, but looking for a more memory efficient way

Spark UDAF - using generics as input type?

寵の児 提交于 2019-12-01 00:46:21
I want to write Spark UDAF where type of the column could be any that has a Scala Numeric defined on it. I've searched over Internet but found only examples with concrete types like DoubleType , LongType . Isn't this possible? But how then use that UDAFs with other numeric values? For simplicity let's assume you want to define a custom sum . You'll have provide a TypeTag for the input type and use Scala reflection to define schemas: import org.apache.spark.sql.expressions._ import org.apache.spark.sql.types._ import org.apache.spark.sql.Row import scala.reflect.runtime.universe._ import org

How to use CriteriaQuery SUM of custom operation on some cells?

十年热恋 提交于 2019-11-30 23:11:42
Consider you have table T, with fields A and B. With regular SQL, I could do this: SELECT SUM(A * (100.0 - B) / 100.0) AS D FROM T; And I would get exactly what I expect. However, I'm not sure how to do it with CriteriaQuery. I know how to do sum over 1 field, but not how to do sum over some math expression over multiple fields in a row. The CriteriaBuilder interface provides the following arithmetic functions: addition: sum(a, b) substraction: diff(a, b) multiplication: prod(a, b) division: quot(a, b) where a b parameters can be an expression and/or literal. As for the query, here is an

string concatenate in group by function with other aggregate functions

我们两清 提交于 2019-11-30 20:46:56
Is it possible to concatenate strings with one or more of other group by function like sum, avg, count etc . Say I have the following table Id Name Order Value 1 a 1 100 2 b 2 200 3 c 1 300 4 d 1 100 5 e 2 300 Now if I want the result to be something of this sort Order Name Value Count 1 a,c,d 500 3 2 b,e 500 2 How can i achieve the same using a query on SQL server. Sample table create table t123 (Id int, Name varchar(10), [Order] int, Value int) insert t123 select 1,'a','1',100 union all select 2,'b','2',200 union all select 3,'c','1',300 union all select 4,'d','1',100 union all select 5,'e',

BigQuery User Defined Aggregation Function?

微笑、不失礼 提交于 2019-11-30 20:17:23
I know I can define a User Defined Function in order to perform some custom calculation. I also know I can use the 'out-of-the-box' aggregation functions to reduce a collection of values to a single value when using a GROUP BY clause. Is it possible to define a custom user-defined, Aggregation Function to use with a GROUP BY clause? Turns out that this IS possible (as long as the groups we seek to aggregate are of a reasonable size in memory) with a little bit of 'glue' - namely the ARRAY_AGG function The steps are as follows: Create a UDF with an input parameter of type ARRAY<T> where T is

Weird behaviour of SUM and CONCAT in MySql

我只是一个虾纸丫 提交于 2019-11-30 20:12:39
If I want to make a sum of a specific numeric column in MySQL, I do SELECT SUM(MyColumn) FROM MyTable WHERE 1; This returns for example number 100. But I'd like to prepend some text to the sum value, so I do SELECT CONCAT('Sum is: ',SUM(MyColumn)) FROM MyTable WHERE 1; but instead of getting Sum is: 100 I get something like 546573743a20343030 . Is this a bug or a feature? What am I doing wrong? UPDATE SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS varchar(20))) FROM MyTable WHERE 1; Casting to varchar doesn't work: getting SQL syntax error. NumberFour As FreshPrinceOfSO suggested in the

Ordering distinct column values by (first value of) other column in aggregate function

◇◆丶佛笑我妖孽 提交于 2019-11-30 18:58:40
I'm trying to order the output order of some distinct aggregated text based on the value of another column with something like: string_agg(DISTINCT sometext, ' ' ORDER BY numval) However, that results in the error: ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list I do understand why this is, since the ordering would be "ill-defined" if the numval of two repeated values differs, with that of another lying in-between. Ideally, I would like to order them by first appearance / lowest order-by value, but the ill-defined cases are actually rare enough in my