group-by

MySQL SELECT n records base on GROUP BY

耗尽温柔 提交于 2019-12-19 22:04:59
问题 Lets say I have SQL records: Country | Number USA | 300 USA | 450 USA | 500 USA | 100 UK | 300 UK | 400 UK | 1000 And I am doing something like this: SELECT * FROM table GROUP BY Country . What if, I want to choose to display the result with 2 greatest number only in each country? How can I archive this? The result would be: Country | Number USA | 450 USA | 500 UK | 400 UK | 1000 回答1: Sample data create table data (Country varchar(10), Number int); insert into data select 'USA' , 300 union

MySQL SELECT n records base on GROUP BY

雨燕双飞 提交于 2019-12-19 22:04:36
问题 Lets say I have SQL records: Country | Number USA | 300 USA | 450 USA | 500 USA | 100 UK | 300 UK | 400 UK | 1000 And I am doing something like this: SELECT * FROM table GROUP BY Country . What if, I want to choose to display the result with 2 greatest number only in each country? How can I archive this? The result would be: Country | Number USA | 450 USA | 500 UK | 400 UK | 1000 回答1: Sample data create table data (Country varchar(10), Number int); insert into data select 'USA' , 300 union

Group by a field not in select

余生颓废 提交于 2019-12-19 21:22:03
问题 I want to find how many modules a lecturer taught in a specific year and want to select name of the lecturer and the number of modules for that lecturer. Problem is that because I am selecting Name , and I have to group it by name to make it work. But what if there are two lecturers with same name? Then sql will make them one and that would be wrong output. So what I really want to do is select name but group by id , which sql is not allowing me to do. Is there a way around it? Below are the

Group by a field not in select

…衆ロ難τιáo~ 提交于 2019-12-19 21:21:24
问题 I want to find how many modules a lecturer taught in a specific year and want to select name of the lecturer and the number of modules for that lecturer. Problem is that because I am selecting Name , and I have to group it by name to make it work. But what if there are two lecturers with same name? Then sql will make them one and that would be wrong output. So what I really want to do is select name but group by id , which sql is not allowing me to do. Is there a way around it? Below are the

Group by a field not in select

淺唱寂寞╮ 提交于 2019-12-19 21:20:21
问题 I want to find how many modules a lecturer taught in a specific year and want to select name of the lecturer and the number of modules for that lecturer. Problem is that because I am selecting Name , and I have to group it by name to make it work. But what if there are two lecturers with same name? Then sql will make them one and that would be wrong output. So what I really want to do is select name but group by id , which sql is not allowing me to do. Is there a way around it? Below are the

How do I include empty rows in a single GROUP BY DAY(date_field) SQL query?

你离开我真会死。 提交于 2019-12-19 19:53:12
问题 I'm using MS SQL Server but welcome comparitive solutions from other databases. This is the basic form of my query. It returns the number of calls per day from the 'incidentsm1' table: SELECT COUNT(*) AS "Calls", MAX(open_time), open_day FROM ( SELECT incident_id, opened_by, open_time - (9.0/24) AS open_time, DATEPART(dd, (open_time-(9.0/24))) AS open_day FROM incidentsm1 WHERE DATEDIFF(DAY, open_time-(9.0/24), GETDATE())< 7 ) inc1 GROUP BY open_day This data is used to draw a bar graph, but

SQL Server left joining

孤人 提交于 2019-12-19 19:49:20
问题 I'm trying to make left join in one query, but it seems that I'm wrong somewhere. table machines -------------- machineID FaNo Barcode RoutingCode Name table log ------------- logID lineBarcode machineBarcode In the log table there are records on the machines and the lines. On one line there can be many different machines and machines from the same type. The machine type is routingCode , so I'm interested in selecting all the machines in the line and group them. Only machines with different

How to pass a variable name in group_by

和自甴很熟 提交于 2019-12-19 19:44:41
问题 I can calculate the rank of the values (val) in my dataframe df within the group name1 with the code: res <- df %>% arrange(val) %>% group_by(name1) %>% mutate(RANK=row_number()) Instead of writing the column "name1" in the code, I want to pass it as variable, eg crit = "name1". However, the code below does not work since crit1 is assumed to be the column name instead of a variable name. res <- df %>% arrange(val) %>% group_by(crit1) %>% mutate(RANK=row_number()) How can I pass crit1 in the

Groupby class and count missing values in features

£可爱£侵袭症+ 提交于 2019-12-19 12:27:28
问题 I have a problem and I cannot find any solution in the web or documentation, even if I think that it is very trivial. What do I want to do? I have a dataframe like this CLASS FEATURE1 FEATURE2 FEATURE3 X A NaN NaN X NaN A NaN B A A A I want to group by the label( CLASS ) and display the number of NaN-Values that are counted in every feature so that it looks like this. The purpose of this is to get a general idea how missing values are distributed over the different classes. CLASS FEATURE1

Using IW and MM in Oracle

青春壹個敷衍的年華 提交于 2019-12-19 12:05:41
问题 I'm using IW for weekly result and MM for Monthly result. But I always get an error: ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" My queries are these (Weekly) SELECT 'Data' || ',' || TO_CHAR(d.dtime_day, 'MM/dd/yyyy') || ',' || NVL(o.cnt_opened, 0) --as cnt_opened || ',' || NVL(c.cnt_closed, 0) --as cnt_closed FROM owner_dwh.dc_date d LEFT JOIN ( SELECT TRUNC(t.create_time, 'IW') AS report_date, count(*) AS cnt_opened FROM app_account.otrs_ticket t WHERE t