我有一个查询
select t1.id,t1.col1,t1.col2,t2.col1,t2.col2,sum(t2.col3) as total
From table1 t1
join table2 t2 on t1.id=t2.id
where t1.col =@col
Group by t1.id
它给了我一个错误
t1.col1 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。
如何在 group by 子句中选择多列和单列?
我有一个查询
select t1.id,t1.col1,t1.col2,t2.col1,t2.col2,sum(t2.col3) as total
From table1 t1
join table2 t2 on t1.id=t2.id
where t1.col =@col
Group by t1.id
它给了我一个错误
t1.col1 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。
如何在 group by 子句中选择多列和单列?
简单地说,你不能在子句中选择多列Group By
,除非你把它们放在某个组函数中,例如 AVG、MIN、MAX 等,或者你在Group BY
子句本身中添加所有列。
select t1.id,t1.col1,t1.col2,t2.col1,t2.col2,sum(t2.col3) as total
From table1 t1
join table2 t2 on t1.id=t2.id
where t1.col =@col
Group by t1.id , ,t1.col1,t1.col2,t2.col1,t2.col2
如果 t1.col1,t1.col2,t2.col1,t2.col2 是数字列,则可以min, max
使用。对于字符串列,您需要在groupby
Statement中使用它们