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

问题描述 投票:0回答:5

我有桌子

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

customerid 是一个外键。

我需要计算单个客户在输出中使用公司名称和所有字段所下的订单。

sql sql-server sql-server-2008 window-functions
5个回答
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
投票

要获得这样的结果,您必须

GROUP BY
customerid
但您不能在结果集中使用
orderid
,因为
COUNT
必须在
orderid
的不同值上运行。


1
投票

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

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

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

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;

0
投票

使用 SQL

要计算单个客户下的订单,我们可以使用 windows 函数应用 count() 操作,这将给出我们想要的输出: 让我们试试这个: 正如在输入中我们有 customer_id 是 FK.

计算单个客户下的订单:

select cust_id,cnt as orders_cnt,
dense_rank() over(oreder by cnt as desc) as drnk 
from (select order_id,cust_id,count(*) over(partition by 
cust_id) as cnt from orders_table) as x;

以计数/最大订单列出已订购订单的客户详细信息:

select * from 
(select cust_id,cnt,dense_rank() over(order by cnt desc) as drnk from
(select order_id,cust_id,count(*) over(partition by cust_id) as cnt from
orders_table) as x) as xx
join customer_table c
on c.cust_id = xx.cust_id where drnk = 1
© www.soinside.com 2019 - 2024. All rights reserved.