Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 有计数子查询_Sql_Oracle - Fatal编程技术网

Sql 有计数子查询

Sql 有计数子查询,sql,oracle,Sql,Oracle,我试图得到一些查询,但我有我的计数问题,它说我缺少一个表达式,请帮助 select d.kode, rpad(d.nama,75,' ') as "NAMA", lpad(count(th.fk_distributor),10,' ') as "JUMLAH" from mh_distributor d join th_beli th on th.fk_distributor = d.kode having count(td.fk_produk) > select avg("JUMLAH

我试图得到一些查询,但我有我的计数问题,它说我缺少一个表达式,请帮助

select d.kode, rpad(d.nama,75,' ') as "NAMA", lpad(count(th.fk_distributor),10,' ') as "JUMLAH"
from mh_distributor d 
join th_beli th on th.fk_distributor = d.kode
having count(td.fk_produk) > select avg("JUMLAH") as "AVERAGE" from(
    select d.nama,count(th.fk_distributor) as "JUMLAH"
    from mh_distributor d
    join th_beli th on th.fk_distributor = d.kode
    group by d.nama
)
group by d.nama, td.fk_produk, d.kode
order by d.kode asc;

这就是我的查询

只需将第二条
选择
语句与子查询打包即可:

select avg("JUMLAH") 
from(select d.nama,count(th.fk_distributor) as "JUMLAH"
     from mh_distributor d join 
          th_beli th on th.fk_distributor = d.kode
     group by d.nama
    ) t;
因此,您的完整声明如下:

select d.kode, rpad(d.nama,75,' ') as "NAMA", 
      lpad(count(th.fk_distributor),10,' ') as "JUMLAH"
from mh_distributor d join 
     th_beli th 
     on th.fk_distributor = d.kode
having count(td.fk_produk) > (select avg("JUMLAH") 
                              from(select d.nama,count(th.fk_distributor) as "JUMLAH"
                                   from mh_distributor d join 
                                        th_beli th on th.fk_distributor = d.kode
                                   group by d.nama
                                 ) t
                             )
group by d.nama, td.fk_produk, d.kode
order by d.kode asc;
使用窗口功能

select nk.*
from (select d.kode, d.nama, count(*) as jumlah,
             avg(count(*)) over (partition by d.nama) as avg_jumlah
      from mh_distributor d join
           th_beli th
           on th.fk_distributor = d.kode
      group by d.nama, td.fk_produk, d.kode
     ) nk
where jumlah > avg_jumlah;
我省略了对字符串的转换,因为我认为这只会使逻辑更难遵循——当然,将其添加回特定用例的外部查询中


这应该比复杂的
having
子句有更好的性能。

我的select平均值及其子查询工作正常,但它说我在having count行中缺少了一个表达式hello!你能加上你面临的错误吗?我想知道答案,但它和我在上面写的一样,所以这就是你的意思