0

我有桌子

Order(orderid, customerid, billingcompanyname, billingfirstname,
 billinglastname, billingcountry, shipcountry, paymentamount, 
 creditcardtransactionid, orderdate, creditcardauthorizationdate, orderstatus, 
 total_payment_received, tax1_title salestax1) 

customerid是外键。

我需要计算单个客户下的订单,其中包含公司名称,尤其是输出中的所有字段。

4

4 回答 4

2

尝试这个:

SELECT o1.cnt, o2.*
FROM (
    SELECT COUNT(*) cnt, customerid FROM order GROUP BY customerid
) o1
INNER JOIN order o2 on o1.customerid = o2.customerid

甚至更好:

SELECT order.*, COUNT(*) OVER (PARTITION BY customerid) AS cnt
FROM order
于 2012-04-11T08:44:33.537 回答
1

要获得这样的结果,您必须,GROUP BYcustomerid不能orderid在结果集中,因为COUNT必须在 的不同值上运行orderid

于 2012-04-11T08:45:01.213 回答
1

您在订单表上有 companyName 字段吗?您应该在订单表上有一个公司表和一个 companyId。

无论如何,在这种情况下(如果客户总是属于同一家公司),您可以简单地执行以下操作:

select customerid, billingcompanyname, count(*)
from orders
group by customerid, billingcompanyname
于 2012-04-11T09:38:54.107 回答
0

用这个。

with customers as (select '1424' id_customer, '13-Feb-15' date_purchase, 'Petr' name_first, 'Kellner' name_last, 'Chicago' name_city from dual
union all
select '1425' id_customer, '13-Feb-15' date_purchase, 'Shelley' name_first, 'Birdwick' name_last, 'San Jose' name_city from dual
union all
select '1426' id_customer, '13-Feb-15' date_purchase, 'Morris' name_first, 'Moore' name_last, 'San Fransisco' name_city from dual
union all
select '1427' id_customer, '13-Feb-15' date_purchase, 'Shyam' name_first, 'Bajaj' name_last, 'Detroit' name_city from dual
union all
select '1428' id_customer, '13-Feb-15' date_purchase, 'Xu' name_first, 'Wang' name_last, 'New York' name_city from dual),

orders as (select '1224215' id_order, '1425' id_customer, '13-Feb-15' date_purchase, '235' amount_product, 'Name of Book' name_product from dual
union all
select '1224216' id_order, '1424' id_customer, '13-Feb-15' date_purchase, '356' amount_product, 'Name of Book' name_product from dual
union all
select '1224217' id_order, '1426' id_customer, '13-Feb-15' date_purchase, '263' amount_product, 'Name of Book' name_product from dual
union all
select '1224218' id_order, '1426' id_customer, '13-Feb-15' date_purchase, '326' amount_product, 'Name of Book' name_product from dual
union all
select '1224219' id_order, '1427' id_customer, '13-Feb-15' date_purchase, '236' amount_product, 'Name of Book' name_product from dual
union all
select '1224220' id_order, '1428' id_customer, '13-Feb-15' date_purchase, '233' amount_product, 'Name of Book' name_product from dual
union all
select '1224221' id_order, '1426' id_customer, '13-Feb-15' date_purchase, '633' amount_product, 'Name of Book' name_product from dual
union all
select '1224222' id_order, '1424' id_customer, '13-Feb-15' date_purchase, '235' amount_product, 'Name of Book' name_product from dual
union all
select '1224215' id_order, '1426' id_customer, '13-Feb-15' date_purchase, '632' amount_product, 'Name of Book' name_product from dual
union all
select '1224215' id_order, '1425' id_customer, '13-Feb-15' date_purchase, '236' amount_product, 'Name of Book' name_product from dual)

select customers.name_first, customers.name_last, count(distinct id_order) orders, sum(amount_product) total_amount
from customers left join orders on customers.id_customer = orders.id_customer group by customers.name_first, customers.name_last;
于 2015-02-16T05:13:21.520 回答