简体   繁体   English

必须出现在GROUP BY子句中或在聚合函数中使用(PostgreSQL)

[英]Must appear in the GROUP BY clause or be used in an aggregate function (PostgreSQL)

I have query like this : 我有这样的查询:

select u.fullname ,s.schedate,
sum(case when s.isvisiting =1 then s.isvisiting else 0 end) as visit ,
count(s.custid) as cust, 
sum(case when s.isclosing = 1 then s.isclosing else 0 end)as orders 
from vmstrschedule s JOIN vmsmsuser u ON u.usercode = s.salesmanid 
where u.branchid = 'BLL' group by u.fullname

but it shows error like this : 但它显示如下错误:

must appear in the GROUP BY clause or be used in an aggregate function 必须出现在GROUP BY子句中或在聚合函数中使用

it works in MySQL , but when I tried it in PostgreSQL it didn't work . 它在MySQL中工作,但是当我在PostgreSQL中尝试时不起作用。 I want to display data like this , per month : 我想每月显示这样的数据:

在此处输入图片说明

but if I used this query : 但是如果我使用此查询:

select u.fullname,s.schedate,
sum(case when s.isvisiting =1 then s.isvisiting else 0 end) as visit ,
count(s.custid) as cust, 
sum(case when s.isclosing = 1 then s.isclosing else 0 end)as orders 
from vmstrschedule s JOIN vmsmsuser u ON u.usercode = s.salesmanid 
where u.branchid = 'BLL' group by u.fullname,s.schedate
order by u.fullname

it will shows data like this : 它将显示如下数据:

在此处输入图片说明

If by "works" you mean it arbitrarily selects one of the possible schedate values for each group, you're correct. 如果按“工作”的意思是它为每个组任意选择了一个可能的schedate值,那是正确的。 However, postgresql and most other SQL products want you to be explicit about what value they should select 但是,postgresql和大多数其他SQL产品希望您明确选择应选择的值

If all the values are the same within each group, putting it in either the GROUP BY clause or an arbitrary aggregate (eg MAX(s.schedate) ) should work. 如果每个组中的所有值都相同,则将其放在GROUP BY子句或任意聚合中(例如MAX(s.schedate) )应该起作用。 If the values are different, please tell the server which one it should select - the MIN , or MAX would usually be appropriate. 如果值不同,请告诉服务器应该选择哪一个MINMAX通常比较合适。

select u.fullname ,s.schedate,
sum(case when s.isvisiting =1 then s.isvisiting else 0 end) as visit ,
count(s.custid) as cust, 
sum(case when s.isclosing = 1 then s.isclosing else 0 end)as orders 
from vmstrschedule s JOIN vmsmsuser u ON u.usercode = s.salesmanid 
where u.branchid = 'BLL' group by u.fullname, s.schedate 

should work 应该管用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL:必须出现在GROUP BY子句中或在聚合函数中使用 - SQL: Must appear in the GROUP BY clause or be used in an aggregate function SQL:列必须出现在 GROUP BY 子句中或在聚合函数中使用 - SQL: Column Must Appear in the GROUP BY Clause Or Be Used in an Aggregate Function postgres-SELECT / GROUP BY中的部分列-列必须出现在GROUP BY子句中或在聚合函数中使用 - postgres - partial column in SELECT/GROUP BY - column must appear in the GROUP BY clause or be used in an aggregate function PGError:错误:列“ inboxes.id”必须出现在GROUP BY子句中或在聚合函数中使用 - PGError: ERROR: column “inboxes.id” must appear in the GROUP BY clause or be used in an aggregate function 列“d.discount_amount”必须出现在 GROUP BY 子句中或用于聚合函数中 - column “d.discount_amount” must appear in the GROUP BY clause or be used in an aggregate function 无效操作:列“[column_name]”必须出现在 GROUP BY 子句中或在聚合 function 中使用; - Invalid operation: column “[column_name]” must appear in the GROUP BY clause or be used in an aggregate function; GROUP BY子句必须与聚合函数一起使用吗? - The GROUP BY clause must be used with aggregate functions? 选定的项目不必出现在 GROUP BY 子句中或用于聚合 function - selected items don't have to appear in the GROUP BY clause or be used in an aggregate function invalid:聚合函数或GROUP BY子句 - invalid : aggregate function or the GROUP BY clause 与GROUP BY子句一起使用的MAX函数 - MAX function used with GROUP BY clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM