2013-09-06 4 views
0

하루에 몇 번이나 주문했는지 알고 싶습니다. 그러나 취소 된 주문은 제외하고 싶습니다.1 일 이상 주문한 고객을 일별로 그룹화하십시오.

  • 나는 고객

  • 내가 주문과 customer_order 테이블이 (주문이 을 취소하는 경우는 customer_order 테이블에 유지)

  • 은 내가 order_item 테이블이있는 고객 테이블이 (원래 주문이 있고 또한 취소 주문, 취소 된 주문은 새로운 신용 주문을받으며 원래 주문 번호 은 신용 주문의 id_credit_order 행에 표시됩니다)

나는 이런 식으로 뭔가 싶어 :

date | no of customers with 1 order | no of customers with 2 orders | no of customers with 3 order | etc. 

을하지만 원래 주문이 취소 된 경우 나는 어떤 수를 싶지!

나는이 쿼리를 가지고 있지만, 확실히 충분하지는 않습니다. 누군가 내 결과를 얻는 방법을 알고 있습니까? 감사!

SELECT DATE(co.date_order), COUNT(co.id_customer) 
FROM customer_order co 
GROUP BY DATE(co.date_order) 
ORDER BY DATE(co.date_order) DESC; 

답변

0

HAVING 절을 사용해보십시오.

SELECT DATE(co.date_order), COUNT(co.id_customer) 
FROM customer_order co 
WHERE co.Cancelled = 0 
GROUP BY DATE(co.date_order) 
HAVING COUNT(co.id_customer) >= 1 
ORDER BY DATE(co.date_order) DESC; 
0

이 기능을 사용해보십시오. 주문 취소 방법을 반영하여 WHERE를 변경하십시오. 먼저 당신은 1, 2, 3 ... 주문 각각의 고객의 수를 요청, 이상의 주문 고객의 번호를 요청할로

SELECT DATE(co.date_order), COUNT(co.id_customer) 
FROM customer_order co 
WHERE co.cancelled = 0 
GROUP BY DATE(co.date_order) 
HAVING COUNT(co.id_customer) > 0 
ORDER BY DATE(co.date_order) DESC; 
1

문제는 약간 잘못된 것입니다.

여기에 숫자를 알려주지 만 피벗 해제 된 것이 있습니다. o.id에 대해 오른쪽 열 이름을 입력해야합니다.

Select -- outer query takes each order count and counts how many customers have that many 
    co.date_order, 
    co.customer_order_count, 
    count(*) as customer_count 
From (
    Select -- inner query counts how many valid orders each customer has 
     o.date_order, 
     o.id_customer, 
     count(*) as customer_order_count 
    From 
     customer_order o 
    Where 
     o.id_credit_order is null and -- rule out credit orders 
     not exists (-- rule out orders with related credit orders 
      select 
       'x' 
      from 
       customer_order c 
      where 
       c.id_credit_order = o.id -- column name isn't mentioned in the question 
     ) 
    Group By 
     o.date_order, 
     o.id_customer 
    ) co 
Group By 
    co.date_order, 
    co.customer_order_count 
Order By 
    co.date_order desc, 
    co.customer_order_count