group-by

Linq order by, group by and order by each group?

蹲街弑〆低调 提交于 2019-12-17 05:47:04
问题 I have an object that looks something like this: public class Student { public string Name { get; set; } public int Grade { get; set; } } I would like to create the following query: group grades by student name, order each student group by grades, and order groups by max grade in each group. So it will look like this: A 100 A 80 B 80 B 50 B 40 C 70 C 30 I created the following query: StudentsGrades.GroupBy(student => student.Name) .OrderBy(studentGradesGroup => studentGradesGroup.Max(student

Find last row in group by query-SQL Server

梦想的初衷 提交于 2019-12-17 05:15:43
问题 I have table in SQL Server. I want to find last row in each group. I tried with the following query, but it does not return exact result. ID column is PK and other columns are set to NOT NULL. select ID, Name FROM (select ID, Name, max(ID) over (partition by Name) as MAX_ID from Customer) x where ID= MAX_ID To be more clear. I have 2 queries.First: ALTER PROCEDURE [dbo].[Ramiz_Musterija_RowNum] @Datum DATE, @BrojKamiona INT AS SET NOCOUNT ON SELECT Ime,MusterijaID,RowNum=ROW_NUMBER() OVER

LINQ: combining join and group by

瘦欲@ 提交于 2019-12-17 04:59:33
问题 I have a query that combines a join and a group, but I have a problem. The query is like: var result = from p in Products join bp in BaseProducts on p.BaseProductId equals bp.Id group p by p.SomeId into pg select new ProductPriceMinMax { SomeId = pg.FirstOrDefault().SomeId, CountryCode = pg.FirstOrDefault().CountryCode, MinPrice = pg.Min(m => m.Price), MaxPrice = pg.Max(m => m.Price), BaseProductName = bp.Name <------ can't use bp. }; As you see, it joins the Products table with the

MySQL Order before Group by

倾然丶 夕夏残阳落幕 提交于 2019-12-17 04:28:19
问题 I need to find the latest post for each author and then group the results so I only a single latest post for each author. SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status='publish' AND wp_posts.post_type='post' GROUP BY wp_posts.post_author ORDER BY wp_posts.post_date DESC This is correctly grouping the output so I only get one post per author, but it is ordering the results after they have been grouped and not before they have been selected. 回答1: select wp_posts.* from wp_posts

MySQL Order before Group by

回眸只為那壹抹淺笑 提交于 2019-12-17 04:28:17
问题 I need to find the latest post for each author and then group the results so I only a single latest post for each author. SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status='publish' AND wp_posts.post_type='post' GROUP BY wp_posts.post_author ORDER BY wp_posts.post_date DESC This is correctly grouping the output so I only get one post per author, but it is ordering the results after they have been grouped and not before they have been selected. 回答1: select wp_posts.* from wp_posts

Pandas groupby diff

跟風遠走 提交于 2019-12-17 04:07:23
问题 So my dataframe looks like this: from pandas.compat import StringIO d = StringIO(''' date,site,country,score 2018-01-01,google,us,100 2018-01-01,google,ch,50 2018-01-02,google,us,70 2018-01-03,google,us,60 2018-01-02,google,ch,10 2018-01-01,fb,us,50 2018-01-02,fb,us,55 2018-01-03,fb,us,100 2018-01-01,fb,es,100 2018-01-02,fb,gb,100 ''') df = pd.read_csv(d, sep=",") Each site has a different score depending on the country. I'm trying to find the 1/3/5 day difference of scores for each site

Group by & count function in sqlalchemy

大兔子大兔子 提交于 2019-12-17 03:26:23
问题 I want a "group by and count" command in sqlalchemy. How can I do this? 回答1: The documentation on counting says that for group_by queries it is better to use func.count() : from sqlalchemy import func session.query(Table.column, func.count(Table.column)).group_by(Table.column).all() 回答2: You can also count on multiple groups and their intersection: self.session.query(func.count(Table.column1),Table.column1, Table.column2).group_by(Table.column1, Table.column2).all() The query above will

SELECT / GROUP BY - segments of time (10 seconds, 30 seconds, etc)

一个人想着一个人 提交于 2019-12-17 03:25:58
问题 I have a table (MySQL) that captures samples every n seconds. The table has many columns, but all that matters for this is two: a time stamp (of type TIMESTAMP) and a count (of type INT). What I would like to do, is get sums and averages of the count column over a range of times. For instance, I have samples every 2 seconds recorded, but I would like the sum of the count column for all the samples in a 10 second or 30 second window for all samples. Here's an example of the data: +------------

Group by & count function in sqlalchemy

喜你入骨 提交于 2019-12-17 03:25:27
问题 I want a "group by and count" command in sqlalchemy. How can I do this? 回答1: The documentation on counting says that for group_by queries it is better to use func.count() : from sqlalchemy import func session.query(Table.column, func.count(Table.column)).group_by(Table.column).all() 回答2: You can also count on multiple groups and their intersection: self.session.query(func.count(Table.column1),Table.column1, Table.column2).group_by(Table.column1, Table.column2).all() The query above will

Ms Access Query: Concatenating Rows through a query

梦想与她 提交于 2019-12-17 02:26:16
问题 Suppose I have table in Ms Access with following information: ColumnA ColumnB 1 abc 1 pqr 1 xyz 2 efg 2 hij 3 asd My question is, how can I concatenate the values in the second column to a row value based on the first column. The query results that I want is as follows: ColumnA ColumnB 1 abc, pqr, xyz 2 efg, hij 3 asd I want to achieve this through a query. Can someone help me attain this? 回答1: You need a function to do the concatenation. Microsoft Access condense multiple lines in a table