aggregate-functions

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

泄露秘密 提交于 2019-12-19 04:04:26
问题 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. 回答1: The CriteriaBuilder interface provides the following arithmetic functions: addition: sum(a, b) substraction: diff(a, b) multiplication: prod(a, b)

Select running total until specific SUM is reached

人盡茶涼 提交于 2019-12-19 03:44:14
问题 I am trying to select the first n rowid values from the following table variable that will get me as close to a sum(itemcount) of 200,000 without crossing that threshhold. If I was looking at this manually, I would just take the top 3 rows. I do not want to use a cursor unless there is no pure-set-based way. What is a good set-based way to get all of the rowid values "sum while/until" I get to a running total of 200,000? I looked at "running totals" at http://www.1keydata.com/sql/sql-running

MySQL SUM json values grouped by json keys

☆樱花仙子☆ 提交于 2019-12-18 17:11:22
问题 Is it possible to calculate sum of json values grouped by json keys? Mysql version is 5.7.17 on Google cloud sql. Example_1: A short example of my point: col1 | col2 -----|----------------------- aaa | {"key1": 1, "key2": 3} -----|----------------------- bbb | {"key1": 0, "key2": 2} -----|----------------------- aaa | {"key1": 50, "key2": 0} SQL query should produce: col1 | col2 -----|----------------------- aaa | {"key1": 51, "key2": 3} -----|----------------------- bbb | {"key1": 0, "key2":

MySQL SUM json values grouped by json keys

谁都会走 提交于 2019-12-18 17:11:17
问题 Is it possible to calculate sum of json values grouped by json keys? Mysql version is 5.7.17 on Google cloud sql. Example_1: A short example of my point: col1 | col2 -----|----------------------- aaa | {"key1": 1, "key2": 3} -----|----------------------- bbb | {"key1": 0, "key2": 2} -----|----------------------- aaa | {"key1": 50, "key2": 0} SQL query should produce: col1 | col2 -----|----------------------- aaa | {"key1": 51, "key2": 3} -----|----------------------- bbb | {"key1": 0, "key2":

SQL Server: collect values in an aggregation temporarily and re-use in the same query

烂漫一生 提交于 2019-12-18 16:47:21
问题 How do I accumulate values in T-SQL? AFAIK there is no ARRAY type. I want to re-use the values in the same query like demonstrated in this PostgreSQL example using array_agg(). SELECT a[1] || a[i] AS foo ,a[2] || a[5] AS bar -- assuming we have >= 5 rows for simplicity FROM ( SELECT array_agg(text_col ORDER BY text_col) AS a ,count(*)::int4 AS i FROM tbl WHERE id between 10 AND 100 ) x How would I best solve this with T-SQL ? Best I could come up with are two CTE and subselects: ;WITH x AS (

Is it possible to perform a bitwise group function?

我只是一个虾纸丫 提交于 2019-12-18 12:21:49
问题 I have a field in a table which contains bitwise flags. Let's say for the sake of example there are three flags: 4 => read, 2 => write, 1 => execute and the table looks like this * : user_id | file | permissions -----------+--------+--------------- 1 | a.txt | 6 ( <-- 6 = 4 + 2 = read + write) 1 | b.txt | 4 ( <-- 4 = 4 = read) 2 | a.txt | 4 2 | c.exe | 1 ( <-- 1 = execute) I'm interested to find all users who have a particular flag set (eg: write) on ANY record. To do this in one query, I

Is it possible to perform a bitwise group function?

纵然是瞬间 提交于 2019-12-18 12:20:59
问题 I have a field in a table which contains bitwise flags. Let's say for the sake of example there are three flags: 4 => read, 2 => write, 1 => execute and the table looks like this * : user_id | file | permissions -----------+--------+--------------- 1 | a.txt | 6 ( <-- 6 = 4 + 2 = read + write) 1 | b.txt | 4 ( <-- 4 = 4 = read) 2 | a.txt | 4 2 | c.exe | 1 ( <-- 1 = execute) I'm interested to find all users who have a particular flag set (eg: write) on ANY record. To do this in one query, I

Postgres CASE in ORDER BY using an alias

送分小仙女□ 提交于 2019-12-18 12:16:39
问题 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(

Why is there no “product()” aggregate function in SQL? [duplicate]

喜夏-厌秋 提交于 2019-12-18 12:02:45
问题 This question already has answers here : is there a PRODUCT function like there is a SUM function in Oracle SQL? (6 answers) Closed 4 years ago . When there are Sum(), min(), max(), avg(), count() functions, can someone help understand why there is no product() built-in function. And what will be the most efficient user-implementation of this aggregate function ? Thanks, Trinity 回答1: If you have exponential and log functions available, then: PRODUCT(TheColumn) = EXP(SUM(LN(TheColumn))) 回答2:

COUNT CASE and WHEN statement in MySQL

别说谁变了你拦得住时间么 提交于 2019-12-18 11:02:36
问题 How to use COUNT CASE and WHEN statement in MySQL query, to count when data is NULL and when it is not NULL in one MySQL query? 回答1: Use: SELECT SUM(CASE WHEN t.your_column IS NULL THEN 1 ELSE 0 END) AS numNull, SUM(CASE WHEN t.your_column IS NOT NULL THEN 1 ELSE 0 END) AS numNotNull FROM YOUR_TABLE t That will sum up the column NULL & not NULL for the entire table. It's likely you need a GROUP BY clause, depending on needs. 来源: https://stackoverflow.com/questions/5045124/count-case-and-when