180
Views
4
Comments
column invalid in the select list because it is not contained in aggregate function
Question

Hey all,

In my sql query getting error for a column, stating it invalid column. Below is the query:

SELECT 
{TimeSheetWorkingData}.[Key1], {TimeSheetWorkingData}.[Key2], {TimeSheetWorkingData}.[Key3], {TimeSheetWorkingData}.[Key4],
sum({TimeSheetWorkingData}.[Day01Hours]) as Day01HoursSum, sum({TimeSheetWorkingData}.[Day02Hours]) as Day02HoursSum, sum({TimeSheetWorkingData}.[Day03Hours]) as Day03HoursSum,
sum({TimeSheetWorkingData}.[Day04Hours]) as Day04HoursSum, sum({TimeSheetWorkingData}.[Day05Hours]) as Day05HoursSum, 
sum({TimeSheetWorkingData}.[Day01Hours])+ sum({TimeSheetWorkingData}.[Day02Hours])+ sum({TimeSheetWorkingData}.[Day03Hours])+
sum({TimeSheetWorkingData}.[Day04Hours])+ sum({TimeSheetWorkingData}.[Day05Hours]) as TotalSum
from {TimeSheetWorkingData}
join {TimeEntryType}
on {TimeSheetWorkingData}.[TimeEntryTypeId] = {TimeEntryType}.[Id] 
where {TimeSheetWorkingData}.[Key1] = @LeaveId 
and {TimeSheetWorkingData}.[TimeSheetId] = @TimesheetId 
and {TimeSheetWorkingData}.[TimeEntryTypeId] = @TimeEntryType 

Getting the below error:

It is throwing error for below columns:

{TimeSheetWorkingData}.[Key1], {TimeSheetWorkingData}.[Key2], {TimeSheetWorkingData}.[Key3], {TimeSheetWorkingData}.[Key4]
2021-07-07 13-36-32
Hans Dollen

add
Group by {TimeSheetWorkingData}.[Key1], {TimeSheetWorkingData}.[Key2], {TimeSheetWorkingData}.[Key3], {TimeSheetWorkingData}.[Key4]
to your query.

Since you specify Key1 etc, together with a sum, it wants to summarize data, and therefore needs to group data together to calculate the sum. 

UserImage.jpg
Nitin Soni

Hans Dollen wrote:

add
Group by {TimeSheetWorkingData}.[Key1], {TimeSheetWorkingData}.[Key2], {TimeSheetWorkingData}.[Key3], {TimeSheetWorkingData}.[Key4]
to your query.

Since you specify Key1 etc, together with a sum, it wants to summarize data, and therefore needs to group data together to calculate the sum. 

 Error goes after applying group by but sum is also grouped into several rows, can I still get only one value from sum function ?

 

2017-07-15 18-41-36
Sachin Mahawar

Hi Nitin,

This error generally comes when we included certain column in the select list which is neither part of the group by clause nor included in an aggregate function.

Check this

Thanks,

Sachin


2024-10-23 03-01-14
Donis Sousa
Champion

in another words, your GROUP BY a column is missing that exists in the SELECT

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.