0

我有一个查询

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 子句中选择多列和单列?

4

2 回答 2

2

简单地说,你不能在子句中选择多列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
于 2012-06-30T06:50:10.893 回答
0

如果 t1.col1,t1.col2,t2.col1,t2.col2 是数字列,则可以min, max使用。对于字符串列,您需要在groupbyStatement中使用它们

于 2012-06-30T06:46:56.383 回答