[英]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. 如果值不同,请告诉服务器应该选择哪一个MIN
或MAX
通常比较合适。
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.