让员工采用周末工作模式

时间:2018-08-20 08:24:27

标签: sql sql-server

我有一个employee表,其中包含诸如 员工编号,上班日期,上班日期。

现在,我所需要的是找到那些按周末工作的员工。

这就像雇员在第1周工作,那么他(她)应该在第2周不工作,而必须在第3周工作。 第1周,第2周和第3周是连续的周末。

我尝试使用sql的滞后函数。

    SELECT   employee_id, 
         punch_in_date, 
         Lag(punch_in_date) OVER(partition BY employee_id ORDER BY employee_id)                              AS week_lag,
         Datediff(day,Lag(punch_in_date) OVER(partition BY employee_id ORDER BY employee_id) ,punch_in_date) AS days
FROM     employee 
WHERE    Datediff(day,Lag(punch_in_date) OVER(partition BY employee_id ORDER BY employee_id) ,punch_in_date)>= 14
AND      datediff(day, punch_in_date, 'Today's date') <= 90 /*This means the data must falls under 3 months duration*/;

但是我遇到了类似的错误

  

SQL错误[4108] [S0001]:窗口函数只能出现在   SELECT或ORDER BY子句。

如何获得所需的结果?

样本数据:

employee_ID |punch_in_date |punch_out_date |
------------|--------------|---------------|
2           |2015-12-05    |2015-12-05     |
2           |2015-12-12    |2015-12-12     |
2           |2015-12-19    |2015-12-19     |
2           |2016-01-02    |2016-01-02     |
2           |2016-01-23    |2016-01-24     |
2           |2016-01-24    |2016-01-25     |
2           |2016-01-30    |2016-01-30     |
2           |2016-02-06    |2016-02-06     |
2           |2016-02-06    |2016-02-06     |
2           |2016-02-06    |2016-02-07     |
2           |2016-02-13    |2016-02-14     |
2           |2016-02-27    |2016-02-28     |
2           |2016-03-12    |2016-03-13     |

2 个答案:

答案 0 :(得分:1)

错误消息指出;窗口功能仅在选择和排序时才允许。 您可以做的是在子查询中使用您的查询

Select Employee_id,punch_in_date, week_lag,[days] FROM(
 SELECT   employee_id, 
         punch_in_date, 
         Lag(punch_in_date) OVER(partition BY employee_id ORDER BY employee_id)                              
AS week_lag,
         Datediff(day,Lag(punch_in_date) OVER(partition BY employee_id ORDER BY 
employee_id) ,punch_in_date) AS [days]
FROM     employee 
where punch_in_date >= dateadd(day,-90,getdate())
) q
WHERE    [days]>= 14

答案 1 :(得分:1)

我怀疑您想要

override func viewDidLoad() {
    super.viewDidLoad()

    if let code = self.code {
        let url = URL(string : API.pdf + "/" + code)
        let loading = self.showLoading()
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
            DispatchQueue.main.async {
                self.pdfView.document = PDFDocument(data : responseData!)
                self.pdfView.scaleFactor = 1
                self.pdfView.backgroundColor = UIColor.lightGray
                self.pdfView.autoScales = true
                loading.dismiss(animated: true, completion: nil)
            }
        })
        task.resume()
    }
}

使用窗口函数时,请特别注意 import UIKit import PDFKit class PDFTestViewController : ViewController { @IBOutlet weak var pdfView: PDFView! override func viewDidLoad() { super.viewDidLoad() if let url = Bundle.main.url(forResource: "4547315264964", withExtension: "pdf"), let doc = PDFDocument(url: url){ self.pdfView.document = doc self.pdfView.scaleFactor = 1 } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 过滤。过滤器是在窗口函数之前应用的,因此您可能会错过一些想要的行。