group-by

MySQL Query - recent entries per group

佐手、 提交于 2019-12-12 16:55:44
问题 I'm trying to select the most recent entries per group in a table. Say I have a table "blog_posts" which has a column for "id" (all unique, auto incremented), "post_cat" which can be values 'category1' or 'category2' or 'category3', and a "publish_status" column which can be values 'online' or 'offline'. How can I select the most recent entries for each category? I have the following right now, but it almost feels like it's selecting randomly: select * FROM `blog_posts` WHERE (publish_status

SQL: Group By with Case Statement for multiple fields

放肆的年华 提交于 2019-12-12 15:25:45
问题 I am trying to write a GROUP BY clause with a CASE statement so I can conditionally GROUP BY according to the value of the parameter in my query. Here is my query that I am using (significantly shortened) and I keep getting "RVPname is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. And DVPname is invalid and AEname is invalid. SELECT DVPname, RVPname, AEname, Dealer, Product, Distribution, CASE WHEN @LEVEL = 'DVP' THEN DVPname

Applying a jinja filter after using groupby filter

陌路散爱 提交于 2019-12-12 15:23:01
问题 I have a list of dictionaries that I want to group by a certain attribute and then sum by another. For a variable 'foo' this would be something like: foo | groupby('a') | sum(attribute='b') This clearly won't work because after the groupby , I have a list of tuples. Is there any way to unpack the tuple and then repack it, so that i can maintain the work done by groupby , but work on the second tuple value? 回答1: You can use the map() filter to apply a sum to each group, provided you extract

How to further filter GROUP BY record in mysql?

瘦欲@ 提交于 2019-12-12 14:42:22
问题 Please check the images i want complete ROW of query-id "43"(and with another nic_id max query_id row) but it give me only max query_id, I tried different queries for-example: SELECT `Query_id`, `nic_id`, `date`, `subject`, `followup_no`, MAX(Query_id) as queryid FROM `sales_queries` GROUP BY `nic_id` HAVING MAX(`Query_id`) SELECT `Query_id`, `nic_id`, `date`, `subject`, `followup_no`, MAX(Query_id) as queryid FROM `sales_queries` WHERE GROUP BY `nic_id` HAVING MAX(`Query_id`) SELECT `Query

How to select where there are 2 consecutives rows with a specific value using MySQL?

ぃ、小莉子 提交于 2019-12-12 14:35:22
问题 I'm building a system that should show when the students missed two days in a row. For example, this table contains the absences. day | id | missed ---------------------------------- 2016-10-6 | 1 | true 2016-10-6 | 2 | true 2016-10-6 | 3 | false 2016-10-7 | 1 | true 2016-10-7 | 2 | false 2016-10-7 | 3 | true 2016-10-10 | 1 | false 2016-10-10 | 2 | true 2016-10-10 | 3 | true (days 2016-10-8 and 2016-10-9 are weekend) in the case above: student 1 missed the days 1st and 2nd. (consecutive)

Variant use of the GROUP BY clause in TSQL

痞子三分冷 提交于 2019-12-12 12:52:18
问题 Imagine the following schema and sample data (SQL Server 2008): OriginatingObject ---------------------------------------------- ID 1 2 3 ValueSet ---------------------------------------------- ID OriginatingObjectID DateStamp 1 1 2009-05-21 10:41:43 2 1 2009-05-22 12:11:51 3 1 2009-05-22 12:13:25 4 2 2009-05-21 10:42:40 5 2 2009-05-20 02:21:34 6 1 2009-05-21 23:41:43 7 3 2009-05-26 14:56:01 Value ---------------------------------------------- ID ValueSetID Value 1 1 28 etc (a set of rows for

MySQL: How to GROUP BY a field to retrieve the rows with ORDER BY another field?

怎甘沉沦 提交于 2019-12-12 12:33:31
问题 assume following data: Data: id | date | name | grade --------+---------------+-----------+--------------- 1 | 2010/12/03 | Mike | 12 2 | 2010/12/04 | Jenny | 12 3 | 2010/12/04 | Ronald | 15 4 | 2010/12/03 | Yeni | 11 i want to know who has the best grade in each day, something like this: Desired Result: id | date | name | grade --------+---------------+-----------+--------------- 1 | 2010/12/03 | Mike | 12 3 | 2010/12/04 | Ronald | 15 i thought query should look like this: SELECT name FROM

SQL Server Count on a grouped by

感情迁移 提交于 2019-12-12 12:20:28
问题 I got 3 tables. I want to group by the item.name and count the times it can be found in idetail. This query only counts all rows not the grouped by rows. Anyone knows how to fix it? SELECT i.name, COUNT(i.name) FROM item AS i INNER JOIN item_category AS ic ON i.i_category_id = ic.ic_id INNER JOIN idetail AS id ON ic.ic_id = id.id_category_id WHERE ic.ic_id = 1002 GROUP BY i.name 回答1: This is what you want: select x.name , count(*) as cntNames , sum(x.cntDetails) as cntDetails from ( SELECT i

Using Pandas GroupBy and size()/count() to generate an aggregated DataFrame

末鹿安然 提交于 2019-12-12 12:19:45
问题 So I currently have a DataFrame called df that goes: date tag 2011-02-18 12:57:00-07:00 A 2011-02-19 12:57:00-07:00 A 2011-03-18 12:57:00-07:00 B 2011-04-01 12:57:00-07:00 C 2011-05-19 12:57:00-07:00 Z 2011-06-03 12:57:00-07:00 A 2011-06-05 12:57:00-07:00 A ... I'm trying to do a GroupBy the tag, and the date (yr/month), so it looks like: date A B C Z 2011-02 2 0 0 0 2011-03 0 1 0 0 2011-04 0 0 1 0 2011-05 0 0 0 1 2011-06 2 0 0 0 ... I've tried the following, but it doesn't quite give me what

Pyspark:How to calculate avg and count in a single groupBy? [duplicate]

心已入冬 提交于 2019-12-12 12:14:25
问题 This question already has answers here : Multiple Aggregate operations on the same column of a spark dataframe (3 answers) Closed last year . I would like to calculate avg and count in a single group by statement in Pyspark. How can I do that? df = spark.createDataFrame([(1, 'John', 1.79, 28,'M', 'Doctor'), (2, 'Steve', 1.78, 45,'M', None), (3, 'Emma', 1.75, None, None, None), (4, 'Ashley',1.6, 33,'F', 'Analyst'), (5, 'Olivia', 1.8, 54,'F', 'Teacher'), (6, 'Hannah', 1.82, None, 'F', None), (7