group-by

MySQL dense rank for each group/partition

岁酱吖の 提交于 2021-02-19 08:57:39
问题 I am trying to build a dense rank kind of functionality but not exactly dense rank. I want to rank each group but within each group I want to keep the rank same for same values. I've written the following query which gives me a dense rank kind of functionality SELECT id, item_code @curRank := @curRank + 1 AS rank FROM table t, (SELECT @curRank := 0) r WHERE <several filters> Whereas, I want this The closest question I found is this one MySQL give a rank to each group Please help. 回答1: You can

SQL turning rows into columns and populating with values

一世执手 提交于 2021-02-19 08:22:08
问题 I'm not quite sure why this table was designed this way, but it's making it hard to solve my problem. Looking at the data: NAME TYPE_NAME DEFAULT_VALUE VALUE TEST 1 Currency Null 14 TEST 1 Event Count 0 0 TEST 1 Usage 8 Null TEST 1 Events Amt 0 0 TEST 1 Usage Amt Null 13 TEST 1 From Date Null 5 TEST 1 To Date 6 Null TEST 1 Traffic Scenario Null 2 TEST 1 Band 1 Null TEST 1 Service 15 Null TEST 1 Tariff Rate Name Null 4 TEST 2 Currency EUR 0 TEST 2 Event Count Null 9 TEST 2 Usage 10 Null TEST 2

Calculate Percentages In Query - Access SQL

匆匆过客 提交于 2021-02-19 07:42:31
问题 I'm trying to quantify some things. Here's a sample (simplified of course): tblParent: { Number, Name } tblChild: { Number, Name, ParentNumber, Criterion } I'd like to count the total number of Children with the same ParentNumber (easy using a Group By and a Count(1) ), but the problem is comparing that number with the number of children with the same ParentNumber who have Criterion = "Something" . Here's what I have so far: SELECT "Criterion = 1" AS CritDesc, COUNT(1) AS Total, ParentNumber

Creating percentage stacked bar chart using groupby

六眼飞鱼酱① 提交于 2021-02-19 06:20:08
问题 I'm looking at home ownership within levels of different loan statuses, and I'd like to display this using a stacked bar chart in percentages. I've been able to create a frequency stacked bar chart using this code: df_trunc1=df[['loan_status','home_ownership','id']] sub_df1=df_trunc1.groupby(['loan_status','home_ownership'])['id'].count() sub_df1.unstack().plot(kind='bar',stacked=True,rot=1,figsize=(8,8),title="Home ownership across Loan Types") which gives me this picture:1 but I can't

Creating percentage stacked bar chart using groupby

大兔子大兔子 提交于 2021-02-19 06:20:06
问题 I'm looking at home ownership within levels of different loan statuses, and I'd like to display this using a stacked bar chart in percentages. I've been able to create a frequency stacked bar chart using this code: df_trunc1=df[['loan_status','home_ownership','id']] sub_df1=df_trunc1.groupby(['loan_status','home_ownership'])['id'].count() sub_df1.unstack().plot(kind='bar',stacked=True,rot=1,figsize=(8,8),title="Home ownership across Loan Types") which gives me this picture:1 but I can't

GROUP BY for specific rows

ε祈祈猫儿з 提交于 2021-02-18 18:58:41
问题 I have a sql server result which groups by person. And SUMS's their daily figure and monthly figure. Person Team Daily Figure Month To Date Tony Team 1 53 635 Dan Team 2 47 172 Tom Team 3 17 232 Simon Team 2 16 655 Ben Team 3 17 232 Fred Team 2 16 655 How do i group these results just by team 2? For example i still want the others individual results to show but i want just team 2 to be grouped together. Example result Person Team Daily Figure Month To Date Tony Team 1 53 635 Tom Team 3 17 232

LINQ: Selecting items from a list (Group By/Select/Sum & Max!)

这一生的挚爱 提交于 2021-02-18 05:50:49
问题 Just getting my head around Linq and having lots of fun! Can any one aid me with a query for this: I have a list of data: Key Value Aaa 12 AaA 10 AAa 5 BBB 2 Bbb 1 1. I want to group by Key.ToUpper() 2. For every group I need the Max(Value) & Sum(Value) 3. For every group I want to select the entries There the Value != Max(value) the final result should be like this: Key Max Total AaA 12 27 AAa 12 27 Bbb 2 3 Thanks! Update, actually I also need the Key from the Maximum entry: Key Max Total

LINQ: Selecting items from a list (Group By/Select/Sum & Max!)

≡放荡痞女 提交于 2021-02-18 05:50:25
问题 Just getting my head around Linq and having lots of fun! Can any one aid me with a query for this: I have a list of data: Key Value Aaa 12 AaA 10 AAa 5 BBB 2 Bbb 1 1. I want to group by Key.ToUpper() 2. For every group I need the Max(Value) & Sum(Value) 3. For every group I want to select the entries There the Value != Max(value) the final result should be like this: Key Max Total AaA 12 27 AAa 12 27 Bbb 2 3 Thanks! Update, actually I also need the Key from the Maximum entry: Key Max Total

R How to group_by, split or subset by row values

拜拜、爱过 提交于 2021-02-17 07:08:07
问题 This is continued from last question R, how to group by row value? Split? The change in input Dataframe is id = str_c("x",1:22) val = c(rep("NO1", 2), "START", rep("yes1", 2), "STOP", "NO", "START","NO1", "START", rep("yes2", 3), "STOP", "NO1", "START", rep("NO3",3), "STOP", "NO1", "STOP") data = data.frame(id,val) Expected output is dataframe with val column as follows- val = c("START", rep("yes1", 2), "STOP", "START","NO1", "START", rep("yes2", 3), "STOP", "START", rep("NO3",3), "STOP",

Converting series from pandas to pyspark: need to use “groupby” and “size”, but pyspark yields error

不打扰是莪最后的温柔 提交于 2021-02-17 07:03:31
问题 I am converting some code from Pandas to pyspark. In pandas, lets imagine I have the following mock dataframe, df: And in pandas, I define a certain variable the following way: value = df.groupby(["Age", "Siblings"]).size() And the output is a series as follows: However, when trying to covert this to pyspark, an error comes up: AttributeError: 'GroupedData' object has no attribute 'size' . Can anyone help me solve this? 回答1: The equivalent of size in pyspark is count: df.groupby(["Age",