2016-12-07 65 views
0

我有一個查詢,在該查詢中,我總結了一個特定的列。根據以前和現在的銷售列創建2排列

我想根據總計的列獲得以前的排名和當前的排名。

這裏是我的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` 
+0

是你每天的排名? – Beginner

+0

是的。日常。它今天和昨天使用。 – Ligthers

+0

是每個員工每日總銷售額的排名? – Beginner

回答

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 
0

試試這個queury

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

+0

沒有工作。擡頭。在我的查詢中只有2個表。加載和成員。 member_id連接兩個表。 IN loading我總結了某個member_id – Ligthers

+0

的所有零售價格,您能否顯示結果。 – Beginner

+0

#1146 - 表'xxxxxx.sales'不存在 – Ligthers

相關問題