MYSQL按性能

时间:2017-11-09 03:31:05

标签: mysql

我不是MySQL的专家,今天已证明这一点很明显,但我需要帮助按销售顺序订购用户。

我有两张桌子。一个叫用户

+------------------------+
+ id | fname | lname     +
+------------------------+
+  1 | bob   | french    +
+  2 | fred  | smith     +
+  3 | ted   | nugent    +
+  4 | kyle  | frank     +
+------------------------+

和另一个销售

+------------------------------------------------------------+
+ id | date        | commission  | lister      | seller      +
+------------------------------------------------------------+
+  1 | 2017-11-01  | 2200        | 2           | 2           +
+  2 | 2018-01-15  | 1800        | 1           | 1           +
+  3 | 2017-11-07  | 3600        | 2           | 1           +
+  4 | 2017-11-30  | 1252        | 4           | 1           +
+------------------------------------------------------------+

佣金由卖家和卖家分成50/50。 列表和卖家列对应于用户ID

我需要找到两件事。

A)一个人(例如Bob French)他的佣金总额,这个月的销售额

因此鲍勃应该拥有销售额#3的50%和销售#4的50%。

销售#2不在本月

#p>所以销售#3(1800)的一半和销售#3(626)的一半是2426

这个月的排名应该

  1. Fred @ 4000总佣金
  2. Bob @ 2426总佣金
  3. Kyle @ 626总佣金
  4. Ted @ 0总佣金
  5. 本月我需要返回RANK(在这种情况下为2)和TOTAL COMMISSION(在这种情况下为2426)(本例中为bob)(本例中为11月)

    B)我需要在不同的声明中显示整个上表(对于主管来说,看到每个人都排名。用户只能匿名查看他们的排名。

    对于两者,它可能是相同的SQL查询,然后我只是从结果集中通过id从用户中提取用户,除非有更有效的方法。

    我尝试了什么

    SELECT x.id, x.fname, x.lname, y.commission,
    FIND_IN_SET( commission , ( 
        SELECT GROUP_CONCAT( commission ORDER BY commission DESC )
        FROM sales
    ) ) AS rank
    
    FROM users x
    JOIN sales y ON x.id IN (lister, seller)
    ORDER BY rank ASC
    

    这是有效的,但不限于月份。我试过添加

    WHERE (date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
    

    但似乎没有效果,因为我尝试将日期限制在2010年,结果无论如何都会回来。

    此外,上述查询是由获得单一最高佣金的人返回RANK,而不是他们的佣金总和。

    请帮忙。

2 个答案:

答案 0 :(得分:1)

您可以使用UNION ALL

select x.id, x.name, sum(commision) as commision from 
(select a.date as date, b.id as id, b.fname as name, a.commision/2 as commision
from sales a join users b
on a.lister = b.id
union all
select a.date as date, b.id as id, b.fname as name, a.commision/2 as commision
from sales a join users b
on a.seller = b.id) as x
where x.date between '2017-11-01' and '2017-11-30'
group by x.id, x.name

这里的小提琴:http://sqlfiddle.com/#!9/8ae69a/7

对于您的过滤器WHERE和订购,您可以添加:

select x.id, x.name, sum(commision) as commision from 
(select a.date as date, b.id as id, b.fname as name, a.commision/2 as commision
from sales a join users b
on a.lister = b.id
union all
select a.date as date, b.id as id, b.fname as name, a.commision/2 as commision
from sales a join users b
on a.seller = b.id) as x
where (x.date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
group by x.id, x.name
order by commision

- 编辑 -

添加排名:(小提琴:http://sqlfiddle.com/#!9/8ae69a/30

SET @rank = 0;

SELECT @rank := @rank + 1 AS rank,
       y.id,
       y.name,
       y.commision
FROM   (SELECT x.id,
               x.name,
               Sum(commision) AS commision
        FROM   (SELECT a.date          AS date,
                       b.id            AS id,
                       b.fname         AS name,
                       a.commision / 2 AS commision
                FROM   sales a
                       JOIN users b
                         ON a.lister = b.id
                UNION ALL
                SELECT a.date          AS date,
                       b.id            AS id,
                       b.fname         AS name,
                       a.commision / 2 AS commision
                FROM   sales a
                       JOIN users b
                         ON a.seller = b.id) AS x
        WHERE  ( x.date BETWEEN Date_format(Now(), '%Y-%m-01') AND Now() )
        GROUP  BY x.id,
                  x.name
        ORDER  BY x.commision) AS y  

答案 1 :(得分:0)

选择@a:= @ a + 1作为排名,' @',u.fname,COALESCE(总和(佣金),0)佣金,'总佣金' from(SELECT sub.dates,(commission * 2)commission,sub.lister as id FROM(SELECT s.dates,(s.commission / 4)as commission,s.lister,(select @a:= 0)rank FROM sales s where s.dates 在2017-01-01'之间和' 2017-12-31')sub 联合所有 SELECT sub.dates,(佣金* 2)卖方,sub.seller FROM (SELECT s.dates,(s.commission / 4)as commission,s.sellerFROM sales s where s.dates 在2017-01-01'之间和' 2017-12-31')sub)main 在u.id = main.id上加入用户u 按sum(佣金)desc

分组