带有 ORDER BY 的 SELECT 语句
我有一个简单的 select 语句,在 oracle 中有一个连接,我有一个聚合函数,比如 sum(COLUMN) ,我的查询工作正常并且返回结果,现在我从同一个表中添加一到两列,我有一个连接,我需要简单的列值,没有聚合函数,当我添加第一列时,它询问我它需要在分组依据中,因为它不是聚合函数,我这样做了,但是在这样做之后,我得到了更多的行数以前什么可能是这个原因,当我再添加一列时,行数会增加,就像我的第一个查询一样,没有添加这两列,结果是 544,现在是 766
select distinct 'carTypes' as type, mi.WOMENNAME as "mother name", mi.womencnic as "mother CNIC", '' as BFORMNO, '' as child_gender, mi.PROVINCE, mi.district, mi.tehsil, mi.HUSBANDNAME, mi.PHONENO contact, mi.address, SUM(p.amount) paid_amount
from MINFORMATION mi
inner join PAYMENTINFORMATION p on p.BENEFICIARYID=mi.WOMENID and p.BANKSTATUS='Successfull'
where mi.month IS NOT NULL
and p.generationdate between '24-JUN-21' and '01-JAN-22'
group by mi.WOMENNAME, mi.womencnic,mi.PROVINCE, mi.district, mi.tehsil, mi.HUSBANDNAME, mi.PHONENO , mi.address
上面是我的查询
I have a simple select statement with joins in oracle I have one agregate function like sum(COLUMN) , my query works fine and it return the result , Now I am adding one-two more columns from same table to which I have a join , I am requiring simple column values no agregate function when I add first column it ask me that it needed to be in group by as it is not agregate function I did that , but after doing that i am getting more number of rows which i were getting previously what could be the reason, when I add one more columns than again number of rows increased like with my first query without adding these two columns the result was 544 and now it is 766
select distinct 'carTypes' as type, mi.WOMENNAME as "mother name", mi.womencnic as "mother CNIC", '' as BFORMNO, '' as child_gender, mi.PROVINCE, mi.district, mi.tehsil, mi.HUSBANDNAME, mi.PHONENO contact, mi.address, SUM(p.amount) paid_amount
from MINFORMATION mi
inner join PAYMENTINFORMATION p on p.BENEFICIARYID=mi.WOMENID and p.BANKSTATUS='Successfull'
where mi.month IS NOT NULL
and p.generationdate between '24-JUN-21' and '01-JAN-22'
group by mi.WOMENNAME, mi.womencnic,mi.PROVINCE, mi.district, mi.tehsil, mi.HUSBANDNAME, mi.PHONENO , mi.address
above is my query
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事情就是这样。当
select
列列表中有聚合函数时,所有非聚合列必须包含在group by
子句中(group by
,而不是 < code>order by 正如您的标题所示;order by
与此无关)。例如,根据 Scott 的
emp
表:如果要计算工资总和,结果只有一行:如果添加
deptno
列,则意味着您要计算每个部门的工资总和。由于有来自 3 个部门的员工,因此会产生 3 行:添加另一列 -
job
- 意味着您要计算每个部门以及该部门内每个职位的工资总和;所有这些都会增加结果行数:因此,您希望获得什么样的输出?如果你能解释一下,也许我们可以建议实现这一目标的方法。
That's how it goes. When there's an aggregate function in the
select
column list all non-aggregated columns must be included in thegroup by
clause (group by
, notorder by
as your title suggests;order by
is irrelevant in this matter).For example, as of Scott's
emp
table: if you want to compute sum of salaries, the result is only one row:If you add
deptno
column, it means you want to compute sum of salaries per each department. As there are employees from 3 departments, it results in 3 rows:Adding yet another column -
job
- means that you want to calculate sum of salaries per each department, and per each job within that department; all that increases number of resulting rows:Therefore, what kind of output would you want to get? If you could explain it, maybe we could suggest approach that would make it happen.