aggregate-functions

MySQL Group By functionality in different version

南楼画角 提交于 2019-11-28 05:13:56
问题 Following is a simple SQL query: SELECT * FROM *table_name* GROUP BY *column_name* In my system I have MySQL 5.5. It is working absolutely fine. Whereas in my friend's system he have MySQL 5.7, and he is getting the following error: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'testdb.assetentry.entryId' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by It is

Why is PostgreSQL array access so much faster in C than in PL/pgSQL?

回眸只為那壹抹淺笑 提交于 2019-11-28 05:02:39
I have a table schema which includes an int array column, and a custom aggregate function which sums the array contents. In other words, given the following: CREATE TABLE foo (stuff INT[]); INSERT INTO foo VALUES ({ 1, 2, 3 }); INSERT INTO foo VALUES ({ 4, 5, 6 }); I need a "sum" function that would return { 5, 7, 9 } . The PL/pgSQL version, which works correctly, is as follows: CREATE OR REPLACE FUNCTION array_add(array1 int[], array2 int[]) RETURNS int[] AS $$ DECLARE result int[] := ARRAY[]::integer[]; l int; BEGIN --- --- First check if either input is NULL, and return the other if it is -

Why does the following join increase the query time significantly?

天涯浪子 提交于 2019-11-28 02:15:41
I have a star schema here and I am querying the fact table and would like to join one very small dimension table. I can't really explain the following: EXPLAIN ANALYZE SELECT COUNT(impression_id), imp.os_id FROM bi.impressions imp GROUP BY imp.os_id; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=868719.08..868719.24 rows=16 width=10) (actual time=12559.462..12559.466 rows=26 loops=1) -> Seq Scan on impressions imp (cost=0.00..690306.72 rows=35682472 width=10) (actual time=0

SQL select elements where sum of field is less than N

落花浮王杯 提交于 2019-11-28 01:03:26
问题 Given that I've got a table with the following, very simple content: # select * from messages; id | verbosity ----+----------- 1 | 20 2 | 20 3 | 20 4 | 30 5 | 100 (5 rows) I would like to select N messages, which sum of verbosity is lower than Y (for testing purposes let's say it should be 70, then correct results will be messages with id 1,2,3). It's really important to me, that solution should be database independent (it should work at least on Postgres and SQLite). I was trying with

Is there a way to do aggregate functions on Google App Engine?

二次信任 提交于 2019-11-28 00:53:44
One of the nice things relational databases support are the aggregate functions like count, sum, avg etc. But it seems that if you are using GAE, when inserting or updating a record you must calculate and store the count, sum, avg, etc. values of the whole table. But what if you have many conditional groupings? Given a Person: class Person { @Id Integer age; String city; } If I want the total number of persons and the average age Is it correct that everytime I create, update or delete a person I should also calculate both aggregates and store them as separate columns in the same table. If I

mysql count performance

谁都会走 提交于 2019-11-27 23:20:25
select count(*) from mytable; select count(table_id) from mytable; //table_id is the primary_key both query were running slow on a table with 10 million rows. I am wondering why since wouldn't it easy for mysql to keep a counter that gets updated on all insert,update and delete? and is there a way to improve this query? I used explain but didn't help much. As cherouvim pointed out in the comments, it depends on the storage engine. MyISAM does keep a count of the table rows, and can keep it accurate since the only locks MyISAM supports is a table lock. InnoDB however supports transactions, and

Group by data intervals

折月煮酒 提交于 2019-11-27 22:34:32
I have a single table which stores bandwidth usage on the network over a period of time. One column will contain the date time (primary key) and another column will record the bandwidth. Data is recorded every minute. We will have other columns recording other data at that moment in time. If the user requests the data on 15 minute intervals (within a 24 hour period given start and end date), is it possible with a single query to get the data I require or would I have to write a stored procedure/cursor to do this? Users may then request 5 minute intervals data etc. I will most likely be using

Why are aggregate functions not allowed in where clause

我的未来我决定 提交于 2019-11-27 21:27:33
I am looking for clarification on this. I am writing two queries below: We have a table of employee name with columns ID , name , salary 1. Select name from employee where sum(salary) > 1000 ; 2. Select name from employee where substring_index(name,' ',1) = 'nishant' ; Query 1 doesn't work but Query 2 does work. From my development experience, I feel the possible explanation to this is: The sum() works on a set of values specified in the argument. Here 'salary' column is passed , so it must add up all the values of this column. But inside where clause, the records are checked one by one , like

How to avoid error “aggregate functions are not allowed in WHERE”

北城以北 提交于 2019-11-27 20:39:18
问题 This sql code throws an aggregate functions are not allowed in WHERE SELECT o.ID , count(p.CAT) FROM Orders o INNER JOIN Products p ON o.P_ID = p.P_ID WHERE count(p.CAT) > 3 GROUP BY o.ID; How can I avoid this error? 回答1: Replace WHERE clause with HAVING , like this: SELECT o.ID , count(p.CAT) FROM Orders o INNER JOIN Products p ON o.P_ID = p.P_ID GROUP BY o.ID HAVING count(p.CAT) > 3; HAVING is similar to WHERE , that is both are used to filter the resulting records but HAVING is used to

SQL query to sum the data

丶灬走出姿态 提交于 2019-11-27 19:28:23
问题 I have my table data as follows TaxTypeCode1 TaxTypeCode2 PNO Amount ----------------------------------------- TX01 TX02 124 600 TX02 null 124 700 TX03 TX04 124 200 TX04 null 124 300 TX05 TX06 126 400 TX06 null 127 500 TX07 null 128 800 I would like to write SQL query to retrieve data. Conditions apply IF pno is same and TaxTypeCode1 contain TaxTypeCode2 then sum the amt, otherwise display actual amt My expected output is PNO Amount --------------- 124 1300 124 500 126 400 127 500 128 800 124