如何统计个人客户的订单?

时间:2012-04-11 08:40:14

标签: sql sql-server sql-server-2008 window-functions

我有桌子

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

customerid 是外键。

我需要计算具有公司名称的个别客户的订单以及输出中的所有字段。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

要获得此类结果,您必须GROUP BY customerid,但您不能在结果集中包含orderid,因为COUNT必须在不同的值上运行orderid

答案 2 :(得分:1)

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

无论如何,在这种情况下(如果客户总是属于同一家公司),您可以这样做:

select customerid, billingcompanyname, count(*)
from orders
group by customerid, billingcompanyname

答案 3 :(得分: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;