SQL日期条件引用不同的表

时间:2012-12-10 17:16:54

标签: sql date

我正在尝试构建两个视图。视图#1只返回一组推荐数据,条件是推荐源自过去14天(以及其他一些标准)。这已经完成。

第二个视图#2必须返回过去14天内有过推荐的客户的所有相关推介(来自View#1的推荐),但必须限制在该推荐日期之前不超过60天。视图#1中的推荐,不得包含来自视图#1的相同推荐,并且在视图#1的相关推荐之后不能产生相关推荐

查看#1:TP.INC_REFERRALS_V

所以基本上我想在第二个视图中为每个推荐设置一个日期条件,该推荐基于第一个视图中的推荐对应。

到目前为止,我在查询结束时的条件是:

... WHERE TP.PROSPECT.PROSPECT_ID IN ((SELECT TP.INC_REFERRALS_V.REFERRAL_TABLE_PROS_ID FROM TP.INC_REFERRALS_V)) - 这会将标识符(prospect_id)与第一个视图匹配(tp.inc_referrals_v以返回所有相关推介

AND TP.REFERRAL_V.REFERRAL_ID NOT IN (SELECT TP.INC_REFERRALS_V.REFERRAL_ID FROM TP.INC_REFERRALS_V) - 这不包括第一个视图中已经返回的推荐

AND... - 需要其他标准的逻辑。

建议?

第一个视图的查询如下。第三个视图将是完全相同的格式,但具有不同的条件..(这是我遇到麻烦的部分)

查看#1

    SELECT TP.REFERRAL_V.REFERRAL_ID,
TP.REFERRAL_V.PROSPECT_ID AS REFERRAL_TABLE_PROS_ID,  
TP.PROSPECT.PROSPECT_ID,    
TP.PROSPECT.SEARCH_NAME AS CUSTOMER_PROSPECT_NAME,  
TP.PROSPECT.CUSTOMER_ID,  
TP.PROSPECT.CUSTOMER_SINCE,  
TP.PROSPECT.IS_BUSINESS,  
TP.PRODUCT_ML_INFO.PRODUCT_CODE,  
TP.PRODUCT_ML_INFO.PRODUCT_ID,  
TP.REGION_ML_INFO.REGION_DESCRIPTION,  
CASE WHEN (TP.REFERRAL_V.PRODUCT_ID) IS NULL THEN
TP.PRODUCT_CATEGORY_ML_INFO.PROD_CAT_DESCRIPTION ELSE 
TP.PRODUCT_ML_INFO.PRODUCT_DESCRIPTION END AS PROD_CAT_NAME,
TP.REFERRAL_V.PROD_CATEGORY_ID,  
(VXUZ.FIRST_NAME || ' ' || VXUZ.LAST_NAME)    AS CREATED_BY,
(VXU2.FIRST_NAME || ' ' || VXU2.LAST_NAME) AS ASSIGN_TO,
CASE WHEN (TP.REFERRAL_V.REFERRAL_STATUS) = '2'   
THEN TP.REFERRAL_V.CREDIT_LOCATION_NAME ELSE LCX1.LOCATION_NAME END AS   ASSIGN_TO_COST_CENTER,  
     EXU2.EMPLOYEE_NUMBER AS ASSIGN_TO_EMP_NUM,
    CXZ1.CODE_TEXT AS STAGE,  
  TP.CODE.CODE_TEXT AS STATUS, 
  TP.REFERRAL_EXTENSION_V.EXTENDED_DATETIME2 AS SOLD_DATE,    
TP.REFERRAL_V.CREATED_WHEN AS ORIGINATION_DATE,  
FROM TP.REFERRAL_V  
(joins removed - too many to list)  
WHERE VXU4.USER_ID <> VXU2.USER_ID  
   AND TRUNC(TP.REFERRAL_V.CREATED_WHEN) BETWEEN TRUNC(SYSDATE - 14) AND TRUNC(SYSDATE)

查看1结果

Referral_id    Origination_Date   Prospect_ID
        2001              29-NOV-12             A1
        2002              02-DEC-12             A2
        2003              07-DEC-12             A3

View2结果(应该是这样):

Referral_id   Origination_Date   Prospect_ID
        1995              01-NOV-12             A1  
        1993              25-OCT-12             A2  
        1994              20-OCT-12             A2
        1990              17-OCT-12             A2  
        1989              10-NOV-12             A3  

正如您在View2结果中看到的,结果应该包括View1中涉及的“prospect_ids”的所有相关引荐,但不应该在60天之前有任何日期,并且不应该在视图1中包含结果。

1 个答案:

答案 0 :(得分:1)

这看起来怎么样?

SELECT DISTINCT referralID, r.origination_date, r.prospectID, min_dt
FROM referralV r INNER JOIN
(
    SELECT prospectID, min(origination_date) AS min_dt
    FROM referralv
    WHERE origination_date >= dateadd(d,-14,getdate())
    GROUP BY prospectID
) p ON
r.prospectID = p.prospectid
WHERE
  origination_date < dateadd(d,-14,getdate()) AND
  origination_date >= dateadd(d,-60,min_dt)


CREATE TABLE [referralv](
    [referralID] [int] NOT NULL,
    [origination_date] [datetime] NOT NULL,
    [prospectID] [char](2) NOT NULL
) ON [PRIMARY];

INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (2001,'29-NOV-12','A1');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (2002,'02-DEC-12','A2');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (2003,'07-DEC-12','A3');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (1995,'01-NOV-12','A1');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (1993,'25-OCT-12','A2');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (1994,'20-OCT-12','A2');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (1990,'17-OCT-12','A2');
INSERT INTO [referralv]([referralID],[origination_date],[prospectID]) VALUES
           (1989,'10-NOV-12','A3');

sqlfiddle