2014-02-20 48 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;