-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 

工作正常。

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 

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

我难住了。

4

3 回答 3

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;
于 2013-03-19T19:42:13.797 回答
0

想想你想要达到的目标。您要选择一些记录,但其中一列不是常规记录的内容,而是所有列的汇总。你不能混在一起。

您需要对数据进行分组以实现这一目标或使用子选择。

于 2013-03-19T19:44:08.990 回答
0

您需要在第二个查询中使用 group by 子句,因为您有一个聚合。总量是

max(insert_date_time)
于 2013-03-19T19:42:21.250 回答