使用LEAD获取下一个日期

时间:2018-03-20 12:39:33

标签: sql sql-server lag lead

我需要创建一个回答以下问题的查询:

我想返回阅读日期为26天的注册数量,阅读日期为27天的条目数量,最多为31天,最近6个月。我需要你这样:

我的预期输出是:

No Event

(然后去......)

但我的表是这样的:

import pandas as pd
import numpy as np
input_data = {'day': ['1/1/2017', '1/4/2017', '1/5/2017', '1/6/2017', 
                         '1/7/2017', '1/8/2017', '1/9/2017', '1/10/2017', '1/11/2017'],
             'temperature': ['32 F', -99999, 28, -99999, '32 C', '38 F', 35, 34, 40],
             'windspeed': ['6 mph', 9, -99999, 7, -88888, -99999, '10 kmph', 8, 12],
             'event': ['Rain', 'Sunny', 'Snow', 0, 'Rain', 'Sunny', 0, 'Cloudy', 'Sunny']
      }
weather2 = pd.DataFrame(input_data)
new_weather = weather2.replace({"temperature":{-99999: np.NaN,
                                          -88888: np.NaN,
                                          '[A-Za-z]':''},
                            "windspeed":{-99999: np.NaN,
                                          -88888: np.NaN,
                                          '[A-Za-z]':''},
                            "event":{0:'No Event'}},regex=True)

我尝试的sql是这个(请抽象连接):

LOCALE   COMPETENCE    Reading_date(DAYS)     INSCRIPTION (Quantity)
------    ----         ----------             ---------
CEARA    JAN18          26                      20
CEARA    JAN18          27                      02
CEARA    JAN18          28                      34
CEARA    JAN18          29                      07
CEARA    JAN18          30                      12
CEARA    JAN18          31                      11
CEARA    FEV18          26                      21
CEARA    FEV18          27                      09
有人帮帮我吗?谢谢,抱歉英文不好

1 个答案:

答案 0 :(得分:1)

我发表了评论,但我也会将其作为答案发布。错误非常自我解释:

  

消息207,级别16,状态1,行27无效的列名称“next_lecture”。

表格中没有列next_lecture。那是因为,就像我在评论中说的那样:

  

您试图通过它的别名引用SELECT中的其他字段;你不能这样做。

唯一这个地方你可以通过它在查询本身中的别名来引用一个字段(所以不在它之外,例如在使用子查询时)在ORDER BY子句中;其他任何地方都会导致错误。所以,像这样的简单陈述将失败:

SELECT 1 + 1 AS Two, Two * 2 AS Four;

您必须提供完整的表达式,因此:

SELECT 1 + 1 AS Two, (1 + 1) * 2 AS Four;    

结果您的查询变为:

SELECT DISTINCT TOP 10
       LOCALE,
       COMPETENCE
       LEAD(Reading_date, 1) OVER (PARTITION by Reading_date ORDER BY Reading_date) AS next_lecture
       DATEDIFF(DAY, Reading_date, LEAD(Reading_date, 1) OVER (PARTITION by Reading_date ORDER BY Reading_date)) Reading_date
       INSCRIPTION
FROM BASE o
     JOIN LOCALE l ON u.localidade = l.cod_localidade;

注意我还重新调整了GROUP BY并使用了DISTINCT。您的查询中没有聚合函数,因此我假设就是您尝试使用它时所做的。

编辑:随机猜测OP想要什么,但它没有到达那里。我已经添加了这个作为参考,但是,事情还不太清楚:

WITH VTE AS (
    SELECT INSCRIP,COMPETENCE,CONVERT(date,reading_date) AS Reading_date
    FROM (VALUES('00183938',201801,'20180102'),
                ('00183938',201802,'20180201'),
                ('00183946',201709,'20170901'),
                ('00183946',201710,'20171001'),
                ('00183946',201711,'20171101')) V(INSCRIP,COMPETENCE,reading_date))
SELECT COMPETENCE,
       LEAD(Reading_Date) OVER (/*PARTITION BY LOCALE*/ ORDER BY Reading_Date) AS next_lecture,
       DATEDIFF(DAY,Reading_date, LEAD(Reading_Date) OVER (/*PARTITION BY LOCALE*/ ORDER BY Reading_Date)) AS ReadingDate,
       INSCRIP
FROM VTE
ORDER BY Reading_date ASC