(貌似)简单查询的SQL错误

tod*_*und -1 sql

select distinct 
    assignedTo, 
    alert_id, 
    insert_date_time, 
    alert_status_id, 
    alert_action_id, 
    alert_call_reason_id, 
    target_date  
from Case_Management.AlertDetail 
Run Code Online (Sandbox Code Playgroud)

工作良好.

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 
Run Code Online (Sandbox Code Playgroud)

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

我很难过.

Mah*_*mal 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;
Run Code Online (Sandbox Code Playgroud)