aggregate-functions

Cumulative sum over days

↘锁芯ラ 提交于 2019-12-10 09:05:32
问题 I have a MySQL table like the following: date count 2010-01-01 5 2010-01-02 6 2010-01-03 7 How can I accumulate the sum of each day to the next one? So the result is like: date acum per day 2010-01-01 5 2010-01-02 11 2010-01-03 18 I think i need some kind of for(each date)... but no clue. Just the final query i used following answer from Eric. (thanks). SELECT t1.dia, sum(t2.operacions), sum(t2.amount) FROM (SELECT count(*) operations, sum(amount), date(b.timestamp) dia FROM transactions b

ios most efficient way to get average value while also filtering out some objects

会有一股神秘感。 提交于 2019-12-09 22:32:01
问题 I'm trying to get the average value of an attribute in a child entity while also trying to only include a select set of records. I have two entities in my Core Data model: Invoice and InvoiceDetail. Invoice:<br> invoiceNum - attribute<br> invoiceDate - attribute<br> invoiceDetails - one-to-many relationship to InvoiceDetail InvoiceDetail:<br> itemAmount - attribute<br> itemType - attribute<br> invoice - one-to-one relationship to Invoice<br> If I wanted to just get the average value of

Get latest child per parent from big table - query is too slow

落花浮王杯 提交于 2019-12-09 22:20:01
问题 I have a query generated by Django's ORM, that is taking hours to run. The report_rank table (50 million rows) is in a one to many relation to report_profile (100k rows). I'm trying to retrieve the latest report_rank for each report_profile . I'm running Postgres 9.1 on an extra large Amazon EC2 server with plenty of available RAM (2GB/15GB used). Disk IO is pretty bad of course. I have indexes on report_rank.created as well as on all foreign key fields. What can I do to speed this query up?

in postgres select, return a column subquery as an array?

五迷三道 提交于 2019-12-09 15:47:02
问题 (have done this before, but memory fades, as does goggle) wish to get select from users with the tag.tag_id s for each user returned as an array. select usr_id, name, (select t.tag_id from tags t where t.usr_id = u.usr_id) as tag_arr from users u; with the idea embedded query tag_arr would be an array 回答1: Use the aggregate function: select usr_id, name, array_agg(tag_id) as tag_arr from users join tags using(usr_id) group by usr_id, name or an array constructor from the results of a subquery

Applying a custom groupby aggregate function to output a binary outcome in pandas python

柔情痞子 提交于 2019-12-08 23:47:06
问题 I have a dataset of trader transactions where the variable of interest is Buy/Sell which is binary and takes on the value of 1 f the transaction was a buy and 0 if it is a sell. An example looks as follows: Trader Buy/Sell A 1 A 0 B 1 B 1 B 0 C 1 C 0 C 0 I would like to calculate the net Buy/Sell for each trader such that if the trader had more than 50% of trades as a buy, he would have a Buy/Sell of 1, if he had less than 50% buy then he would have a Buy/Sell of 0 and if it were exactly 50%

Cassandra aggregation

泄露秘密 提交于 2019-12-08 21:44:08
问题 I have a Cassandra cluster with 4 table and data inside. I want to make request with aggregation function ( sum, max ...) but I've read here that it's impossible : http://www.datastax.com/documentation/cql/3.1/cql/cql_reference/cql_function_r.html Is there a way to make sum , average, group by, without buying the enterprise version, can I use presto , or other solutions? Thanks 回答1: Aggregate functions will be available as part of Cassandra 3.0 https://issues.apache.org/jira/browse/CASSANDRA

In SQL, how do I get all rows where a column's value is the lowest in the table?

泪湿孤枕 提交于 2019-12-08 20:30:41
问题 I am a newbie to SQL, I am using this query to look for the minimum value in the field weight of my table. SELECT product_id, MIN(weight) FROM table WHERE 1; It does show one field with the min value, but only one? But I have many products with the same minimum weight. Is there a way I could specify that I need to show all other products? 回答1: select * from table where weight = (select MIN(weight) from table) 回答2: This may be what you're asking for: SELECT product_id FROM table WHERE weight =

SQL random aggregate

一曲冷凌霜 提交于 2019-12-08 15:30:54
问题 Say I have a simple table with 3 fields: 'place', 'user' and 'bytes'. Let's say, that under some filter, I want to group by 'place', and for each 'place', to sum all the bytes for that place, and randomly select a user for that place (uniformly from all the users that fit the 'where' filter and the relevant 'place'). If there was a "select randomly from" aggregate function, I would do: SELECT place, SUM(bytes), SELECT_AT_RANDOM(user) WHERE .... GROUP BY place; ...but I couldn't find such an

Aggregating MySQL data on hourly basis from minute-wise raw data

别说谁变了你拦得住时间么 提交于 2019-12-08 09:41:39
问题 I have a table, table_1, which has data for EACH minute and looks like: +---------------------+---------+ | date_time | value | +---------------------+---------+ | 2015-06-05 18:00:00 | 222.663 | | 2015-06-05 18:01:00 | 222.749 | | 2015-06-05 18:02:00 | 222.957 | | 2015-06-05 18:03:00 | 223.063 | | 2015-06-05 18:04:00 | 223.117 | +---------------------+---------+ I wish to fetch hourly min and max values. Something like: +---------------------+---------+ | date_time | value | +---------------

Grouping Events in Postgres

元气小坏坏 提交于 2019-12-08 06:05:10
问题 I've got an events table that is generated by user activity on a site: timestamp | name 7:00 AM | ... 7:01 AM | ... 7:02 AM | ... 7:30 AM | ... 7:31 AM | ... 7:32 AM | ... 8:01 AM | ... 8:03 AM | ... 8:05 AM | ... 8:08 AM | ... 8:09 AM | ... I'd like to aggregate over the events to provide a view of when a user is active. I'm defining active to mean the period in which an event is within +/- 2 minutes. For the above that'd mean: from | till 7:00 AM | 7:02 AM 7:30 AM | 7:32 AM 8:01 AM | 8:05