group-by

Select all rows in a group where within the group, one column has one specific value, and another column has another specific value on the next row

一曲冷凌霜 提交于 2019-12-25 18:03:29
问题 I am new to SQL, so I’m not sure which approach is best for this kind of task: I have a table where groups of rows all relate to the same item, whose name appears in the first column. Other details appear in the other columns. I am trying to retrieve all rows for every group based on having the same value in the first column, where every time a certain value appears in one column, another value appears in a different column in the following row. | Fruit | Value1| Value2| ---------------------

SQL: group by from other table and invert result

丶灬走出姿态 提交于 2019-12-25 17:10:29
问题 I have some problem with my SQL query I have table message and table recipient message is ID author date -------------------- 0 1 2013-07-08 05:38:47 1 1 2013-07-13 05:38:47 2 1 2013-07-15 05:38:47 3 1 2013-07-15 05:38:47 4 2 2013-07-17 05:38:47 5 1 2013-07-28 05:38:47 recipient is ID m_id recipient -------------------- 0 0 2 1 1 2 2 2 3 3 3 2 4 4 1 5 5 2 I need return rows from table message with group by recipient column from table recipient with last date in message table I'll try this

Grouping in Pandas

假装没事ソ 提交于 2019-12-25 14:03:54
问题 I want to group data in a dataframe I have oo the Column "Count" and by another column "State". I would like to output a list of list, each sub set list would just be the count for each state. example output: [[120,200], [40, 20, 40], ...] 120 and 200 would be counts for let's say the State California I tried the following: df_new = df[['State']].groupby(['Count']).to_list() I get a keyerror: 'count' Traceback: Traceback (most recent call last): File "C:\Users\Michael\workspace

-find top x by count from MySQL in Python?

隐身守侯 提交于 2019-12-25 14:01:25
问题 I have a csv file like this: nohaelprince@uwaterloo.ca, 01-05-2014 nohaelprince@uwaterloo.ca, 01-05-2014 nohaelprince@uwaterloo.ca, 01-05-2014 nohaelprince@gmail.com, 01-05-2014 I am reading the above csv file and extracting domain name and also the count of emails address by domain name and date as well. All these things I need to insert into MySQL table called domains which I am able to do it successfully. Problem Statement:- Now I need to use the same table to report the top 50 domains by

How to pivot a dataframe

爷,独闯天下 提交于 2019-12-25 11:45:58
问题 What is pivot? How do I pivot? Is this a pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables. Even if they don't know that they are asking about pivot tables, they usually are. It is virtually impossible to write a canonical question and answer that encompasses all aspects of pivoting.... ... But I'm going to give it a go. The problem with existing questions and answers is that often the question is focused on a nuance that the OP has trouble

creating multiple custom aggregation functions

对着背影说爱祢 提交于 2019-12-25 09:49:29
问题 I have this table named Account. A simplified view of this table is as follows: acct_num | ssn | branch | open_date |close_date | has_product1 | has_product2 ---------------------------------------------------------------------------------------- 0123456 | 123456789 | 01 | 01/01/2000 | NULL | 1 | 0 0123457 | 123456789 | 02 | 03/05/2004 | NULL | 0 | 1 1234405 | 322145678 | 04 | 04/16/2016 | 05/01/2016 | 1 | 1 ... Notice how the ssn 123456789 has 2 accounts. I need to create a new data set that

Change number column

◇◆丶佛笑我妖孽 提交于 2019-12-25 09:47:42
问题 I have NAME and PAY, but I need CHANGEGROUP in this example: NAME PAY DATE CHANGEGROUP Sally 12 10/01/2011 1 Sally 12 10/01/2011 1 Sally 12 11/02/2011 1 Sally 12 11/02/2011 1 Sally 12 12/01/2012 1 Sally 13 04/23/2013 2 Sally 12 04/24/2013 3 Sally 10 05/01/2013 4 Sally 10 10/01/2014 4 I tried RANK() and DENSE_RANK() , but they group according to the value - because pay goes down, it messes up my grouping. I saw this but it's not compatible with this older version of SQL 2005 回答1: This is a

Syntax error or access violation: 1055 Expression #17 in group by

孤街浪徒 提交于 2019-12-25 08:58:50
问题 I tried a query using group in laravel 5.3.I caught SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #17 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'testtravel.country.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `travel_request`.*, `travel_request`.`id` as `travel_id`, `department`.`name` as `dept_name`, `users`.`firstname` as `approver_name`,

How to fill 0 and NaN for a dataframe after groupby it in python?

我与影子孤独终老i 提交于 2019-12-25 08:56:23
问题 I have a dataframe looks like this after I groupby them. gb = new_user_log.groupby(['msno', 'date', 'num_25', 'num_50', 'num_75', 'num_985', 'num_100', 'num_unq', 'total_secs', 'days']).days.count() Part of the dataframe shows here: msno date num_25 num_50 num_75 num_985 num_100 num_unq total_secs days ++orpnUqSevh2M5A97pRRiONA58g5m9DwaNrhD44HY0= 2016-08-14 78 13 3 3 77 84 18987.862 2 1 +0krxpTkQT7hciN95OEp7i2lyKvbXft887VNQGF6xN4= 2016-12-22 27 23 5 2 11 65 5946.577 35 1 98 1 +1eAmfPiXsMG0J+U

groupby with overlapping intervals timeseries

牧云@^-^@ 提交于 2019-12-25 08:47:00
问题 I have a time series in python pandas dataframe object and I want to create a group based on index but I want overlapping groups i.e groups are not distinct. The header_sec is the index column. Each groups consists of a 2 second window. Input dataFrame header_sec 1 17004 days 22:17:13 2 17004 days 22:17:13 3 17004 days 22:17:13 4 17004 days 22:17:13 5 17004 days 22:17:14 6 17004 days 22:17:14 7 17004 days 22:17:14 8 17004 days 22:17:14 9 17004 days 22:17:15 10 17004 days 22:17:15 11 17004