group-by

Count each condition within group

空扰寡人 提交于 2020-01-13 08:27:23
问题 For every unique GroupId I would like to get a count of each IsGreen , IsRound , IsLoud condition and a total number of rows. Sample data: ----------------------------------------------------- id | ItemId | GroupId | IsGreen | IsRound | IsLoud ----+--------+---------+---------+---------+--------- 1 | 1001 | 1 | 0 | 1 | 1 2 | 1002 | 1 | 1 | 1 | 0 3 | 1003 | 2 | 0 | 0 | 0 4 | 1004 | 2 | 1 | 0 | 1 5 | 1005 | 2 | 0 | 0 | 0 6 | 1006 | 3 | 0 | 0 | 0 7 | 1007 | 3 | 0 | 0 | 0 Desired result: --------

Pandas - Convert columns to new rows after groupby

人盡茶涼 提交于 2020-01-13 06:15:49
问题 I have a pandas dataframe. I need to convert some of the columns into rows. The dataframe has same data in the first two columns for every 3 rows. So, I need 6 more columns as you will see in my expected dataframe. I have the following dataframe: shopCode Product Code Score 111 Apple 123 0.70 111 Apple 456 0.75 111 Apple 789 0.80 222 Orange 142 0.66 222 Orange 136 0.83 222 Orange 623 0.76 My expected dataframe is: shopCode Product Code1 Code2 Code3 Score1 Score2 Score3 111 Apple 123 456 789 0

Doctrine2 Order By before Group By

流过昼夜 提交于 2020-01-13 06:15:13
问题 I am having issues implementing a sub-select solution for ORDERING a resulting dataset before the GROUP BY reduces it. Normally, in SQL you would do a sub-select: SELECT * FROM ( SELECT * FROM a_table order by a_table.timestamp desc ) as table_tmp group by userId However, I am having difficulty implementing this in DQL. Can anyone point me in the right direction please? My query is more complex than this and I assume I JOIN other tables through 'table_tmp' and in the outer SELECT. Thanks. 回答1

How to select the record contains MAX(some_field) within GROUP(group by)

ⅰ亾dé卋堺 提交于 2020-01-13 06:12:48
问题 SELECT MAX(some_field) FROM table_A GROUP BY another_field This only gets the max value of the 'some_field'; I want to get the whole record which contains the MAX(some_field) . 回答1: select a.* from table_A a inner join ( SELECT another_field, MAX(some_field) as MaxSomeField FROM table_A GROUP BY another_field ) am on a.another_field = am.another_field and a.some_field = am.MaxSomeField 回答2: SELECT * FROM table_A INNER JOIN ( SELECT MAX(some_field) AS some_field, another_field FROM table_A

How to select the record contains MAX(some_field) within GROUP(group by)

不羁岁月 提交于 2020-01-13 06:12:46
问题 SELECT MAX(some_field) FROM table_A GROUP BY another_field This only gets the max value of the 'some_field'; I want to get the whole record which contains the MAX(some_field) . 回答1: select a.* from table_A a inner join ( SELECT another_field, MAX(some_field) as MaxSomeField FROM table_A GROUP BY another_field ) am on a.another_field = am.another_field and a.some_field = am.MaxSomeField 回答2: SELECT * FROM table_A INNER JOIN ( SELECT MAX(some_field) AS some_field, another_field FROM table_A

How to select the record contains MAX(some_field) within GROUP(group by)

↘锁芯ラ 提交于 2020-01-13 06:12:45
问题 SELECT MAX(some_field) FROM table_A GROUP BY another_field This only gets the max value of the 'some_field'; I want to get the whole record which contains the MAX(some_field) . 回答1: select a.* from table_A a inner join ( SELECT another_field, MAX(some_field) as MaxSomeField FROM table_A GROUP BY another_field ) am on a.another_field = am.another_field and a.some_field = am.MaxSomeField 回答2: SELECT * FROM table_A INNER JOIN ( SELECT MAX(some_field) AS some_field, another_field FROM table_A

including missing (zero-count) rows when using GROUP BY

主宰稳场 提交于 2020-01-13 03:48:30
问题 I have an application that receives sms messages. What i want to do is make a statistic with mysql that will count meessages in a hour. For example in 7 am i received 10 sms messages, in 8 am i received 20 etc. My table has this columns ID, smsText, smsDate ... (others are not important). When i run this script: SELECT HOUR(smsDate), COUNT(ID) FROM SMS_MESSAGES GROUP BY HOUR(smsDate) it show how many messages i get in every hour. The problem is when i dont receive any message for example in

Check if all elements in a group are equal using pandas GroupBy

六月ゝ 毕业季﹏ 提交于 2020-01-13 02:13:30
问题 Is there a pythonic way to group by a field and check if all elements of each resulting group have the same value? Sample data: datetime rating signal 0 2018-12-27 11:33:00 IG 0 1 2018-12-27 11:33:00 HY -1 2 2018-12-27 11:49:00 IG 0 3 2018-12-27 11:49:00 HY -1 4 2018-12-27 12:00:00 IG 0 5 2018-12-27 12:00:00 HY -1 6 2018-12-27 12:49:00 IG 0 7 2018-12-27 12:49:00 HY -1 8 2018-12-27 14:56:00 IG 0 9 2018-12-27 14:56:00 HY -1 10 2018-12-27 15:12:00 IG 0 11 2018-12-27 15:12:00 HY -1 12 2018-12-20

Check if all elements in a group are equal using pandas GroupBy

青春壹個敷衍的年華 提交于 2020-01-13 02:13:13
问题 Is there a pythonic way to group by a field and check if all elements of each resulting group have the same value? Sample data: datetime rating signal 0 2018-12-27 11:33:00 IG 0 1 2018-12-27 11:33:00 HY -1 2 2018-12-27 11:49:00 IG 0 3 2018-12-27 11:49:00 HY -1 4 2018-12-27 12:00:00 IG 0 5 2018-12-27 12:00:00 HY -1 6 2018-12-27 12:49:00 IG 0 7 2018-12-27 12:49:00 HY -1 8 2018-12-27 14:56:00 IG 0 9 2018-12-27 14:56:00 HY -1 10 2018-12-27 15:12:00 IG 0 11 2018-12-27 15:12:00 HY -1 12 2018-12-20

SQL: Is it possible to 'group by' according to 'like' function's results?

会有一股神秘感。 提交于 2020-01-12 14:26:08
问题 I am using Oracle SQL and I want to group some different rows that 'like' function results. To elaborate with an example: Let's assume I have a table MESA with one of the columns is a huge string. And I am counting the number of rows matching particular patterns: SELECT m.str, count(*) FROM MESA m WHERE m.str LIKE '%FRUIT%' AND (m.str LIKE '%APPLE%' OR m.str LIKE '%ORANGE%') So let's assume the result of this query is: FRUIT..afsafafasfa...RED_APPLE 20 FRUIT..afsafafasfa...YELLOW_APPLE 12