为什么我的明显不工作? SQL Developer

时间:2015-11-09 19:47:31

标签: mysql sql oracle-sqldeveloper

select distinct(caa.account_name), count(ool.ordered_item), caa.account_number
from oe_order_lines_all ool, hz_cust_accounts_all caa, oe_order_headers_all hdr
where trunc(hdr.creation_date) between '2015-10-01' and '2015-10-31'
and hdr.orig_sys_document_ref = ool.orig_sys_document_ref
and caa.cust_account_id = ool.sold_to_org_id
group by caa.account_name, ool.ordered_item, caa.account_number
order by count(ool.ordered_item) desc

我正在尝试查找哪个帐户名称订购了最有序的商品。 当我运行它时,它返回值但不具有不同的帐户名称。它将为亚马逊155,亚马逊200而不是一个值(亚马逊355)。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

在为account_name设置distinct时尝试不使用不同的列:

select distinct
    caa.account_name
from
    oe_order_lines_all ool,
    hz_cust_accounts_all caa,
    oe_order_headers_all hdr
where
    trunc(hdr.creation_date) between '2015-10-01' and '2015-10-31' and hdr.orig_sys_document_ref = ool.orig_sys_document_ref and caa.cust_account_id = ool.sold_to_org_id
group by caa.account_name , ool.ordered_item , caa.account_number
order by count(ool.ordered_item) desc

答案 1 :(得分:1)

的作用:

select caa.account_name, count(ool.ordered_item)
from oe_order_lines_all ool, hz_cust_accounts_all caa, oe_order_headers_all hdr
where trunc(hdr.creation_date) between '2015-10-01' and '2015-10-31'
and hdr.orig_sys_document_ref = ool.orig_sys_document_ref
and caa.cust_account_id = ool.sold_to_org_id
group by caa.account_name
order by count(ool.ordered_item) desc

给你想要你想要的?