我的SQL分组按周查询如下所示,但是我得到了以下错误。
列'timesheet.timesheet_date'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
在这方面有人可以帮助我
我的查询:
select
DatePart(week, timesheet_date),
count(timesheet.timesheet_id) as TimeSheetID,
timesheet.timesheet_date,
timesheet.start_time,
timesheet.end_time,
timesheet.timesheet_status_id,
timesheet.is_deleted,
timesheet.created_by,
timesheet.modified_by,
timesheet.created_date,
task.name,
project.name,
[user].first_name, [user].last_name
from
timesheet, task, project, [user]
where
task_id = (select task_id from project_task
where project_task_id = timesheet.project_task_id)
and project_id = (select project_id from project_task
where project_task_id = timesheet.project_task_id)
and [user].user_id = timesheet.user_id
and timesheet.user_id = 30
group by
timesheet.timesheet_id
答案 0 :(得分:1)
从异常消息中,我将假设SQL Server。
对于聚合(SUM / MAX / MIN / COUNT),您需要包括GROUP BY中不属于聚合的所有列。
因此,对于您的查询,您将拥有类似
的内容select DatePart(week, timesheet_date),
count(timesheet.timesheet_id) as TimeSheetID,
timesheet.timesheet_date,
timesheet.start_time,
timesheet.end_time,
timesheet.timesheet_status_id,
timesheet.is_deleted,
timesheet.created_by,
timesheet.modified_by,
timesheet.created_date,
task.name,
project.name,
[user].first_name,
[user].last_name
from timesheet,
task,
project,
[user]
where task_id = (select task_id from project_task where project_task_id=timesheet.project_task_id)
and project_id = (select project_id from project_task where project_task_id=timesheet.project_task_id)
and [user].user_id = timesheet.user_id
and timesheet.user_id =30
group by DatePart(week, timesheet_date),
timesheet.timesheet_date,
timesheet.start_time,
timesheet.end_time,
timesheet.timesheet_status_id,
timesheet.is_deleted,
timesheet.created_by,
timesheet.modified_by,
timesheet.created_date,
task.name,
project.name,
[user].first_name,
[user].last_name
答案 1 :(得分:0)
试试这个:
select DatePart(week, timesheet_date),
count(timesheet.timesheet_id) as TimeSheetID,
timesheet.timesheet_date,
timesheet.start_time,
timesheet.end_time,
timesheet.timesheet_status_id,
timesheet.is_deleted,
timesheet.created_by,
timesheet.modified_by,
timesheet.created_date,
task.name,
project.name,
[user].first_name,
[user].last_name
from timesheet,
task,
project,
[user]
where task_id = (select task_id from project_task where project_task_id = timesheet.project_task_id)
and project_id = (select project_id from project_task where project_task_id = timesheet.project_task_id)
and [user].user_id = timesheet.user_id
and timesheet.user_id =30
group by timesheet.timesheet_id,
timesheet.timesheet_date;