带有 ORDER BY 的 SELECT 语句

发布于 2025-01-14 21:06:34 字数 841 浏览 5 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2025-01-21 21:06:34

事情就是这样。当 select 列列表中有聚合函数时,所有非聚合列必须包含在 group by 子句中(group by,而不是 < code>order by 正如您的标题所示;order by 与此无关)。

例如,根据 Scott 的 emp 表:如果要计算工资总和,结果只有一行:

SQL> select sum(sal)
  2  from emp;

  SUM(SAL)
----------
     29025

如果添加 deptno 列,则意味着您要计算每个部门的工资总和。由于有来自 3 个部门的员工,因此会产生 3 行:

SQL> select deptno, sum(sal)
  2  from emp
  3  group by deptno;

    DEPTNO   SUM(SAL)
---------- ----------
        30       9400
        20      10875
        10       8750

添加另一列 - job - 意味着您要计算每个部门以及该部门内每个职位的工资总和;所有这些都会增加结果行数:

SQL> select deptno, job, sum(sal)
  2  from emp
  3  group by deptno, job
  4  order by deptno, job;

    DEPTNO JOB         SUM(SAL)
---------- --------- ----------
        10 CLERK           1300
        10 MANAGER         2450
        10 PRESIDENT       5000
        20 ANALYST         6000
        20 CLERK           1900
        20 MANAGER         2975
        30 CLERK            950
        30 MANAGER         2850
        30 SALESMAN        5600

9 rows selected.

因此,您希望获得什么样的输出?如果你能解释一下,也许我们可以建议实现这一目标的方法。

That's how it goes. When there's an aggregate function in the select column list all non-aggregated columns must be included in the group by clause (group by, not order 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:

SQL> select sum(sal)
  2  from emp;

  SUM(SAL)
----------
     29025

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:

SQL> select deptno, sum(sal)
  2  from emp
  3  group by deptno;

    DEPTNO   SUM(SAL)
---------- ----------
        30       9400
        20      10875
        10       8750

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:

SQL> select deptno, job, sum(sal)
  2  from emp
  3  group by deptno, job
  4  order by deptno, job;

    DEPTNO JOB         SUM(SAL)
---------- --------- ----------
        10 CLERK           1300
        10 MANAGER         2450
        10 PRESIDENT       5000
        20 ANALYST         6000
        20 CLERK           1900
        20 MANAGER         2975
        30 CLERK            950
        30 MANAGER         2850
        30 SALESMAN        5600

9 rows selected.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文