Sql 无法使用Group by-显示错误:列必须出现在Group by子句中或在聚合函数中使用

Sql 无法使用Group by-显示错误:列必须出现在Group by子句中或在聚合函数中使用,sql,postgresql,Sql,Postgresql,基于oncolumn1的不同值,其他列应该适合该值。 例如 结果应该是,, 如果选择了4列,但其中没有一列是聚合函数的一部分,则需要将它们全部放在group by子句中。这就是没有错误的代码: org.postgresql.util.PSQLException: ERROR: column "table1.column2" must appear in the GROUP BY clause or be used in an aggregate function 这样做: select t

基于oncolumn1的不同值,其他列应该适合该值。 例如

结果应该是,,

如果选择了4列,但其中没有一列是聚合函数的一部分,则需要将它们全部放在group by子句中。这就是没有错误的代码:

org.postgresql.util.PSQLException: ERROR: column "table1.column2" must appear in the GROUP BY clause or be used in an aggregate function 
这样做:

select table1.column1
       , table1.column2
       , table2.column1
       , table2.column2 
from table1
join table2 on table1.column1=table2.column1 
where table1.column2 in ('data1','data2','data3') 
and table2.column1 <> 1 
group by table1.column1
       , table1.column2
       , table2.column1
       , table2.column2 ;
from table1,table2 
where table1.column1=table2.column1
另一个温暖的建议是在查询中使用别名:

from table1
join table2  on table1.column1 = table2.column1
选择t1.column1
,t1.2
,t2.1
,t2.2
来自表1 t1
在t1.column1=t2.column1上连接表2 t2
其中t1.column2位于('data1','data2','data3')
和t2.1
按t1.1列分组
,t1.2
,t2.1
,t2.2;
如果要选择“我想获取包含“table1.column1”的不同值的行”,但也要显示其他列,则需要决定对其他列做什么?例如:

select t1.column1
       , t1.column2
       , t2.column1
       , t2.column2 
from table1 t1
join table2 t2 on t1.column1 = t2.column1 
where t1.column2 in ('data1','data2','data3') 
and t2.column1 <> 1 
group by t1.column1
       , t1.column2
       , t2.column1
       , t2.column2;
选择t1.column1
,最大值(t1.2列)
,总和(t2.第1列)
,最小值(t2.第2列)
来自表1 t1
在t1.column1=t2.column1上连接表2 t2
其中t1.column2位于('data1','data2','data3')
和t2.1
按t1.1分组;
您的查询应该是:

select t1.column1
       , max(t1.column2)
       , sum(t2.column1)
       , min(t2.column2)
from table1 t1
join table2 t2 on t1.column1 = t2.column1 
where t1.column2 in ('data1','data2','data3') 
and t2.column1 <> 1 
group by t1.column1;

你好@Mohamedasiq这个答案和我提供的有什么不同?你是100%正确的@VBoka。我提出了完整的问题,而不是部分的解释。对于初学者来说,他们不知道如何进一步进行简单的解释。但是@Mohamedasiq I也有完整的查询和部分查询,并且所有查询都是在这个答案之前编写的?以及附加的带别名的完整查询,以及用户可能需要的解决方案。在我的回答中,我的问题与你的答案相同,所以我真的很困惑,这个答案带来了什么?我不是说@VBoka。你的答案是你自己的。在编辑之前我刚刚告诉过你,我在你的答案中只看到了部分解释。谢谢@VBoka:-)希望我没有伤害你。基于column1的不同值,其他列应该适合于此。我想删除column1中包含重复值的行。应取第1列中包含值“abc”的第一行。@SakthivelNatarajan请删除选择第二个表列并重试。无论他想忽略什么数据,都不应在where子句中给出。@SakthivelNatarajan。请尝试使用要筛选的条件。通过分组,您的条件基于Column1,并且使用where子句包含Column2中不需要的值。这就是为什么您会得到多个值。基于什么原因,您需要第一条记录而不是第二条记录?我想知道为什么要忽略第2列下包含值673的记录。这是否意味着,您只需要最匹配的记录?Column2是否有外键引用?
select t1.column1
       , t1.column2
       , t2.column1
       , t2.column2 
from table1 t1
join table2 t2 on t1.column1 = t2.column1 
where t1.column2 in ('data1','data2','data3') 
and t2.column1 <> 1 
group by t1.column1
       , t1.column2
       , t2.column1
       , t2.column2;
select t1.column1
       , max(t1.column2)
       , sum(t2.column1)
       , min(t2.column2)
from table1 t1
join table2 t2 on t1.column1 = t2.column1 
where t1.column2 in ('data1','data2','data3') 
and t2.column1 <> 1 
group by t1.column1;
     select a.Column1,a.Column2
from Table1 a
Inner join Table2 b
on a.Column1  = b.Column1
where a.Column2 in ('1231','562','122')
group by a.column1,a.Column2
order by a.column1,a.Column2