aggregate-functions

Find most common elements in array with a group by

天大地大妈咪最大 提交于 2019-11-27 19:24:53
问题 I have a table of rows with the following structure name TEXT, favorite_colors TEXT[], group_name INTEGER where each row has a list of everyone's favorite colors and the group that person belongs to. How can I GROUP BY group_name and return a list of the most common colors in each group? Could you do a combination of int[] && int[] to set for overlap, int[] & int[] to get the intersection and then something else to count and rank? 回答1: Quick and dirty: SELECT group_name, color, count(*) AS ct

MySQL: GROUP_CONCAT with LEFT JOIN

天大地大妈咪最大 提交于 2019-11-27 17:41:29
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 computer is not booting.'); INSERT INTO Tickets VALUES(2, 'Jane Doe', 'My browser keeps crashing.');

mysql count the sum of all rows

喜夏-厌秋 提交于 2019-11-27 17:38:25
问题 I have a mysql table that has a number of rows, and in each row a field called "value", the field value will differ from row to row. What I want, is to select all the rows and count the sum of all the "value" fields. any idea? 回答1: Do you mean like this? SELECT SUM(value) FROM myTable If you have multiple columns to return, simply add each non-aggregate (i.e., summed) row to the GROUP BY clause: SELECT firstName, lastName, SUM(value) FROM myTable GROUP BY firstName, lastName 回答2: SELECT SUM

Oracle SQL - Sum and group data by week

本秂侑毒 提交于 2019-11-27 17:21:30
问题 I have records related to dates: DATE AMOUNT 16.03.2013 3 16.03.2013 4 16.03.2013 1 16.03.2013 3 17.03.2013 4 17.03.2014 3 I know how to sum them up for each day, but how could I sum them up by week?` 回答1: Try this SELECT to_char(DATE - 7/24,'IYYY'), to_char(DATE - 7/24,'IW'),SUM(AMOUNT) FROM YourTable GROUP BY to_char(DATE - 7/24,'IYYY'), to_char(DATE - 7/24,'IW') FIDDLE DEMO Output would be: +-----+-------+--------+ |YEAR | WEEK | AMOUNT | +-----+-------+--------+ |2013 | 11 | 18 | |2013 |

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

时光毁灭记忆、已成空白 提交于 2019-11-27 17:17:39
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 -------- ----- 1 'Alan, David, David, John' 2 'Charles, Julie' Any ideas how I can filter the names by

Why Mysql's Group By and Oracle's Group by behaviours are different

白昼怎懂夜的黑 提交于 2019-11-27 16:45:54
问题 Why Mysql's Group By and Oracle's Group by behaviours are different I found many times that Mysql's groupBy functionality and Oracle's GroupBy funcnality are behaving different Many times I found error in Oracle(which is actually wrong query) but Mysql will give result in to this so is there any reason behind this Mysql weird behavior 回答1: The MySQL designers put in their nonstandard extension to GROUP BY in an attempt to make development easier and certain queries more efficient. Here's

Bigquery SQL for sliding window aggregate

你。 提交于 2019-11-27 16:40:23
问题 Hi I have a table that looks like this Date Customer Pageviews 2014/03/01 abc 5 2014/03/02 xyz 8 2014/03/03 abc 6 I want to get page view aggregates grouped by week but showing aggregates for past 30 days - (sliding window aggregates with window-size of 30 days for every week) I am using google bigquery EDIT: Gordon - re your comment about "Customer", Actually what I need is slightly more complicated thats why I included customer in the table above. I am looking to get the number of customers

SQL Query: Return Max value record of a Group

亡梦爱人 提交于 2019-11-27 16:30:00
I have a sample table with similar structure & data as shown below: +------+---------+-------------+------------+ | S_ID | S_NAME | SUBJECT | MARK_VALUE | +------+---------+-------------+------------+ | 1 | Stud | SUB_1 | 50 | | 2 | Stud | SUB_2 | 60 | | 3 | Stud | SUB_3 | 70 | | 4 | Stud_1 | SUB_1 | 40 | | 5 | Stud_1 | SUB_2 | 50 | | 6 | Stud_2 | SUB_2 | 40 | +------+---------+-------------+------------+ Table has consolidated mark of each student in all subjects each that student has appeared. Please help me, to write a query to extract MAXIMUM mark obtained by each student ( irrespective of

How to combine aggregate functions in MySQL?

北城以北 提交于 2019-11-27 15:09:58
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 statistic down for each question? (users can answer the same question multiple times). Something like:

date_trunc 5 minute interval in PostgreSQL [duplicate]

谁说我不能喝 提交于 2019-11-27 14:35:43
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What is the fastest way to truncate timestamps to 5 minutes in Postgres? Postgresql SQL GROUP BY time interval with arbitrary accuracy (down to milli seconds) I want to aggregate data at a 5 minute interval in PostgreSQL. If I use the date_trunc() function, I can aggregate data at an hourly, monthly, daily, weekly, etc. interval but not a specific interval like 5 minute or 5 days. select date_trunc('hour', date1