2013-03-19 165 views
-1
select distinct 
    assignedTo, 
    alert_id, 
    insert_date_time, 
    alert_status_id, 
    alert_action_id, 
    alert_call_reason_id, 
    target_date 
from Case_Management.AlertDetail 

工作正常。(看似)简单查询的SQL错误

select distinct 
    assignedTo, 
    alert_id, 
    max(insert_date_time), 
    alert_status_id, 
    alert_action_id, 
    alert_call_reason_id, 
    target_date 
from Case_Management.AlertDetail 

返回,因为它不是在聚合函数或GROUP BY子句中包含的错误列“Case_Management.AlertDetail.assignedTo”在选择列表中无效。

我很难过。

+1

对于其余字段,您正在使用不带“GROUP BY”的聚合函数('MAX')。 – LittleBobbyTables 2013-03-19 19:41:54

回答

5

的错误是很清楚,在聚合函数添加不列到GROUP BY条款:

select 
    assignedTo, 
    alert_id, 
    max(insert_date_time), 
    alert_status_id, 
    alert_action_id, 
    alert_call_reason_id, 
    target_date 
from Case_Management.AlertDetail 
GROUP BY assignedTo, 
     alert_id, 
     alert_status_id, 
     alert_action_id, 
     alert_call_reason_id, 
     target_date; 
0

您BY子句中的第2个查询需要一个组,因为你有一个汇总。总计是

max(insert_date_time) 
0

想想你想达到什么。您想要选择一些记录,但其中一列不是常规记录的内容,而是所有列的汇总。你无法混合。

您需要对数据进行分组以实现该功能或使用子选择。