SQL用于条件值排名

时间:2019-01-19 22:19:44

标签: sql conditional ms-access-2010 ranking ranking-functions

我正试图在此表中添加一列:

from bs4 import BeautifulSoup
import requests
import time

url = 'some url'
r = requests.get(url)
html = r.text
soup = BeautifulSoup(html, 'lxml')
images = []
for img in soup.findAll('img', {'class': '_images'}):
    images.append(img.get('data-url'));

for i in range(len(images)):
    s = images[i]
    cutname = s.split("/")[-1]
    filename = cutname[:cutname.find("?")]
    f = open(filename,'wb') 
    f.write((requests.get(s)).content)
    f.close()
    time.sleep(0.5)

...的输出为:

CREATE TABLE #NEW_HIGH (SYMBOL char(4) not null, CLOSE_DATE date not null, CLOSE_PRICE money)
INSERT INTO #NEW_HIGH VALUES ('A','01/07/2019',31.46)
INSERT INTO #NEW_HIGH VALUES ('A','01/08/2019',32.5)
INSERT INTO #NEW_HIGH VALUES ('A','01/09/2019',34.94)
INSERT INTO #NEW_HIGH VALUES ('A','01/10/2019',34.95)
INSERT INTO #NEW_HIGH VALUES ('A','01/11/2019',34.57)
INSERT INTO #NEW_HIGH VALUES ('B','01/07/2019',12.21)
INSERT INTO #NEW_HIGH VALUES ('B','01/08/2019',11.88)
INSERT INTO #NEW_HIGH VALUES ('B','01/09/2019',12.25)
INSERT INTO #NEW_HIGH VALUES ('B','01/10/2019',12.30)
INSERT INTO #NEW_HIGH VALUES ('B','01/11/2019',11.26)
INSERT INTO #NEW_HIGH VALUES ('C','01/07/2019',22.99)
INSERT INTO #NEW_HIGH VALUES ('C','01/08/2019',22.95)
INSERT INTO #NEW_HIGH VALUES ('C','01/09/2019',22.14)
INSERT INTO #NEW_HIGH VALUES ('C','01/10/2019',23.09)
INSERT INTO #NEW_HIGH VALUES ('C','01/11/2019',22.91)

基本上,我正在尝试添加一个名为“ RANK_BY_NEW_HI”的第四列,该列从第1行的值1开始,每当下一个CLOSE_PRICE记录(以SYMBOL和DATE顺序排列)较高时,就增加1,并将其重置为1每当它等于或低于上一个CLOSE_PRICE记录,或开始一个新的符号。

我花了几天的时间来解决这个问题,因此我们将不胜感激!

1 个答案:

答案 0 :(得分:2)

这在SQL Server中要简单得多。您可以使用lag()来确定峰,使用累积和来确定结果组,然后使用row_number()

select nh.*, row_number() over (partition by symbol, high_grp order by close_date) as rank_by_new_high
from (select nh.*,
             sum(case when prev_cp < close_price then 0 else 1 end) over (partition by nh.symbol order by nh.close_date) as high_grp
      from (select nh.*,
                   lag(nh.close_price) over (partition by nh.symbol order by nh.close_date) as prev_cp
            from #new_high nh
           ) nh
     ) nh
order by symbol, close_date;

Here是db <>小提琴。

您并不是要征求我的意见,但我建议您在SQL Server而不是MS Access中进行工作。它是功能更强大的数据库。