group-by

Why should not disable ONLY_FULL_GROUP_BY

安稳与你 提交于 2020-12-13 05:56:57
问题 During internet searching I see lot of questions now to disable ONLY_FULL_GROUP_BY feature, so it mean lot of developers have a trouble with writing SQL queries in this strict mode. I know it pretty simple to disable this limitation, but now I ask asking: Why shouldn't I do this? What problems or side effects will be involved by removing ONLY_FULL_GROUP_BY limitation? 回答1: Without the strictness that ONLY_FULL_GROUP_BY enforces, you may: not realize you have the wrong query; be getting some

Want to find Year on Year calculation using Groupby and apply for various years

拈花ヽ惹草 提交于 2020-12-13 03:48:06
问题 I have a dataframe as follows: MARKET PRODUCT TIMEPERIOD DATE VALUES 0 USA MARKET APPLE QUARTER 2020-06-01 100 1 USA MARKET APPLE YEARLY 2020-06-01 1000 2 USA MARKET PEAR QUARTER 2020-06-01 200 3 USA MARKET PEAR YEARLY 2020-06-01 5000 4 USA MARKET APPLE QUARTER 2019-06-01 300 5 USA MARKET PEAR YEARLY 2019-06-01 2000 6 USA MARKET PEAR QUARTER 2019-06-01 100 7 USA MARKET PEAR YEARLY 2019-06-01 3000 8 USA MARKET APPLE QUARTER 2018-06-01 300 9 USA MARKET PEAR YEARLY 2018-06-01 2000 10 USA MARKET

Get count on two different date columns and group by date

非 Y 不嫁゛ 提交于 2020-12-12 07:05:33
问题 I have table containing two DATE columns. TS_customer and TS_verified I am searching for a way to get a result where in the first column I have dates where either someone created a user (TS_customer) or someone got verified (TS_verified). In the second column I want count(TS_customer) grouped by the first column. The third column I want count(TS_verified) grouped by the first column. It might be 0 customers verified on a sign up date, and in another case 0 signups on a date someone got

mysql select distinct comma delimited values

房东的猫 提交于 2020-12-12 06:58:03
问题 i have a mysql table id cid c_name keywords 1 28 Stutgart BW,Mercedes,Porsche,Auto,Germany 2 34 Roma Sezar,A.S. Roma 3 28 München BMW,Oktober Fest,Auto,Germany i need a query to show keywords from cid=28 but i want to see only 1 time a keyword, like (BW,Mercedes,Porsche,Auto,Bmw,Oktober Fest,Germany) i dont want to list 2 time a keyword, how can resolve this problem? i have tried distinct but could not get what i want 回答1: Split it before adding it all up with DISTINCT.Of course,better is to

mysql select distinct comma delimited values

旧巷老猫 提交于 2020-12-12 06:57:53
问题 i have a mysql table id cid c_name keywords 1 28 Stutgart BW,Mercedes,Porsche,Auto,Germany 2 34 Roma Sezar,A.S. Roma 3 28 München BMW,Oktober Fest,Auto,Germany i need a query to show keywords from cid=28 but i want to see only 1 time a keyword, like (BW,Mercedes,Porsche,Auto,Bmw,Oktober Fest,Germany) i dont want to list 2 time a keyword, how can resolve this problem? i have tried distinct but could not get what i want 回答1: Split it before adding it all up with DISTINCT.Of course,better is to

PostgreSQL select max with group by and additional value [duplicate]

我只是一个虾纸丫 提交于 2020-12-09 06:53:33
问题 This question already has answers here : Select first row in each GROUP BY group? (17 answers) Closed 5 years ago . With the following data from a SELECT * FROM (SELECT...) AS foo : ID Country Area 1 US 100 1 UK 200 2 AU 150 2 NZ 250 how can you select the top area and country by ID ? So GROUP BY ID and MAX(DESC) but also include the County . The the result of the query would be: 1 UK 200 2 NZ 250 回答1: SELECT DISTINCT ON (ID) ID, Country, Area FROM foo ORDER BY ID, Area DESC NULLS LAST;

PostgreSQL select max with group by and additional value [duplicate]

萝らか妹 提交于 2020-12-09 06:52:27
问题 This question already has answers here : Select first row in each GROUP BY group? (17 answers) Closed 5 years ago . With the following data from a SELECT * FROM (SELECT...) AS foo : ID Country Area 1 US 100 1 UK 200 2 AU 150 2 NZ 250 how can you select the top area and country by ID ? So GROUP BY ID and MAX(DESC) but also include the County . The the result of the query would be: 1 UK 200 2 NZ 250 回答1: SELECT DISTINCT ON (ID) ID, Country, Area FROM foo ORDER BY ID, Area DESC NULLS LAST;

MySQL how to get avg of values into specific range

不打扰是莪最后的温柔 提交于 2020-12-09 06:46:49
问题 I have the following table data: value 1 5 10.5 12 36 I want to map these values into: range avg 0-21 (1 + 5 + 10.5 + 12) / 4 21.001-34 0 34.001-64 36 64 + 0 Basically map each value to ranges and calculate the avg of all values in each range. I tried to do: select case when avggeneral between 0 and 21 then ' 0-21' when avggeneral between 21.00001 and 34 then ' 21-34' when avggeneral between 34.00001 and 64 then ' 34-64' else '64+' end as 'range', AVG(*) as 'avg' from table but this doesn't

MySQL how to get avg of values into specific range

北慕城南 提交于 2020-12-09 06:43:49
问题 I have the following table data: value 1 5 10.5 12 36 I want to map these values into: range avg 0-21 (1 + 5 + 10.5 + 12) / 4 21.001-34 0 34.001-64 36 64 + 0 Basically map each value to ranges and calculate the avg of all values in each range. I tried to do: select case when avggeneral between 0 and 21 then ' 0-21' when avggeneral between 21.00001 and 34 then ' 21-34' when avggeneral between 34.00001 and 64 then ' 34-64' else '64+' end as 'range', AVG(*) as 'avg' from table but this doesn't

How to group by one column and sort the values of another column?

让人想犯罪 __ 提交于 2020-12-08 11:47:30
问题 Here is my dataframe import pandas as pd df = pd.DataFrame({'A': ['one', 'one', 'two', 'two', 'one'] , 'B': ['Ar', 'Br', 'Cr', 'Ar','Ar'] , 'C': ['12/15/2011', '11/11/2001', '08/30/2015', '07/3/1999','03/03/2000' ], 'D':[1,7,3,4,5]}) My goal is to group by column A and sort within grouped results by column B . Here is what I came up with: sort_group = df.sort_values('B').groupby('A') I was hoping that grouping operation would not distort order, but it does not work and also returns not a