简体   繁体   English

MSSQL,如何将记录与 dataframe 表进行比较?

[英]MSSQL, how compare records with dataframe table?

let's say we have a table with invoices and employees ID假设我们有一张包含发票和员工 ID 的表格在此处输入图像描述

and another table for vacation which also contains an employee ID.另一个休假表也包含员工 ID。 In this case only emplyee1 had vacation a these date frames.在这种情况下,只有 emplyee1 在这些日期范围内休假。

在此处输入图像描述 Now we have to compare if the invoice date is between or exactly at the date frames of vacation table.现在我们必须比较发票日期是在假期表的日期范围内还是恰好在假期表的日期范围内。

The aim is: The invoice date has to be compared, if an employee is on vacation the calc_flag = 0, if not then always=1.目的是:必须比较发票日期,如果员工正在休假,则 calc_flag = 0,否则始终 = 1。 Eg employee1 was from 2023/01/05 till 2023/01/12 on vacation.例如,员工 1 从 2023/01/05 到 2023/01/12 休假。 So all his invoices must be calc_flag=0 for this time.所以这次他所有的发票都必须是 calc_flag=0。

How to create the sql query in mssql for this topic?如何在 mssql 中为此主题创建 sql 查询?

Thanks for any advice.感谢您的任何建议。

I already tried to check the dateframes but if there are several entries in vacation table, I'm not sure how to handle it.我已经尝试检查日期框架,但如果假期表中有多个条目,我不确定如何处理它。

One way to do this is a LEFT JOIN to check for existance.一种方法是使用 LEFT JOIN 检查是否存在。 Perhaps you need to tweak the < or > depening if you would like to include the Begin and or End date.如果您想包括开始和/或结束日期,您可能需要调整 < 或 > depening。

SELECT i.InvNo,
       i.emp_ID,
       i.InvDate,
       i.calc_flag ,
       CASE WHEN v.Emp_ID IS NOT NULL THEN 0 ELSE 1 END AS calc_flage
FROM dbo.invoices AS i
LEFT JOIN dbo.vacation AS v ON v.Emp_ID = i.emp_ID AND v.[Begin] < i.InvDate AND v.[End] > i.InvDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM