group-by

MySQL Nested Select Query?

烂漫一生 提交于 2019-12-17 23:23:29
问题 Ok, so I have the following query: SELECT MIN(`date`), `player_name` FROM `player_playtime` GROUP BY `player_name` I then need to use this result inside the following query: SELECT DATE(`date`) , COUNT(DISTINCT `player_name`) FROM `player_playtime /*Use previous query result here*/` GROUP BY DATE( `date`) DESC LIMIT 60 How would I go about doing this? 回答1: You just need to write the first query as a subquery (derived table), inside parentheses, pick an alias for it ( t below) and alias the

elasticsearch group-by multiple fields

会有一股神秘感。 提交于 2019-12-17 22:54:36
问题 i am Looking for the best way to group data in elasticsearch. Elasticsearch doesnt support something like 'group by' in sql. Lets say i have 1k categories and millions of products. what do you think is the best way to render a complete category tree? of couse jou need some metadata (icon, link-target, seo-titles,...) and custom sorting for the categories. Using Aggregations: Example: https://found.no/play/gist/8124563 looks useable if you have to group by one field, and need some extra fields

Count and Sort with Pandas

假装没事ソ 提交于 2019-12-17 22:38:40
问题 I have a dataframe for values form a file by which I have grouped by two columns, which return a count of the aggregation. Now I want to sort by the max count value, however I get the following error: KeyError: 'count' Looks the group by agg count column is some sort of index so not sure how to do this, I'm a beginner to Python and Panda. Here's the actual code, please let me know if you need more detail: def answer_five(): df = census_df#.set_index(['STNAME']) df = df[df['SUMLEV'] == 50] df

Pandas Correlation Groupby

只谈情不闲聊 提交于 2019-12-17 22:36:49
问题 Assuming I have a dataframe similar to the below, how would I get the correlation between 2 specific columns and then group by the 'ID' column? I believe the Pandas 'corr' method finds the correlation between all columns. If possible I would also like to know how I could find the 'groupby' correlation using the .agg function (i.e. np.correlate). What I have: ID Val1 Val2 OtherData OtherData A 5 4 x x A 4 5 x x A 6 6 x x B 4 1 x x B 8 2 x x B 7 9 x x C 4 8 x x C 5 5 x x C 2 1 x x What I need:

Distinct pair of values SQL

让人想犯罪 __ 提交于 2019-12-17 22:17:22
问题 Consider create table pairs ( number a, number b ) Where the data is 1,1 1,1 1,1 2,4 2,4 3,2 3,2 5,1 Etc. What query gives me the distinct values the number column b has So I can see 1,1 5,1 2,4 3,2 only I've tried select distinct ( a ) , b from pairs group by b but gives me "not a group by expression" 回答1: What you mean is either SELECT DISTINCT a, b FROM pairs; or SELECT a, b FROM pairs GROUP BY a, b; 回答2: If you want to want to treat 1,2 and 2,1 as the same pair, then this will give you

A.* isn't in GROUP BY with left join on laravel query builder

心不动则不痛 提交于 2019-12-17 21:33:09
问题 $search_alls= DB::table('a16s as A') ->select('A.id') // ->select('A.*') ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 ELSE 0 END) as Yshow')) ->leftjoin('a16s_likes as B', function($join) { $join->on('A.id', '=', 'B.p_id'); }) ->groupBy('A.id') ->get(); when I use above select('A.id') is work well. But when I use select('A.*') to select all A cloumn I got the error SQLSTATE[42000]: Syntax error or access violation: 1055 'employee.A.name' isn't in GROUP BY PS:employee is my DB name The

Group table into 15 minute intervals

左心房为你撑大大i 提交于 2019-12-17 20:51:29
问题 T-SQL, SQL Server 2008 and up Given a sample table of StatusSetDateTime | UserID | Status | StatusEndDateTime | StatusDuration(in seconds) ============================================================================ 2012-01-01 12:00:00 | myID | Available | 2012-01-01 13:00:00 | 3600 I need to break that down into a view that uses 15 minute intervals for example: IntervalStart | UserID | Status | Duration =========================================== 2012-01-01 12:00:00 | myID | Available | 900

SQL Server: Only last entry in GROUP BY

爷,独闯天下 提交于 2019-12-17 20:09:59
问题 I have the following table in MSSQL2005 id | business_key | result 1 | 1 | 0 2 | 1 | 1 3 | 2 | 1 4 | 3 | 1 5 | 4 | 1 6 | 4 | 0 And now i want to group based on the business_key returning the complete entry with the highest id. So my expected result is: business_key | result 1 | 1 2 | 1 3 | 1 4 | 0 I bet that there is a way to achieve that, i just can't see it at the moment. 回答1: An alternative solution, which may give you better performance (test both ways and check the execution plans):

R: Interpolation of NAs by group

北城以北 提交于 2019-12-17 19:53:12
问题 I would like to perform a linear interpolation in a variable of a data frame which takes into account the: 1) time difference between the two points, 2) the moment when the data was taken and 3) the individual taken for measure the variable. For example in the next dataframe: df <- data.frame(time=c(1,2,3,4,5,6,7,1,2,3), Individuals=c(1,1,1,1,1,1,1,2,2,2), Value=c(1, 2, 3, NA, 5, NA, 7, 5, NA, 7)) df I would like to obtain: result <- data.frame(time=c(1,2,3,4,5,6,7,1,2,3), Individuals=c(1,1,1

Mysql DateTime group by 15 mins

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 19:47:08
问题 I have a table that looks like this CREATE TABLE `time_table` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `creationDate` DATETIME NOT NULL, PRIMARY KEY (`id`) ) I basically store the creation time of certain records in the table. I know if I want to get a count of the records that were created in 15 mins interval I will use something like this SELECT FLOOR(UNIX_TIMESTAMP(creationDate)/900) AS t, COUNT(*) FROM time_table GROUP BY t That gives me something like this t COUNT(*) 1434187 1 1434188 3