aggregate-functions

mysql count performance

谁说胖子不能爱 提交于 2019-11-26 21:25:10
问题 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. 回答1: 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

Intersection of multiple arrays in PostgreSQL

此生再无相见时 提交于 2019-11-26 21:23:47
问题 I have a view defined as: CREATE VIEW View1 AS SELECT Field1, Field2, array_agg(Field3) AS AggField FROM Table1 GROUP BY Field1, Field2; What I would like to do is get the intersection of the arrays in AggField with something like: SELECT intersection(AggField) FROM View1 WHERE Field2 = 'SomeValue'; Is this at all possible, or is there a better way to achieve what I want? 回答1: The closest thing to an array intersection that I can think of is this: select array_agg(e) from ( select unnest(a1)

SQL comma-separated row with Group By clause

与世无争的帅哥 提交于 2019-11-26 21:02:46
I have the following query: SELECT Account, Unit, SUM(state_fee), Code FROM tblMta WHERE MTA.Id = '123' GROUP BY Account,Unit This of course throws an exception because the Code is not in the group by clause. Each state_fee has a code. How do I get this code to display in 1 record (1 code per state_fee which is multiple state_fee per unit) as a comma-separated list? I looked into different solutions on here but I couldn't find any that worked with a group by . Adrian Carneiro You want to use FOR XML PATH construct: SELECT ACCOUNT, unit, SUM(state_fee), Stuff((SELECT ', ' + code FROM tblmta t2

Why are aggregate functions not allowed in where clause

别说谁变了你拦得住时间么 提交于 2019-11-26 20:26:17
问题 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

MySQL: GROUP_CONCAT with LEFT JOIN

主宰稳场 提交于 2019-11-26 19:08:00
问题 I'm experiencing a problem with MySQL's "GROUP_CONCAT" function. I will illustrate my problem using a simple help desk database: CREATE TABLE Tickets ( id INTEGER NOT NULL PRIMARY KEY, requester_name VARCHAR(255) NOT NULL, description TEXT NOT NULL); CREATE TABLE Solutions ( id INTEGER NOT NULL PRIMARY KEY, ticket_id INTEGER NOT NULL, technician_name VARCHAR(255) NOT NULL, solution TEXT NOT NULL, FOREIGN KEY (ticket_id) REFERENCES Tickets.id); INSERT INTO Tickets VALUES(1, 'John Doe', 'My

How to use Oracle's LISTAGG function with a unique filter? [duplicate]

拈花ヽ惹草 提交于 2019-11-26 18:55:07
问题 This question already has an answer here: LISTAGG in Oracle to return distinct values 23 answers I have a table like this: group_id name -------- ---- 1 David 1 John 1 Alan 1 David 2 Julie 2 Charles And I want the following result: group_id names -------- ----- 1 'Alan, David, John' 2 'Charles, Julie' I can use the following query: select group_id, listagg(name, ',') within group (order by name) as names from demotable group by group_id To get this (very similar result): group_id names ------

How to fetch the first and last record of a grouped record in a MySQL query with aggregate functions?

早过忘川 提交于 2019-11-26 18:32:13
I am trying to fetch the first and the last record of a 'grouped' record. More precisely, I am doing a query like this SELECT MIN(low_price), MAX(high_price), open, close FROM symbols WHERE date BETWEEN(.. ..) GROUP BY YEARWEEK(date) but I'd like to get the first and the last record of the group. It could by done by doing tons of requests but I have a quite large table. Is there a (low processing time if possible) way to do this with MySQL? Joao Costa You want to use GROUP_CONCAT and SUBSTRING_INDEX : SUBSTRING_INDEX( GROUP_CONCAT(CAST(open AS CHAR) ORDER BY datetime), ',', 1 ) AS open

How to combine aggregate functions in MySQL?

ε祈祈猫儿з 提交于 2019-11-26 18:29:10
问题 I'm just learning MySQL - is there a way to combine (or nest) aggregate functions? Given a query: SELECT user, count(answer) FROM surveyValues WHERE study='a1' GROUP BY user; This will give me the number of questions answered by each user. What I really want is the average number of questions answered per user...something like: SELECT avg(count(answer)) FROM surveyValues WHERE study='a1'; What's the correct way to compute this statistic? If this is possible, is there a way to then break this

Count cumulative total in Postgresql

对着背影说爱祢 提交于 2019-11-26 17:58:18
问题 I am using count and group by to get the number of subscribers registered each day: SELECT created_at, COUNT(email) FROM subscriptions GROUP BY created at; Result: created_at count ----------------- 04-04-2011 100 05-04-2011 50 06-04-2011 50 07-04-2011 300 I want to get the cumulative total of subscribers every day instead. How do I get this? created_at count ----------------- 04-04-2011 100 05-04-2011 150 06-04-2011 200 07-04-2011 500 回答1: With larger datasets, window functions are the most

Postgres window function and group by exception

▼魔方 西西 提交于 2019-11-26 17:46:43
I'm trying to put together a query that will retrieve the statistics of a user (profit/loss) as a cumulative result, over a period of time. Here's the query I have so far: SELECT p.name, e.date, sum(sp.payout) OVER (ORDER BY e.date) - sum(s.buyin) OVER (ORDER BY e.date) AS "Profit/Loss" FROM result r JOIN game g ON r.game_id = g.game_id JOIN event e ON g.event_id = e.event_id JOIN structure s ON g.structure_id = s.structure_id JOIN structure_payout sp ON g.structure_id = sp.structure_id AND r.position = sp.position JOIN player p ON r.player_id = p.player_id WHERE p.player_id = 17 GROUP BY p