我正在尝试按销售量添加排名,并将日期列更改为“月末”字段,该字段每个月都有一个月结束日期-这样做有意义吗? 您是要更改表格并添加列,还是要重命名日期字段并使用set和case来使所有三月日期= 3-31-18和所有四月4-30-18? 我走了这么远:
UPDATE table1
SET DATE=EOMONTH(DATE) AS MONTH_END;
ALTER TABLE table1
ADD COLUMN RANK INT AFTER sales;
UPDATE table1
SET RANK=
RANK() OVER(PARTITION BY cust ORDER BY sales DESC);
LIMIT 2
我可以连续做两套而不添加更新吗?我正在寻找每个月内排名前2位的产品-这样行得通吗?我觉得这是正确且最有效的查询,但它不起作用-感谢您提供任何帮助!
原始表
+------+----------+-------+--+
| CUST | DATE | SALES | |
+------+----------+-------+--+
| 36 | 3-5-2018 | 50 | |
| 37 | 3-15-18 | 100 | |
| 38 | 3-25-18 | 65 | |
| 37 | 4-5-18 | 95 | |
| 39 | 4-21-18 | 500 | |
| 40 | 4-45-18 | 199 | |
+------+----------+-------+--+
所需的输出
+------+-----------+-------+------+
| CUST | Month End | SALES | Rank |
+------+-----------+-------+------+
| | | | |
| 37 | 3-31-18 | 100 | 1 |
| 38 | 3-31-18 | 65 | 2 |
| 39 | 4-30-18 | 500 | 1 |
| 40 | 4-30-18 | 199 | 2 |
+------+-----------+-------+------+
答案 0 :(得分:0)
根据您的预期输出,我认为这也可能有效。
create table Salesdate (Cust int, Dates date, Sales int)
insert into Salesdate values
(36 , '2018-03-05' , 50 )
,(37 , '2018-03-15' , 100 )
,(38 , '2018-03-25' , 65 )
,(37 , '2018-04-05' , 95 )
,(40 , '2018-04-25' , 199 )
,(39 , '2018-04-21' , 500 )
将同一列日期更新到该月的最后一天(EOmonth将有助于提供该月的最后一天),您可以添加单独的列或根据需要更新该列。
Update Salesdate
set Dates = eomonth(Dates)
在表中添加一个名为rank的列。
Alter table Salesdate
add rank int
更新刚刚添加的列等级。
update Salesdate
set Salesdate.[rank] = tbl.Ranked from
(select Cust, Sales, Dates , rank() over (Partition by Dates order by Sales Desc)
Ranked from Salesdate ) tbl
where tbl.Cust = salesdate.Cust
and tbl.Sales = salesdate.Sales
and tbl.dates = salesdate.Dates
-如果希望最终表仅具有1和2的等级,则不确定是否需要执行此步骤,然后可以删除数据。或者也可以仅在选择列表上将其过滤掉。请注意,如果我们没有给定客户的唯一销售金额集,那么有时rank可能会跳过该数字。
;With cte as (
select * from Salesdate)
delete from cte
where [RANK] > 2
select * from Salesdate
order by dates, [RANK]
输出
Cust Dates Sales rank
37 2018-03-31 100 1
38 2018-03-31 65 2
39 2018-04-30 500 1
40 2018-04-30 199 2