根据之前和当前的销售列

时间:2016-12-07 06:03:42

标签: mysql

我有一个查询,在该查询中我总结了一个特定的列。

我希望根据总和列获得之前的排名和当前排名。

这是我的mysql代码

SELECT m.name as name, 
       m.address as address, 
       ROUND(SUM(l.retailer_price),2) as sales 
from members m 
INNER JOIN loading l 
ON m.member_id = l.member_id 
WHERE l.brand_id = '07275324100582216' 
and microtime between unix_timestamp('2016-08-12 00:00:00') 
and unix_timestamp('2016-08-12 23:59:59') 
GROUP by m.name ORDER BY sales desc

我需要一个像这样的结果

| Prev.Rank | Pres.Rank |  Name  |  Prev.Sales |  Pres.Sales  | S.Diff |
------------------------------------------------------------------------
|     2     |    1      |   Joe  |  $20.00     | $23.00       | $3.00  |
------------------------------------------------------------------------
|     5     |    2      | Grace  |  $11.00     |  $20.00      | $9.00  |
------------------------------------------------------------------------
|     3     |    3      | Pat    |  $10.50     |  $10.50      | $0.00  |
------------------------------------------------------------------------
|     1     |    4      | May    |  $50.00     |  $10.00      | $40.00 |
------------------------------------------------------------------------
|     4     |    5      | Daisy  |  $10.75     |  $9.00       | $1.75  |
------------------------------------------------------------------------


SELECT `Member`.`member_id`, 
       `Member`.`name`, 
       Sum(CASE 
             WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-06' 
           THEN 
             ROUND(SUM(`Sale`.`retailer_price`),2) 
             ELSE 0 
           end)                      AS `Previous Sale`, 
       Sum(CASE 
             WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-07' 
           THEN 
             ROUND(SUM(`Sale`.`retailer_price`),2) 
             ELSE 0 
           end)                      AS `Current Sale`, 
       Abs(Sum(CASE 
                 WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-06' 
               THEN 
                 ROUND(SUM(`Sale`.`retailer_price`),2) 
                 ELSE 0 
               end) - Abs(Sum(CASE 
                                WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") 
                                     = 
                                     '2016-12-07' THEN 
                                ROUND(SUM(`Sale`.`retailer_price`),2) 
                                ELSE 0 
                              end))) AS `Sales Diff` 
FROM   `members` `Member` 
       JOIN `loading` `Sale` 
         ON `Sale`.`member_id` = `Member`.`member_id` 
GROUP  BY `Member`.`member_id` 

2 个答案:

答案 0 :(得分:0)

首先使用一个日期的值创建一个表并添加一个排名,然后创建一个相同的表,其中包含此日期之前的日期以及排名。然后加入它们并只选择您需要的列。

这样的事情:

SELECT * 
FROM 
  (
    SELECT name, @i := @i + 1 AS rank, sales, microtime 
    FROM `sales` as s 
    JOIN (SELECT @i := 0) r 
    WHERE microtime = unix_timestamp('2016-08-12 00:00:00')
    ORDER BY sales DESC
  ) as s1,
  (
    SELECT name, @j := @j + 1 AS rank, sales, microtime 
    FROM `sales` as s 
    JOIN (SELECT @j := 0) r 
    WHERE microtime = unix_timestamp('2016-08-11 00:00:00')
    ORDER BY sales DESC
  ) as s2 
WHERE s1.name = s2.name

答案 1 :(得分:0)

尝试此查询

SELECT 
    `Member`.`member_id`, 
    `Member`.`name`, 
    SUM(CASE 
           WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-06' 
              THEN `Sale`.`retailer_price` 
              ELSE 0 
        END) AS `Previous Sale`, 
    SUM(CASE 
           WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-07' 
              THEN `Sale`.`retailer_price` 
              ELSE 0 
        END) AS `Current Sale`, 
    ABS(SUM(CASE 
               WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-06' 
                  THEN `Sale`.`retailer_price` 
                  ELSE 0 
            END) - ABS(SUM(CASE 
                              WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-07' 
                                 THEN `Sale`.`retailer_price` 
                                 ELSE 0 
                           END))) AS `Sales Diff` 
FROM   
    `members` `Member` 
LEFT JOIN 
    `loading` `Sale  ON `Sale`.`member_id` = `Member`.`member_id` 
GROUP BY 
    `Member`.`member_id` 
ORDER BY 
    SUM(CASE 
           WHEN Date_format(`Sale`.`microtime`, "%Y-%m-%d") = '2016-12-07' 
              THEN `Sale`.`retailer_price` 
              ELSE 0 
        END) DESC 

enter image description here

enter image description here