我有一個查詢,在該查詢中,我總結了一個特定的列。根據以前和現在的銷售列創建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`
是你每天的排名? – Beginner
是的。日常。它今天和昨天使用。 – Ligthers
是每個員工每日總銷售額的排名? – Beginner