查询有效约会的条件案例

时间:2019-03-05 16:04:01

标签: sql sql-server-2014

我正在构建SQL,我需要进行过滤以获取最大的(MAX)有效日期行和最低的PRIORITY(MIN)数字,但对于{{1},仅 }的有效日期行超过1个。对于VENDOR_ID的行,该行只有1个有效的过期日期,因此无论该行是否为VENDOR_ID,我都希望检索该行。

这是我当前的SQL-但是,它排除了(我要包括的行)其中有一个MIN PRIORITY,其中有一个MAX有效日期行, AND 它不是{ VENDOR_ID代表MIN PRIORITY

VENDOR_ID

样品数据:

应该在上面的查询中返回单行的PS_DIR_DEP_DISTRIB数据,但这不是因为有效的最大(单)日期行不是MIN优先级(下一行,日期2016-11-26):

SELECT A.VENDOR_ID, *
  FROM PS_VENDOR A
  INNER JOIN HRDEV01_FOR_BUDGET_MODULE.HRDEV92B.dbo.PS_DIR_DEP_DISTRIB B ON B.EMPLID = A.VNDR_FIELD_C30_B
  WHERE A.VNDR_FIELD_C30_B <> ''
  AND B.EFFDT = 
        (SELECT MAX(A_ED.EFFDT) FROM HRDEV01_FOR_BUDGET_MODULE.HRDEV92B.dbo.PS_DIR_DEP_DISTRIB A_ED 
        WHERE B.EMPLID = A_ED.EMPLID 
          AND A_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))

  AND B.PRIORITY = 
     (SELECT MIN(AA.PRIORITY) FROM HRDEV01_FOR_BUDGET_MODULE.HRDEV92B.dbo.PS_DIR_DEP_DISTRIB AA
      WHERE B.EMPLID = AA.EMPLID)

编辑2:

下面是2个VENDOR_ID的示例。 VENDOR_ID 45678有2个“有效日期”行(2018-08-16),而VENDOR_ID只有1个“有效日期”行。

VENDOR_ID   EFFDT                         PRIORITY
12345       1998-09-01 00:00:00.000       300
12345       2005-08-05 00:00:00.000       300
12345       2006-11-30 00:00:00.000       300
12345       2006-11-30 00:00:00.000       310
12345       2006-12-11 00:00:00.000       300
12345       2006-12-11 00:00:00.000       310
12345       2013-03-22 00:00:00.000       300
12345       2013-03-22 00:00:00.000       310
12345       2014-08-01 00:00:00.000       300
12345       2014-08-01 00:00:00.000       310
12345       2016-11-26 00:00:00.000       999

根据我想做的事情,这两个VENDOR_ID的预期输出如下:

VENDOR_ID   EFFDT                     PRIORITY
45678       2018-08-16 00:00:00.000   2
45678       2018-08-16 00:00:00.000   999
12345       1998-09-01 00:00:00.000   300
12345       2005-08-05 00:00:00.000   300
12345       2006-11-30 00:00:00.000   300
12345       2006-11-30 00:00:00.000   310
12345       2006-12-11 00:00:00.000   300
12345       2006-12-11 00:00:00.000   310
12345       2013-03-22 00:00:00.000   300
12345       2013-03-22 00:00:00.000   310
12345       2014-08-01 00:00:00.000   300
12345       2014-08-01 00:00:00.000   310
12345       2016-11-26 00:00:00.000   999

如果VENDOR_ID的有效日期行超过1个,那么我也想在上面应用MIN(PRIORITY)逻辑。否则,如果只有一个MAX有效日期日期行,那么我不想应用MIN(PRIORTY)逻辑。希望有道理。

编辑3:

我正在使用INSERT进行整个SQL脚本,其中上面的SELECT查询被用作INSERT的基础。我希望能够插入硬编码的日期,但仍使用上面的逻辑:

   VENDOR_ID      EFFDT                       PRIORITY
   45678          2018-08-16 00:00:00.000     2
   12345          2016-11-26 00:00:00.000     999

2 个答案:

答案 0 :(得分:0)

似乎您需要简单的汇总

 select VENDOR_ID   ,max(EFFDT),min(PRIORITY)
 from table_name group by VENDOR_ID   

答案 1 :(得分:0)

解决方案是将EFFDT添加为B.PRIORITY上的子查询的一部分,其中已联接DIR_DEP_DISTRIB表的生效日期等于PRIORITY子查询的生效日期。

SELECT A.VNDR_FIELD_C30_B,    -- <-- I added this for debugging purposes
         'SHARE', VENDOR_ID, '000001', '2019-03-01 00:00:00.000', '1', '', 'Y', '001', '022000046', '', '03', '', '01', COALESCE(B.BANK_CD,'<No Match>'), 'Test Bank', '', '','',
         'USA', '','','','','','','','','','','','','','','','','','','','','','', COALESCE(B.ACCOUNT_NUM,'<No Match>')
  FROM PS_VENDOR A
  INNER JOIN HRDEV01_FOR_BUDGET_MODULE.HRDEV92B.dbo.PS_DIR_DEP_DISTRIB B 
          ON B.EMPLID = A.VNDR_FIELD_C30_B  -- Moved the EFFDT and PRIORITY lines here from out of the WHERE clause, so I can do a proper LEFT JOIN
         AND B.EFFDT = (SELECT MAX(A_ED.EFFDT) FROM HRDEV01_FOR_BUDGET_MODULE.HRDEV92B.dbo.PS_DIR_DEP_DISTRIB A_ED 
                        WHERE B.EMPLID = A_ED.EMPLID
                          AND A_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10) )
         AND B.PRIORITY = (SELECT MIN(AA.PRIORITY) FROM HRDEV01_FOR_BUDGET_MODULE.HRDEV92B.dbo.PS_DIR_DEP_DISTRIB AA
                           WHERE B.EMPLID = AA.EMPLID
                             AND B.EFFDT = AA.EFFDT)   -- Min Priority FOR A GIVEN EFFDT 
  WHERE A.VNDR_FIELD_C30_B <> ''