从Oracle SQL查询获取计数的计数

从Oracle SQL查询获取计数的计数,sql,oracle,Sql,Oracle,我有一个oracle SQL查询,它从不同的部门选择公司的计数 select a.cust_nm, a.cust_acct_nb, a.cust_company_nb, to_char(b.case_receive_dt, 'YYYYMM'), count(*) from custo

我有一个oracle SQL查询,它从不同的部门选择公司的计数

select 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM'), 
                 count(*)
from 
                 customer a, case b
where 
                 a.cust_nb = b.case_cust_nb
                 and a.cust_company_nb in 
                     ('01062','01602','01603','01604','01605','01606')
                 and b.case_receive_dt > sysdate -365
                 and b.case_status_cd = 'CC'
group by 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM')
order by 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM')
这会将
a.cust\u nm、a.cust\u act\u nb、a.cust\u company\u nb的
计数返回到\u char(b.case\u receive dt,'yyyyyymm')

在同一个查询中,我需要对所有
cust\u acct\u nb

例如:-

如何在同一查询中获得
最终\u总计


请帮忙

select
语句中使用子查询,如下所示:

  select a.cust_nm,
         a.cust_acct_nb,
         a.cust_company_nb,
         to_char(b.case_receive_dt, 'YYYYMM'),
         count(*),
         (select count(*)
            from customer a1, case b1
           where     a1.cust_nb = b1.case_cust_nb
                 and a1.cust_company_nb in ('01062','01602','01603','01604','01605','01606')
                 and b1.case_receive_dt > sysdate -365
                 and b1.case_status_cd = 'CC'
                 and a1.cust_acct_nb = a.cust_acct_nb)
    from customer a, case b
   where     a.cust_nb = b.case_cust_nb
         and a.cust_company_nb in ('01062','01602','01603','01604','01605','01606')
         and b.case_receive_dt > sysdate -365
         and b.case_status_cd = 'CC'
group by a.cust_nm,
         a.cust_acct_nb,
         a.cust_company_nb,
         to_char(b.case_receive_dt, 'YYYYMM')
order by a.cust_nm,
         a.cust_acct_nb,
         a.cust_company_nb,
         to_char(b.case_receive_dt, 'YYYYMM');
输出:

cust_acct_nb    cust_acct_nb    cust_acct_nb   cust_acct_nb        count(*)    Final_Total
KFC                 1               12             09-10-1991         12           32
KFC                 1               12             10-10-1991         10            32
KFC                 1               12             11-10-1991         10           32
KFC                 2               12             09-10-1991         12           37
KFC                 2               12             10-10-1991         10           37
KFC                 2               12             11-10-1991         15           37 

Oracle SQL使您可以使用汇总扩展在一个查询中查询总计和小计。以下是此类功能的示例:

SELECT fact_1_id,
   fact_2_id,
   SUM(sales_value) AS sales_value
FROM   dimension_tab
GROUP BY ROLLUP (fact_1_id, fact_2_id)
ORDER BY fact_1_id, fact_2_id;

 FACT_1_ID  FACT_2_ID SALES_VALUE
---------- ---------- -----------
     1          1     4363.55
     1          2     4794.76
     1          3     4718.25
     1          4     5387.45
     1          5     5027.34
     1               24291.35
     2          1     5652.84
     2          2     4583.02
     2          3     5555.77
     2          4     5936.67
     2          5     4508.74
     2               26237.04
                     50528.39

你可以在这里找到更多的例子

你能试试这个吗?我无法测试它,因为我没有数据设置

select 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM'), 
                 count(*)
from 
                 customer a, case b
where 
                 a.cust_nb = b.case_cust_nb
                 and a.cust_company_nb in 
                     ('01062','01602','01603','01604','01605','01606')
                 and b.case_receive_dt > sysdate -365
                 and b.case_status_cd = 'CC'
group by rollup
                 (a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM'))
order by 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM')

您可以发布一些示例post数据吗?您可以使用总数据创建另一个视图/子查询,并将其连接到实际查询中,但会有一些开销。从数据库客户端计算总计似乎更容易,使用cursorI无法使用任何客户端脚本,因为它在一个名为
Jitterbit
的ETS中使用,它只会返回结果并进行循环。我认为运行聚合就是您要寻找的:任何要做的事情都会汇总起来?这里的第一个计数在哪里?或者选择中的计数?第二个计数是卷起的,它是一个咒语。我还有一个问题,我不想按姓名订购(
cust\u acct\u nb
)。但是如果我从
GROUPBY
子句中删除它,它会发送错误,因为它在select中。我应该怎么做?如果您在查询中使用聚合函数(例如,
count
sum
,等等),则您在
select
order by
子句中引用的所有列也必须包含在
group by
子句中。通过谷歌搜索
groupby
了解有关Oracle文档的更多信息。
select 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM'), 
                 count(*)
from 
                 customer a, case b
where 
                 a.cust_nb = b.case_cust_nb
                 and a.cust_company_nb in 
                     ('01062','01602','01603','01604','01605','01606')
                 and b.case_receive_dt > sysdate -365
                 and b.case_status_cd = 'CC'
group by rollup
                 (a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM'))
order by 
                 a.cust_nm, 
                 a.cust_acct_nb, 
                 a.cust_company_nb, 
                 to_char(b.case_receive_dt, 'YYYYMM')
- Try This:

select 
             a.cust_nm,  
             a.cust_company_nb, 
             to_char(b.case_receive_dt, 'dd-mm-yyyy'),
             decode(grouping(a.cust_acct_nb),1,'Sum of group',a.cust_acct_nb), 
             count(*)
from 
             customer a, case b
where 
             a.cust_nb = b.case_cust_nb
             and a.cust_company_nb in 
                 ('01062','01602','01603','01604','01605','01606')
             and b.case_receive_dt > sysdate -365
             and b.case_status_cd = 'CC'
group by rollup(
             a.cust_nm, 
             a.cust_acct_nb, 
             a.cust_company_nb, 
             to_char(b.case_receive_dt, 'dd-mm-yyyy'));