SQL Server - 忽略where子句条件

时间:2016-11-16 21:12:00

标签: sql-server tsql date where cross-join

由于某种原因,where子句中的数据条件被忽略 - 有任何建议吗?

SELECT  D.[Date],
        D.Year,
        D.MonthName,
        D.WeekOfYear,
        E.bu_name,
        E.emp_mgr,
        E.emp_sup,
        E.emp_name,
        E.emp_jde,
        C.calls,
        s.sales
FROM    DateTable AS D
        CROSS JOIN EmployeeTable AS E
        LEFT JOIN (SELECT   CAST([start_date] AS DATE) AS call_date,
                            [agent_no] AS agent_id,
                            SUM(CASE WHEN [skill_name] LIKE '5700 Sales l%'
                                          AND [Agent_Time] != '0' THEN 1
                                     ELSE 0
                                END) AS calls
                   FROM     CallTable
                   GROUP BY CAST([start_date] AS DATE),
                            [agent_no]
                  ) AS C ON D.[Date] = C.call_date
                            AND E.emp_vcc = C.agent_id
        LEFT JOIN (SELECT   [AC#DTE_dt] AS sale_date,
                            [EMPJDENUM] AS emp_jde,
                            SUM(CASE WHEN [CHANNEL] = 'I'
                                          AND ([ICGCD2] = 'L'
                                               OR [ICGCD2] = 'H')
                                          AND [ITMQTY] > 3
                                          AND [EMPBUNCOD] IN ('5044','5077') THEN 1
                                     ELSE 0
                                END) AS sales
                   FROM     SalesTable
                   GROUP BY [AC#DTE_dt],
                            [EMPJDENUM]
                  ) AS S ON D.Date = s.sale_date
                            AND E.emp_jde = S.emp_jde
WHERE   c.calls > 0
        OR S.sales > 0
        AND d.Year = '2016'

1 个答案:

答案 0 :(得分:1)

这不是一个真正的答案,而是一个如何格式化此查询以便您可以更轻松地阅读它的示例。我还修改了你的where谓词,以适应几乎可以肯定的问题。

SELECT D.[Date]
    , D.Year
    , D.MonthName
    , D.WeekOfYear
    , E.bu_name
    , E.emp_mgr
    , E.emp_sup
    , E.emp_name
    , E.emp_jde
    , C.calls
    , s.sales
FROM [Date Table] AS D
CROSS JOIN [Employee TABLE] AS E
LEFT JOIN 
(
    SELECT Cast([start_date] AS DATE) AS call_date
        , [agent_no] AS agent_id
        , Sum(CASE WHEN [skill_name] LIKE '5700 Sales l%' AND [Agent_Time] != '0' THEN 1 ELSE 0 END) AS calls
    FROM [Call TABLE]
    GROUP BY Cast([start_date] AS DATE), [agent_no]
) AS C ON D.[Date] = C.call_date
        AND E.emp_vcc = C.agent_id
LEFT JOIN 
(
    SELECT [AC#DTE_dt] AS sale_date
        , [EMPJDENUM] AS emp_jde
        , Sum(CASE WHEN [CHANNEL] = 'I' AND [ICGCD2] IN ('L', 'H') AND [ITMQTY] > 3 AND [EMPBUNCOD] IN ( '5044', '5077' ) THEN 1 ELSE 0 END) AS sales
    FROM [Sales TABLE]
    GROUP BY [AC#DTE_dt], [EMPJDENUM]
) AS S ON D.Date = s.sale_date
    AND E.emp_jde = S.emp_jde
WHERE 
(
    c.calls > 0
    OR 
    S.sales > 0
)
AND d.Year = '2016';