2014-02-20 47 views
0

按星期查询的我的SQL组如下所示,但是在那里出现跟随错误。按星期查询获取错误组

列'timesheet.timeheet_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

是否使用的是RDBMS? –

+1

你有没有试过找出错误信息试图告诉你什么?这是一个非常明确的信息。 – Tomalak

+0

@astander这就是SQL Server。 – Tomalak

回答

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 
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;