通过过滤从单个条目中选择多个值

时间:2017-04-19 23:08:46

标签: sql sql-server

我有一张包含以下信息的表

(EmployeeID,DriverLicenseExpiryDate,AutoInsuranceExpiryDate,TWICCardExpiryDate)

如何选择任何许可证的员工在某一天内到期(比方说30天)?

我正在尝试获得这样的结果

EmployeeID  ExpiryDate  LicenseType

10001       2017-04-31  Driver license
10002       2017-04-21  Driver license
10002       2017-05-11  Auto insurance
10003       2017-04-20  Driver license
10003       2017-05-01  TWIC card

这可能吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

如果您使用的是MSSQL,那么您将拥有BETWEEN运算符,非常适合:

csv

如果您可以在允许脚本的环境中工作,可以使用变量来简化:

import requests
from lxml import html

delimiter = ";"
file_name = 'data.csv'

def Startpoint():
    address = "https://www.sephora.ae/en/stores/"
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//li[contains(@class,"level0")]')
    for title in titles:
        href = title.xpath('.//a[contains(@class,"level0")]/@href')[0]
        Layer2(href)

def Layer2(address):
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//li[contains(@class,"amshopby-cat")]')
    for title in titles:
        href = title.xpath('.//a/@href')[0]
        Endpoint(href)

def Endpoint(address):
    page = requests.get(address)
    tree = html.fromstring(page.text)
    titles=tree.xpath('//div[@class="product-info"]')
    for title in titles:
        Name = title.xpath('.//div[contains(@class,"h3")]/a[@title]/text()')[0]
        Price = title.xpath('.//span[@class="price"]/text()')[0]
        metco=(Name,Price)
        print(metco)
        with open(file_name,'a') as outfile:
            outfile.write(delimiter.join(metco).encode('utf8') + '\n')

with open(file_name,'w') as outfile:
    outfile.write(delimiter.join(["Product Name", "Price"])+'\n')
Startpoint()

答案 1 :(得分:0)

我会使用outer apply取消数据,然后应用条件:

select t.EmployeeId, v.LicenceType, v.ExpiryDate
from t cross apply
     (values ('Driver License', DriverLicenseExpiryDate), 
             ('Auto Insurance', AutoInsuranceExpiryDate),
             ('TWIC Card', TWICCardExpiryDat)
     ) v(LicenceType, ExpiryDate)
where v.ExpiryDate >= getdate() and
      v.ExpiryDate < dateadd(day, 30, getdate();

这是取消数据取消的最有效方法,因为原始表只读取一次。此外,该日期的条件仅表示一次。

我还建议仔细查看日期算术。 getdate()有一个时间组件,因此可能无法完全按照您的意愿执行。