group-by

How do I use an SQL GROUP BY and SUM functions in a IOS CORE-DATA request in SWIFT?

我怕爱的太早我们不能终老 提交于 2019-12-17 19:37:00
问题 I have a table (Transactions) which holds records containing the Account_name and an Amount for the transaction. I would like to calculate the total for all transactions per account which start with 'Private' and for which the transaction amount is > 1000. I would like to get the accounts sorted by name, in a descending order. So the SQL request would be something like this : SELECT Account_name, SUM(Amount) FROM Transactions WHERE Account_name like 'Private%' and Amount > 1000 GROUP BY

Counting number of records hour by hour between two dates in oracle

梦想与她 提交于 2019-12-17 19:07:42
问题 I need a SINGLE query that does this sequence in oracle. select count(*) from table1 where request_time < timestamp'2012-05-19 12:00:00' and (end_time > timestamp'2012-05-19 12:00:00' or end_time=null); select count(*) from table1 where request_time < timestamp'2012-05-19 13:00:00' and (end_time > timestamp'2012-05-19 13:00:00' or end_time=null); select count(*) from table1 where request_time < timestamp'2012-05-19 14:00:00' and (end_time > timestamp'2012-05-19 14:00:00' or end_time=null);

Group By Eloquent ORM

一世执手 提交于 2019-12-17 19:07:19
问题 I was searching for making a GROUP BY name Eloquent ORM docs but I haven't find anything, neither on google. Does anyone know if it's possible ? or should I use query builder ? 回答1: Eloquent uses the query builder internally, so you can do: $users = User::orderBy('name', 'desc') ->groupBy('count') ->having('count', '>', 100) ->get(); 回答2: Laravel 5 This is working for me (i use laravel 5.6 ). $collection = MyModel::all()->groupBy('column'); If you want to convert the collection to plain php

python return lists of continuous integers from list

半世苍凉 提交于 2019-12-17 19:00:01
问题 I have a list of integers, and I want to generate a list containing a list of all the continuous integers. #I have: full_list = [0,1,2,3,10,11,12,59] #I want: continuous_integers = [[0,1,2,3], [10,11,12], [59]] I have the following which works, but seems like a poor way to do it: sub_list = [] continuous_list = [] for x in full_list: if sub_list == []: sub_list.append(x) elif x-1 in sub_list: sub_list.append(x) else: continuous_list.append(sub_list) sub_list = [x] continuous_list.append(sub

hive Expression Not In Group By Key

橙三吉。 提交于 2019-12-17 18:52:13
问题 I create a table in HIVE. It has the following columns: id bigint, rank bigint, date string I want to get avg(rank) per month. I can use this command. It works. select a.lens_id, avg(a.rank) from tableA a group by a.lens_id, year(a.date_saved), month(a.date_saved); However, I also want to get date information. I use this command: select a.lens_id, avg(a.rank), a.date_saved from lensrank_archive a group by a.lens_id, year(a.date_saved), month(a.date_saved); It complains: Expression Not In

SQL - Displaying entries that are the max of a count?

。_饼干妹妹 提交于 2019-12-17 18:39:54
问题 CREATE TABLE doctor( patient CHAR(13), docname CHAR(30) ); Say I had a table like this, then how would I display the names of the doctors that have the most patients? Like if the most was three and two doctors had three patients then I would display both of their names. This would get the max patients: SELECT MAX(count) FROM (SELECT COUNT(docname) FROM doctor GROUP BY docname) a; This is all the doctors and how many patients they have: SELECT docname, COUNT(docname) FROM doctor GROUP BY name;

MySQL Group By Hours

扶醉桌前 提交于 2019-12-17 18:38:27
问题 I'm trying to get a report from my history table by hourly usage. history table is; CREATE TABLE IF NOT EXISTS `history` ( `history_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) unsigned NOT NULL DEFAULT '0', `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`history_id`), KEY `user_id` (`user_id`), KEY `created` (`created`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; I want to group by HOUR and COUNT all records in a

Sample each group after pandas groupby

早过忘川 提交于 2019-12-17 18:34:08
问题 I know this must have been answered some where but I just could not find it. Problem : Sample each group after groupby operation. import pandas as pd df = pd.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [1,1,1,0,0,0,0]}) grouped = df.groupby('b') # now sample from each group, e.g., I want 30% of each group 回答1: Apply a lambda and call sample with param frac : In [2]: df = pd.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [1,1,1,0,0,0,0]}) ​ grouped = df.groupby('b') grouped.apply(lambda x: x.sample(frac=0.3)

Merge pandas DataFrames based on irregular time intervals

[亡魂溺海] 提交于 2019-12-17 18:10:57
问题 I'm wondering how I can speed up a merge of two dataframes. One of the dataframes has time stamped data points ( value col). import pandas as pd import numpy as np data = pd.DataFrame({'time':np.sort(np.random.uniform(0,100,size=50)), 'value':np.random.uniform(-1,1,size=50)}) The other has time interval information ( start_time , end_time , and associated interval_id ). intervals = pd.DataFrame({'interval_id':np.arange(9), 'start_time':np.random.uniform(0,5,size=9) + np.arange(0,90,10), 'end

Python (Pandas) Add subtotal on each lvl of multiindex dataframe

雨燕双飞 提交于 2019-12-17 17:52:43
问题 Assuming I have the following dataframe: a b c Sce1 Sce2 Sce3 Sce4 Sce5 Sc6 Animal Ground Dog 0.0 0.9 0.5 0.0 0.3 0.4 Animal Ground Cat 0.6 0.5 0.3 0.5 1.0 0.2 Animal Air Eagle 1.0 0.1 0.1 0.6 0.9 0.1 Animal Air Owl 0.3 0.1 0.5 0.3 0.5 0.9 Object Metal Car 0.3 0.3 0.8 0.6 0.5 0.6 Object Metal Bike 0.5 0.1 0.4 0.7 0.4 0.2 Object Wood Chair 0.9 0.6 0.1 0.9 0.2 0.8 Object Wood Table 0.9 0.6 0.6 0.1 0.9 0.7 I want to create a MultiIndex, which will contain the sum of each lvl. The output will