group-by

Count totals by year and month

只谈情不闲聊 提交于 2019-12-18 11:09:56
问题 I have a table that looks like this: id,created,action 1,'2011-01-01 04:28:21','signup' 2,'2011-01-05 04:28:21','signup' 3,'2011-02-02 04:28:21','signup' How do I select and group these so the output is: year,month,total 2011,1,2 2011,2,1 回答1: Try this: SELECT DATE_FORMAT(created, '%Y') as 'year', DATE_FORMAT(created, '%m') as 'month', COUNT(id) as 'total' FROM table_name GROUP BY DATE_FORMAT(created, '%Y%m') 回答2: SELECT YEAR(created) as year_val, MONTH(created) as month_val ,COUNT(*) as

How do I order a Group result, in Linq?

泄露秘密 提交于 2019-12-18 10:33:31
问题 I have the following linq query, which works fine. I'm not sure how i order the group'd result. from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g select new { UserId = g.Key, Score = g.Sum(x => x.Score) } the results are currently ordered by UserId ascending. I'm after Score descending. thanks :) 回答1: Just add the orderby clause ;-) from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g let score = g.Sum(x => x.Score)

C# List - Group By - Without Linq

我与影子孤独终老i 提交于 2019-12-18 09:31:45
问题 I have an object: IObject { string Account, decimal Amount } How do I group by Account and Sum the Amount, returning a List without Linq. 2.0 Framework ... that is why no Linq. Here is what I have: ListofObjects = List<IObject>; foreach (var object in objects) { var objectToAdd = new Object(object); var oa = ListofObjects.Find(x => x.Account == objectToAdd.Account); if (oa == null) { ListofObjects.Add(objectToAdd); } else { ListofObjects.Remove(oa); oa.Amount = objectToAdd.Amount;

Find duplicate xelements

孤者浪人 提交于 2019-12-18 09:27:33
问题 I have the below XML <Automobiles> <Cars> <YearofMfr>2010</YearofMfr> <Mileage>12</Mileage> <MeterReading>1500</MeterReading> <Color>Red</Color> <Condition>Excellent</Condition> </Cars> <Cars> <YearofMfr>2010</YearofMfr> <Mileage>12</Mileage> <MeterReading>1500</MeterReading> <Color>Red</Color> <Condition>Excellent</Condition> </Cars> <Cars> <YearofMfr>2008</YearofMfr> <Mileage>11</Mileage> <MeterReading>20000</MeterReading> <Color>Pearl White</Color> <Condition>Good</Condition> </Cars> <

GROUP or DISTINCT after JOIN returns duplicates

守給你的承諾、 提交于 2019-12-18 09:17:10
问题 I have two tables, products and meta . They are in relation 1:N where each product row has at least one meta row via foreign key. (viz. SQLfiddle: http://sqlfiddle.com/#!15/c8f34/1) I need to join these two tables but i need to filter only unique products. When I try this query, everything is ok (4 rows returned): SELECT DISTINCT(product_id) FROM meta JOIN products ON products.id = meta.product_id but when I try to select all columns the DISTINCT rule no longer applies to results, as 8 rows

GROUP or DISTINCT after JOIN returns duplicates

橙三吉。 提交于 2019-12-18 09:16:12
问题 I have two tables, products and meta . They are in relation 1:N where each product row has at least one meta row via foreign key. (viz. SQLfiddle: http://sqlfiddle.com/#!15/c8f34/1) I need to join these two tables but i need to filter only unique products. When I try this query, everything is ok (4 rows returned): SELECT DISTINCT(product_id) FROM meta JOIN products ON products.id = meta.product_id but when I try to select all columns the DISTINCT rule no longer applies to results, as 8 rows

Postgresql group month wise with missing values

眉间皱痕 提交于 2019-12-18 09:06:38
问题 first an example of my table: id_object;time;value;status 1;2014-05-22 09:30:00;1234;1 1;2014-05-22 09:31:00;2341;2 1;2014-05-22 09:32:00;1234;1 ... 1;2014-06-01 00:00:00;4321;1 ... Now i need count all rows with status=1 and id_object=1 monthwise for example. this is my query: SELECT COUNT(*) FROM my_table WHERE id_object=1 AND status=1 AND extract(YEAR FROM time)=2014 GROUP BY extract(MONTH FROM time) The result for this example is: 2 1 2 for may and 1 for june but i need a output with all

Group-wise Maximum of a Certain Column

流过昼夜 提交于 2019-12-18 07:00:14
问题 I've got the table: SELECT * FROM shop; +---------+--------+------ | article | dealer | price +---------+--------+------ | 0001 | A | 3.45 | 0001 | B | 3.99 | 0002 | A | 10.99 | 0003 | B | 1.45 | 0003 | C | 1.69 | 0003 | D | 1.25 | 0004 | D | 19.95 +---------+--------+------ 7 rows in set (0.20 sec) And I want to get - for each article - the dealer or dealers with the most expensive price. Could anyone tell me why this doesn’t work? SELECT article, dealer, MAX(price) FROM shop GROUP BY

Pandas: using multiple functions in a group by

霸气de小男生 提交于 2019-12-18 06:59:57
问题 My data has ages, and also payments per month. I'm trying to aggregate summing the payments, but without summing the ages (averaging would work). Is it possible to use different functions for different columns? 回答1: You can pass a dictionary to agg with column names as keys and the functions you want as values. import pandas as pd import numpy as np # Create some randomised data N = 20 date_range = pd.date_range('01/01/2015', periods=N, freq='W') df = pd.DataFrame({'ages':np.arange(N),

JOIN, GROUP BY, ORDER BY

廉价感情. 提交于 2019-12-18 06:56:39
问题 The problem I first had with the following query was that the group by clause was performed before the order by : The saved.recipe_id column is an integer generated by UNIX_TIMESTAMP() SELECT saved.recipe_id, saved.`date`, user.user_id FROM saved JOIN user ON user.id = saved.user_id GROUP BY saved.recipe_id ORDER BY saved.`date` DESC So I tried all sorts of different possible solution with sub queries and other bs. In the end I ended up with trying out some different sub queries in the join