group-by

SQL Server: aggregate error on grouping

心已入冬 提交于 2021-01-29 11:01:23
问题 I have a table called tasks, which lists different tasks a worker can complete. Then i have a relationship table that links a completed task to a worker. I'm trying to write query that groups the tasks into a list based on the worker id, but the query gives me the following error (see below). Column 'mater.dbo.worker_task_completion.FK_task_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Tables CREATE TABLE [dbo].[tasks] (

SQL Getting sum of all rows with Inner join and group by

廉价感情. 提交于 2021-01-29 09:24:10
问题 I am trying to get the sum of rows of the same DueDate . I'm joing 2 tables to populate other fields. However, I'm having difficulty aggregating the values on Amount column so that I can end up with one row for a specific DueDate The other columns all have the same value except the Amount and DueDate . Below is my query SELECT T2.Company, T2.Code T2.DueDate, SUM(T2.Amount) FROM TBL2 T2 LEFT JOIN TBL1 T1 ON T2.Company = T1.Company AND T2.Code IN ( 0, 1, 2, 3, 4, 5, 6, 7, 8 ) GROUP BY T1

Group (collapse?) rows without summarising to fill in NAs

和自甴很熟 提交于 2021-01-29 08:07:52
问题 I have an issue that should be straightforward with dplyr (I think) but I can't seem to find a resolution. My dataframe comprises numbers and factors. Each observation is represented by two rows which have either a value or NA in one of two columns (Agg_Entropy and Av_Amplitude). I want to combine each observation's rows into a single row (without summarising), so that the NAs are replaced with the relevant values. A simple excerpt of the dataframe: Selection Low High Agg_Entropy Av_Amplitude

Plot through time setting specific filtering [closed]

独自空忆成欢 提交于 2021-01-29 07:30:43
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 months ago . Improve this question I have a dataset like this: Freq Date 01-11 1 05-11 1 04-10 1 02-12 1 12-10 1 ... ... 07-20 286 06-20 288 05-20 390 04-20 471 03-20 510 I would like to plot the frequency by month, to create 12 plots one per each month, showing the frequency through through

Add row to dataframe with sum of within group data

蓝咒 提交于 2021-01-29 07:15:50
问题 I have an example dataframe below. eg_data <- data.frame( time = c("1", "1", "2","2"), type = c("long", "short","long", "short"), size=c(200,50, 500, 150 )) I need to create rows which total the values of size, for each time period. I have looked at combinations of aggregate and by, but I cannot get it to work correctly. An example of what I've tried: rbind(eg_data, data.frame(time="1 + 2", type="long", size=by(eg_data$size, eg_data$time=="long", sum))) An example of what I want the final

Keep zero values with GROUP BY and SUM in mysql

旧街凉风 提交于 2021-01-29 05:23:04
问题 I have two tables career_types and bookings career_types id | name 1 | foo 2 | bar 3 | baz bookings id | career_type_id | details 1 | 1 | foo 2 | 1 | bar 3 | 2 | baz My bookings table has a lot of entries for all career types except career_types.id of 3. I want a query that will list all my career_types.name's and how many each booking has, and in the case of career_types.id = 1 where there are no bookings for it. I want to return 0. So far I've been trying variations of SELECT career_types

How to pivot a dataframe?

左心房为你撑大大i 提交于 2021-01-29 05:10:25
问题 What is pivot? How do I pivot? Is this a pivot? Long format to wide format? I've seen a lot of questions that ask about pivot tables. Even if they don't know that they are asking about pivot tables, they usually are. It is virtually impossible to write a canonical question and answer that encompasses all aspects of pivoting... ... But I'm going to give it a go. The problem with existing questions and answers is that often the question is focused on a nuance that the OP has trouble

How to make null/none count 0 instead?

大城市里の小女人 提交于 2021-01-29 05:02:17
问题 I have some sqlalchemy that counts rows with the value "3" and then prints out the count. It works fine if the count is 1, 2, 3, etc, but when there are no items, it prints nothing. I wanted it to print 0. So I added an if/else statement that didn't work (it prints nothing, no errors). How would I do this? Here's my current code — not really sure if I should be doing this part in sqlalchemy or python. q = (db.session.query(Article.snippet, sa.func.count(ArticleVote.id)) .join(ArticleVote,

Oracle SQL: How return several distinct columns

与世无争的帅哥 提交于 2021-01-29 04:57:20
问题 I have an Oracle Query that outputs but I wish to output all distinct column except for Incident ID, i.e. I tried adding GROUP BY to Oracle Query but get error, DataSource.Error: Oracle: ORA-00979: not a GROUP BY expression Details: DataSourceKind=Oracle DataSourcePath=dlporacle Message=ORA-00979: not a GROUP BY expression ErrorCode=-2147467259 Below is my Query ... please assist SELECT I.INCIDENTID AS "Incident ID", I.CREATIONDATE AS "Creation Date", MO.IPADDRESS AS "IP Address", MO

Oracle SQL: How return several distinct columns

血红的双手。 提交于 2021-01-29 04:51:04
问题 I have an Oracle Query that outputs but I wish to output all distinct column except for Incident ID, i.e. I tried adding GROUP BY to Oracle Query but get error, DataSource.Error: Oracle: ORA-00979: not a GROUP BY expression Details: DataSourceKind=Oracle DataSourcePath=dlporacle Message=ORA-00979: not a GROUP BY expression ErrorCode=-2147467259 Below is my Query ... please assist SELECT I.INCIDENTID AS "Incident ID", I.CREATIONDATE AS "Creation Date", MO.IPADDRESS AS "IP Address", MO