需要解决多个条件的复杂 where 子句的问题

Need to resolve issue with complex where clause for multiple criteria

我有一个正在进行的项目,我正在将几个表合并为一个左连接表,以便 dbo.spi 是主要表。我的问题来自 where 子句并用 null 提取任何内容。

要使其显示在结果中,需要满足几个条件。我的问题是,无论我如何编写 where 子句,某些标准似乎都会破坏另一个。我知道这与我的订单有关,但我无法弄清楚它是什么。我的标准如下。

我尝试过许多不同的命令和 () 但是无论我做什么,在某些时候 where 子句中的某些标准似乎开始被忽略。抱歉,时间太长了,在此先感谢大家。

Part 1:
M.knum must be Null
D.Knum must be null
O.knum must be null
E.Knum must be null
st.customer_id must be Null

Part 2:
IF c.pbs#freq is A and c.pbs#days#delq <= 716 - Show in results
IF c.pbs#freq is E and c.pbs#days#delq <= 1446 - Show in results
IF c.pbs#freq is O and c.pbs#days#delq <= 1081 - Show in results
IF c.pbs#freq is null - Show in results

Part 3: 
i.date >= 5/1/2019 - Show in results 
i.date is Null - Show in results

Part 4:
p.d30 is <= 6 or null - Show in results
p.d60 is <= 1 or null - Show in results
p.d90 is <= 0 or null - Show in results

Part 5:
p.pmts_Made >= 12 or p.pmts_made is null - Show in results
    select s.knum,m.knum as M_knum, d.knum as D_knum, o.knum as O_knum, e.knum as E_knum, s.customer_id, st.customer_id as ST_Customer_id, s.fdd, s.status, s.state, 
        (select max(HighTrw) from (values (trw1), (trw2), (trw3)) as Value(HighTrw)) as high_trw, i.knum as Issued, isnull (i.date, '1980-01-01') as Issued_Date, c.PBS#DISP as MF_Disp, c.LN#DISP as LN_Disp,
        c.PBS#DAYS#DELQ as MF_Delq, c.PBS#FREQ as MF_Freq, c.LN#CURR#BAL as LN_Bal, c.LN#NPDD as NPDD, p.PMTS_Made as Pmts_Made, p.D30, p.D60, p.D90

    From dbo.SPI S

    left outer join dbo.combined C
        on s.knum = c.k
    Left outer join dbo.payments P
        on s.knum = p.knum 
    Left join dbo.dvn D
        on s.knum = d.knum
    Left join dbo.exclusion E
        on s.knum = e.knum
    left join dbo.issued I 
        on s.knum = i.knum
    left join dbo.outfield O
        on s.knum = o.knum 
    left join dbo.spi_tours ST
        on s.customer_id = st.customer_id
    left join dbo.magna M
        on s.knum = m.knum



        where

                              (c.pbs#freq = 'A' and c.pbs#days#delq <= 716  
                           or c.pbs#freq = 'E' and c.pbs#days#delq <= 1446 
                           or c.pbs#freq = 'O' and c.pbs#days#delq <= 1081 ) and

        (
       (i.date >= '2019-05-01' or i.date is null) 

        and

       (st.customer_id is null) 

       and    

       (p.d30 <= '6' or p.d30 is null) and (p.d60 <= '1' or p.d60 is null) and (p.d90 <= '0' or p.d90 is null)

                       )
       and       
               (

                     m.knum is null
                  and o.knum is null 
                  and e.knum is null 
                  and d.knum is null
                )
        and

                     (p.PMTS_Made >= '12' or p.pmts_made is null)

        and

        (
            (
                select max(HighTrw)
                from (values (trw1), (trw2), (TRW3)) As updatedate (HighTrw)
            )  between 625 and 900  
            and c.LN#CURR#BAL between '7500' and '15000' ) 

        or 

        (
            (
                select max(HighTrw)
                from (values (trw1), (trw2), (TRW3)) As updatedate (HighTrw)
            )  between 600 and 624  
            and c.LN#CURR#BAL between '5000' and '7499' ) 

        or

        (
            (
                select max(HighTrw)
                from (values (trw1), (trw2), (TRW3)) As updatedate (HighTrw)
            )  between 600 and 624  
            and c.LN#CURR#BAL between '3000' and '499' ) 
     ```     
    ```
    knum    M_knum  D_knum  O_knum  E_knum  customer_id ST_Customer_id  fdd status  state   high_trw    Issued  Issued_Date MF_Disp LN_Disp MF_Delq MF_Freq LN_Bal  NPDD    Pmts_Made   D30 D60 D90
    109924  NULL    NULL    NULL    NULL    102014  NULL    2018-07-20  Full    PA  618 109924  2019-11-18  A   T   0   O   5584.18 2019-12-05  14  0   0   0
    107238  NULL    NULL    NULL    NULL    97287   NULL    2016-07-21  Full    NY  609 107238  2019-04-15  A   A   0   O   6998.61 2019-11-20  39  0   0   0
    95516   NULL    NULL    NULL    NULL    73190   NULL    2009-11-15  Full    MD  618 95516   2019-11-11  A   T   372 A   7202.02 2019-11-20  67  0   0   0
    109927  NULL    NULL    NULL    NULL    93592   NULL    2018-06-16  Full    NJ  671 109927  2019-11-11  A   A   0   O   7580.33 2019-12-05  16  0   0   0
    105681  NULL    NULL    NULL    NULL    79131   NULL    2015-05-02  Full    NY  662 105681  2019-11-11  A   A   7   A   7787.22 2019-12-05  54  0   0   0
    109928  NULL    NULL    NULL    NULL    96359   NULL    2018-06-16  Full    NY  696 NULL    1980-01-01  A   T   7   A   9837.91 2019-12-05  16  0   0   0
    109928  NULL    NULL    NULL    NULL    96359   NULL    2018-06-16  Full    NY  724 NULL    1980-01-01  A   T   7   A   9837.91 2019-12-05  16  0   0   0
    109931  NULL    NULL    NULL    NULL    106351  NULL    2018-07-16  Full    NY  618 NULL    1980-01-01  A   A   0   O   5158.56 2019-09-05  12  5   3   0
    109934  NULL    NULL    NULL    NULL    102087  NULL    2018-06-26  Full    PA  656 109934  2019-11-18  A   A   7   A   10730.94    2019-11-20  15  0   0   0
    109935  NULL    NULL    NULL    NULL    108582  NULL    2018-07-15  Full    NY  642 109935  2019-10-14  A   A   372 A   11024.11    2019-12-05  15  0   0   0
    104320  NULL    NULL    NULL    NULL    81542   NULL    2014-06-10  Full    NY  688 104320  2019-09-01  A   A   7   A   8171.64 2019-12-05  65  0   0   0
    107249  NULL    NULL    NULL    NULL    102757  NULL    2016-07-22  Full    NJ  617 107249  2019-07-29  A   A   7   A   6156.54 2019-12-05  39  0   0   0
    109940  NULL    NULL    NULL    NULL    108507  NULL    2018-06-17  Full    NJ  774 NULL    1980-01-01  A   A   7   A   9678.42 2019-12-05  16  0   0   0
    107254  NULL    NULL    NULL    NULL    100012  NULL    2016-07-23  Full    DE  700 107254  2019-09-01  A   A   7   A   8457.08 2019-11-20  38  0   0   0
    99906   NULL    NULL    NULL    NULL    82770   NULL    2012-06-16  Full    NJ  707 99906   2019-09-01  A   T   7   A   8777.3  2019-11-20  88  0   0   0
    108521  NULL    NULL    NULL    NULL    97201   NULL    2017-08-18  Full    NY  615 108521  2019-03-31  A   A   7   E   6346.63 2019-12-05  27  0   0   0

我尝试对您的查询进行排序,但仍然有很多令人困惑的事情,因为我不知道数据库的结构。

我认为您的主要查询应该如下所示:

SELECT  s.knum,m.knum as M_knum, 
        d.knum as D_knum, 
        o.knum as O_knum, 
        e.knum as E_knum, 
        s.customer_id, 
        st.customer_id as ST_Customer_id, 
        s.fdd, s.[status], s.[state], 
    (
        SELECT max(HighTrw) 
        FROM (
                VALUES (trw1), (trw2), (trw3)) as Value(HighTrw)
    ) as high_trw, 
    i.knum as Issued, 
    ISNULL(i.date, '1980-01-01') as Issued_Date, 
    c.PBS#DISP as MF_Disp, 
    c.LN#DISP as LN_Disp,
    c.PBS#DAYS#DELQ as MF_Delq, 
    c.PBS#FREQ as MF_Freq, 
    c.LN#CURR#BAL as LN_Bal, 
    c.LN#NPDD as NPDD, 
    p.PMTS_Made as Pmts_Made, 
    p.D30, p.D60, p.D90
From dbo.SPI S 
LEFT OUTER JOIN dbo.combined C on s.knum = c.knum
Left outer join dbo.payments P on s.knum = p.knum 
Left join dbo.dvn D on s.knum = d.knum AND d.knum is null
Left join dbo.exclusion E on s.knum = e.knum AND e.knum IS null
left join dbo.issued I  on s.knum = i.knum
left join dbo.outfield O on s.knum = o.knum AND o.knum is null
 left join dbo.spi_tours ST on s.customer_id = st.customer_id AND st.customer_id IS Null
 left join dbo.magna M on s.knum = m.knum AND m.knum is null
 WHERE
    (   (c.pbs#freq = 'A' and c.pbs#days#delq <= 716 ) or (c.pbs#freq = 'E' and c.pbs#days#delq <= 1446) or (c.pbs#freq = 'O' and c.pbs#days#delq <= 1081) OR c.pbs#freq is null ) 
AND ( (CONVERT(DATE,i.[date]) >= CONVERT(DATE,'2019-05-01') or i.[date] is null) )
AND ( (CAST(p.d30 AS INT) <= 6 or p.d30 is null) AND (CAST(p.d60 AS INT) <= 1 or p.d60 is null) AND (CAST(p.d90 AS INT) <= 0 or p.d90 is null) )

AND (CAST(p.PMTS_Made AS INT) >= 12 or p.pmts_made is null)

我不确定你下面的查询部分,如果你能解释一下我会帮助你(我仍然尝试格式化这个查询):

AND (
        (
            SELECT max(HighTrw)
            from (values (trw1), (trw2), (TRW3)) As updatedate (HighTrw)
        )  between 625 and 900  and c.LN#CURR#BAL between '7500' and '15000' 
        or
        (
            (
            select max(HighTrw)
            from (values (trw1), (trw2), (TRW3)) As updatedate (HighTrw)
            )  BETWEEN 600 and 624  
            and c.LN#CURR#BAL between '5000' and '7499'
        )
        or
        (
            (
                select max(HighTrw)
                from (values (trw1), (trw2), (TRW3)) As updatedate (HighTrw)
            )  between 600 and 624  
            and c.LN#CURR#BAL between '3000' and '499' 
        ) 
    )

我建议首先 运行 以上部分并查看结果,希望您的所有过滤器都可以正常工作,因为我加入的过滤器很少,我只是通过适当的转换格式化的过滤器很少只是尝试戴上合适的牙套。

我相信这会满足您的需求。请注意,您从 where 子句中排除了 high_trw 值检查,因此我将它们添加回:

SELECT s.knum,
    m.knum AS M_knum,
    d.knum AS D_knum,
    o.knum AS O_knum,
    e.knum AS E_knum,
    s.customer_id,
    st.customer_id AS ST_Customer_id,
    s.fdd,
    s.STATUS,
    s.STATE,
    (
        SELECT max(HighTrw)
        FROM (
            VALUES (trw1),
                (trw2),
                (trw3)
            ) AS Value(HighTrw)
        ) AS high_trw,
    i.knum AS Issued,
    isnull(i.DATE, '1980-01-01') AS Issued_Date,
    c.PBS#DISP AS MF_Disp,
    c.LN#DISP AS LN_Disp,
    c.PBS#DAYS#DELQ AS MF_Delq,
    c.PBS#FREQ AS MF_Freq,
    c.LN#CURR#BAL AS LN_Bal,
    c.LN#NPDD AS NPDD,
    p.PMTS_Made AS Pmts_Made,
    p.D30,
    p.D60,
    p.D90
FROM dbo.SPI S
LEFT JOIN dbo.combined C
    ON s.knum = c.k
LEFT JOIN dbo.payments P
    ON s.knum = p.knum
LEFT JOIN dbo.dvn D
    ON s.knum = d.knum
LEFT JOIN dbo.exclusion E
    ON s.knum = e.knum
LEFT JOIN dbo.issued I
    ON s.knum = i.knum
LEFT JOIN dbo.outfield O
    ON s.knum = o.knum
LEFT JOIN dbo.spi_tours ST
    ON s.customer_id = st.customer_id
LEFT JOIN dbo.magna M
    ON s.knum = m.knum
WHERE
    -- Part 1
    M.knum IS NULL
    AND D.Knum IS NULL
    AND O.knum IS NULL
    AND E.Knum IS NULL
    AND st.customer_id IS NULL
    -- Part 2
    AND (
        (
            c.pbs#freq = 'A'
            AND c.pbs#days#delq <= 716
            )
        OR (
            c.pbs#freq = 'E'
            AND c.pbs#days#delq <= 1446
            )
        OR (
            c.pbs#freq = 'O'
            AND c.pbs#days#delq <= 1081
            )
        )
    -- Part 3
    AND (
        i.DATE >= '2019-05-01'
        OR i.DATE IS NULL
        )
    -- Part 4
    AND (
        (
            p.d30 <= '6'
            OR p.d30 IS NULL
            )
        OR (
            p.d60 <= '1'
            OR p.d60 IS NULL
            )
        OR (
            p.d90 <= '0'
            OR p.d90 IS NULL
            )
        )
    AND (
        p.PMTS_Made >= '12'
        OR p.pmts_made IS NULL
        )
    AND CASE
        WHEN high_trw BETWEEN 625 AND 900
            AND c.LN#CURR#BAL BETWEEN '7500' AND '15000'
            THEN 1
        WHEN high_trw BETWEEN 600 AND 624
            AND c.LN#CURR#BAL BETWEEN '3000' AND '7499' -- Typo - was 499
            THEN 1
        ELSE 0
        END = 1