简体   繁体   中英

find total number of sold items per saleperson per shop within specific date range

I am trying to find total number of sold items per saleperson per shop withing a datetime range

The saleperson can be moved from one shop to another shop ( as all shops owned by a single person).

sale person can be mapped to n number of shops but at current moment he should be assigned to only one shop

take for example 'John' with sale_person_id = 1 worked in the shop_id = 1 for the period from '2015-06-01 08:00:00' to '2015-06-01 13:00:00' but now he is working in the shop_id = 2 and he is still working there as his end_working_date is NULL

the output should be as :

sale_person_login_id    shop_name   count(*)
John                    Shop1             2
John                    Shop2             3
David                   Shop2             1
Jenna                   Shop3             1

here is the sqlfiddle http://sqlfiddle.com/#!9/a8d2e/3

because during the period '2015-06-01 08:00' AND '2015-06-01 18:00' John worked for 2 shops which are shop1 and shop2 , he sold 2 items when he was working in shop1 and he sold 3 items when he is in shop2

but the problem is that am getting the output as

sale_person_login_id    shop_name   count(*)
    John    Shop1   5
    John    Shop2   5
    David   Shop2   1
    Jenna   Shop3   1

Here is the query I am using

select sale_person.sale_person_login_id, shop.shop_name ,count(*) from sale 
join sale_person on 
sale.sale_person_id = sale_person.sale_person_id 
join sale_person_shop 
on sale_person_shop.sale_person_id = sale.sale_person_id 
join shop on shop.shop_id = sale_person_shop.shop_id 
WHERE sale.sale_date BETWEEN '2015-06-01 08:00' AND '2015-06-01 18:00'
GROUP BY sale_person.sale_person_login_id, shop.shop_name ;

Your query isn't relating a sale to a shop . That is, your query doesn't know which shop made a sale , so it's matching the sale to each row from user_shop .

Also, it's 2015 already... time to ditch the old-school comma syntax for the join operation (use the JOIN keyword), and move the join predicates to an ON clause.

 SELECT u.login_id
      , shop.shop_name
      , COUNT(*)
   FROM sale s
   JOIN user u
     ON u.user_id = s.user_id
   JOIN shop
     ON shop.shop_id = s.shop_id    -- this condition is missing from your query
   JOIN user_shop us
     ON us.shop_id = shop.shop_id
    AND us.user_id = u.user_id 
  WHERE s.sale_date BETWEEN '2015-06-01 08:00' AND '2015-06-01 18:00'
  GROUP BY s.user_id, shop.shop_name

Actually, I don't think the user_shop table needs to be involved in the query, assuming that (user_id,shop_id) is unique, and the purpose isn't to restrict some rows from sale being returned. The user_shop table could be omitted from the query:

 SELECT u.login_id
      , shop.shop_name
      , COUNT(*)
   FROM sale s
   JOIN user u
     ON u.user_id = s.user_id
   JOIN shop
     ON shop.shop_id = s.shop_id    -- this condition is missing from your query
  WHERE s.sale_date BETWEEN '2015-06-01 08:00' AND '2015-06-01 18:00'
  GROUP BY s.user_id, shop.shop_name

A sale is made to(by?) a user , and a sale is made at(by?) a shop .

But a user can have sale s at(by) more than one shop . Which is why the original query is returning inflated counts.

I think you're looking for something like this - although I'm not quite sure of the relationship between a 'sale' and an 'item'...

SELECT u.login_id 
     , s.shop_name
     , COUNT(l.sale_id) total_sales
  FROM user u
  JOIN user_shop us
    ON us.user_id = u.user_id
  JOIN shop s
    ON s.shop_id= us.shop_id
  JOIN sale l
    ON l.user_id = us.user_id
   AND l.sale_date BETWEEN us.start_working_date AND COALESCE(us.end_working_date,CURDATE())
 WHERE l.sale_date BETWEEN '2015-06-01 08:00:00' AND '2015-06-01 18:00:00'
 GROUP
    BY u.login_id
     , s.shop_name;

http://sqlfiddle.com/#!9/0cc87/20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM