group-by

Python - Grouping by multiple columns and getting max or sum

浪子不回头ぞ 提交于 2019-12-13 01:43:46
问题 I am interested in getting max value of the Product price. Here is the input data.Download Sales Data I want to group by State,Country How do I go about grouping these two columns get the max value of Price. import csv import locale from itertools import groupby locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) total_price = 0 max_price = 0 reader = csv.DictReader(open('/Users/myuser/Downloads/SalesData.csv', 'rU'), dialect='excel') groups = groupby(reader, lambda d: d['State']) result = [max

Informix Group by Alias

旧巷老猫 提交于 2019-12-13 01:30:20
问题 What am I missing according to this query: SELECT mymonth, Header1 FROM ( SELECT month(startdatetime) as mymonth, (CASE WHEN MyTable.somecolumn =2 THEN count(somecolumn2) END) as Header1 FROM MyTable WHERE year(startdatetime)=2013 group by startdatetime ) x GROUP BY Header1 I've red somewhere that Informix is not supporting grouping by alias but when trying to set GROUP BY 2, there is error too Column Header1 must be in a Group by clause 回答1: Your SQL is very unusual. The Informix server is

Distinct a list of records based on two column using groupby

你离开我真会死。 提交于 2019-12-13 01:27:39
问题 I am trying to fetch a list using EF6 .I have a class like this: public class Province { public string province { set; get; } public string provinceCode { set; get; } } Zone class namespace InnoviceDomainClass { using System; using System.Collections.Generic; public partial class Zone { public string CityCode { get; set; } public string City { get; set; } public string Province { get; set; } public Nullable<int> ProvinceCode { get; set; } } } I fetch my data using this : List<Zone> ListZone =

BigQuery: Response too large to return when using GROUP EACH BY

我的未来我决定 提交于 2019-12-13 01:27:17
问题 The table I am working with has 3 fields: userId, timestamp, version I am running the following query: select userid, MAX(version) as current_version FROM my_table GROUP EACH BY userId; The response I get is: "errors": [ { "reason": "responseTooLarge", "message": "Response too large to return." } The size of the table is 644MB and it has 12,279,432 rows. I thought GROUP EACH BY does not have the result size restrictions because it is distributed across multiple nodes. Anyway, What can I do

Mysql get average and sum of columns and group by year & month

谁说胖子不能爱 提交于 2019-12-13 00:53:53
问题 I have a query wherein I need to return the average of the price and the sum of the qty and group the results by year and month. This is the start of my query, just don't know how to get the results that I need. SELECT asin, price, qtyTotal, qtyReserved, qtyWarehouse, qtyFulfillable, qtyUnsellable, perUnitVolume, YEAR(reportDate), MONTH(reportDate), DAY(reportDate) FROM Table WHERE name = 'XXXXXXX' ORDER BY reportDate ASC id | name | price | qty | unitVol | year | month | day | reportDate ---

How to structure Google Datastore (App Engine) web traffic model?

瘦欲@ 提交于 2019-12-13 00:51:13
问题 Simple Task: keep track of web traffic (hits) so that I can graph the number of hits per day for the last 30 days. Current Datastore Model (2 fields): 1) Website ID 2) Timestamp of Hit Problem: I'm using Google App Engine's datastore and don't have the ability to do a group-by or count. Can anyone offer a simple way to structure my Google Datastore database to achieve this task? By returning all of the hits and then grouping them in my code seems like a performance hog. Any ideas? 回答1: I

GROUP BY WITH HAVING ( DISTINCT ) : PHP , MYSQL

家住魔仙堡 提交于 2019-12-13 00:50:07
问题 id | mid | pid | owgh | nwgh | 1 3 12 1.5 0.6 2 3 12 1.5 0.3 3 3 14 0.6 0.4 4 3 15 1.2 1.1 5 4 16 1.5 1.0 6 4 17 2.4 1.2 7 3 19 3.0 1.4 From Above i want total count of mid AND SUM of nwgh with its resp. id ex: mid=3 or mid=4 but with DISTINCT pid but please note sum of nwgh should not be DISTINCT Hence my result will be as below : mid | countmid | totalnwgh 3 4 (DISTINCT value) 3.8 (no DISTINCT it take both value of pid =12) 4 2 2.2 in above result mid = 3 have count 4 beause pid = 12 is

SQL query to find the sum of all rows and sum of portions of rows within a group by clause

早过忘川 提交于 2019-12-13 00:46:46
问题 I am trying to build a query for a SQL Server 2008 database. The query would group the SID and Date column and sum the Profit column. The query also needs to find the sum of Profit for all rows having the same units toward the end of a day. in this case, the units value of 1 switched from -1 at 12:54PM to 1 and remained the same until the last record for that day which is at 1:15PM. SID Date Profit Units RowID ------------------------------------------------------- 1 7/26/10 1:15 PM -25 1 1 1

DataFrame: N largest indexes values (from level=1) to n columns

和自甴很熟 提交于 2019-12-13 00:28:59
问题 I am trying to convert such a df: df = pd.DataFrame({'A': ['A1', 'A1', 'A1', 'A1', 'A1', 'A1', 'A2', 'A2', 'A2', 'A2', 'A2', 'A2', 'A2'], 'B': ['B1', 'B1', 'B2', 'B2', 'B3', 'B3', 'B4', 'B5', 'B6', 'B7', 'B7', 'B8', 'B8']}) by taking n (here 2) largest indexes (by count of B) to: My way of doing it: df = df.groupby(['A', 'B'])['A'].count() df = df.groupby(level=0).nlargest(2).reset_index(level=0, drop=True) what gives me (which is close to what I need): Now, the only methods I know to

MySQL counting multiple columns in group by clause

僤鯓⒐⒋嵵緔 提交于 2019-12-13 00:28:29
问题 I'd like to count each distinct group being used in a group by clause. I am using this query right now: SELECT language, group, count(*) AS frequency FROM bfs where firstname <> '' GROUP BY language, group Which gives me result like this: 'Language' 'Group' 'Frequency' 'ARABIC' 'LEBANESE' 1080 'ARABIC' 'MUSLIM' 40963 'ARABIC' 'MUSLIM MIDDLE EAST' 349 'ARABIC' 'MUSLIM NORTH AFRICAN' 549 What I would like is: instead of total frequency of each combination of language and group column, I'd like