简体   繁体   English

参考其他表以检查日期范围内的记录

[英]Reference other table to check records within date range

I am working a with a series of transactions and inventory receipt. 我正在处理一系列交易和库存收据。 My goal is to create a flag to see whether at up to 5 days before the of the transaction whether the company received a shipment of goods for that item. 我的目标是创建一个标记,以查看在交易开始前最多5天,公司是否收到该项目的货物。 The pseudo logic is as follows: 伪逻辑如下:

For each transaction date: 
   For all inventory receipts whether inventory.item# = transaction.item#
       Are there records between transaction date - 5 and transaction date?

Normally, I would just create two tables and join them, but they really are two separate data structures.My initial attempt: 通常,我只创建两个表并将它们联接在一起,但是它们实际上是两个单独的数据结构。

PROC SQL;
   Create Table TABLE 1 AS 
         SELECT TRANSACTION.DATE, 
                SUM(CASE WHEN INVENTORY.DATE BETWEEN (TRANSACTION.DATE - 5 AND TRANSACTION.DATE) AND INVENTORY.ITEM = TRANSACTION.ITEM;

But how would I reference the tables? 但是我将如何引用这些表? I'm not sure left/right/inner joins are appropriate here. 我不确定左/右/内联接在这里是否合适。 I'm not really trying to join the tables, am I? 我不是真的想参加会议,是吗?

EDIT: My data is structured is as follows: 编辑:我的数据结构如下:

TRANSACTION TABLE 

ITEM DATE
0012 12/2
0231 12/3
0421 12/3 

INVENTORY TABLE 

0012 11/30
0231 12/4
0421 12/1

For ITEM 0012, we would look at the inventory table and see that there was a receipt on 11/30 for that item, so that would be flag = 1. For item 0231, we see that there was no inventory recipe within five days of the transaction, so flag = 0. 对于ITEM 0012,我们将查看库存表,并在11/30上看到该物料的收货,因此标记为1。对于0231物料,我们看到在5天之内没有库存配方交易,因此标志= 0。

A standard SQL query works here for me, not sure why you felt a standard join wouldn't work. 标准SQL查询对我有用,不确定您为什么会觉得标准联接不起作用。 Given the structure you've laid out, it works, and with your sample data. 给定您已布局的结构,它可以工作,并且可以与示例数据一起使用。 If you have duplicates for dates and items I'm not sure how it would work, but you didn't include that in your question and I have to assume that your sample data is reflective of your actual situation. 如果您有日期和项目的重复项,我不确定它是如何工作的,但是您没有在问题中包括它,因此我不得不假设您的样本数据可以反映您的实际情况。

Assuming, Table names are t1 and t2: 假设表名是t1和t2:

proc sql;
create table want as
select t1.*, t2.date as date2, 
    case when not missing(t2.date)  then 1 
            else 0 
     end as check_flag
from t1 as t1
left join t2 as t2
    on t1.item=t2.item
    and t2.date between t1.date-5 and t1.date;
quit;

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

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