0

我正在通过 ODBC 连接到 NetSuite 数据库,并为数据库创建了一个相当大的查询来运行报告。我已将代码放在这篇文章的末尾。我的问题是,查询排​​除了“A_T_PROJECTS”表中“TRADING_PARTNER_ID”字段中具有 NULL 的行。我知道我可以在这里使用 RIGHT OUTER JOIN,但由于我要加入其他几个表,我似乎无法让它工作。关于我可以尝试什么的任何建议?

该连接是只读的,因此没有临时表 - 我希望它可以在单个查询中工作。谢谢你的时间!

SELECT
    [A_T_CUSTOMERS].[COMPANYNAME] AS [Company Name],
    [A_T_MILESTONES].[PROJECT_MILESTONE_NAME] AS [Name],
    [A_T_PROJECTS].[COMPANYNAME] AS [Project Name],
    [A_T_STATUS].[LIST_ITEM_NAME] AS [Status],
    [A_T_MILESTONES].[DUE_DATE] AS [Milestone Due],
    [A_T_ACTIONREQ].[LIST_ITEM_NAME] AS [Action Required By],
    [A_T_PARTNERS].[COMPANYNAME] AS [Trading Partner],
    [A_T_EMPLOYEES].[FULL_NAME] AS [Assigned To],
    [A_T_CUSTOMERS].[ESTIMATED_DELIVERY_DATE] AS [Est Del Date],
    CASE
            WHEN [A_T_MILESTONES].[PROJECT_MILESTONE_NAME] = 'Project Complete/Move to Production'
                    THEN [A_T_MILESTONES].[DUE_DATE]
            ELSE NULL
    END AS [Est Move to Prod]

FROM
    [CUSTOMERS] AS [A_T_CUSTOMERS],
    [CUSTOMERS] AS [A_T_PROJECTS],
    [PROJECT_MILESTONE] AS [A_T_MILESTONES],
    [PARTNERS] AS [A_T_PARTNERS],
    [STATUS] AS [A_T_STATUS],
    [ACTION_REQUIRED] AS [A_T_ACTIONREQ],
    [EMPLOYEES] AS [A_T_EMPLOYEES]
    --RIGHT OUTER JOIN [PARTNERS] ON ([A_T_PARTNERS].[PARTNER_ID] = [A_T_PROJECTS].[TRADING_PARTNER_ID])

WHERE
    -- JOIN CLAUSES --
    [A_T_PROJECTS].[CUSTOMER_ID] = [A_T_MILESTONES].[PROJECT_NAME_ID]
    AND [A_T_CUSTOMERS].[CUSTOMER_ID] = [A_T_MILESTONES].[CUSTOMER_MILESTONE_NAME_ID]
    AND [A_T_PARTNERS].[PARTNER_ID] = [A_T_PROJECTS].[TRADING_PARTNER_ID]
    AND [A_T_EMPLOYEES].[EMPLOYEE_ID] = [A_T_MILESTONES].[ASSIGNED_TO_ID]
    AND [A_T_STATUS].[LIST_ID] = [A_T_MILESTONES].[STATUS_ID]
    AND [A_T_ACTIONREQ].[LIST_ID] = [A_T_MILESTONES].[ACTION_REQUIRED_BY_ID]

    -- FILTER CLAUSES --
    AND [A_T_CUSTOMERS].[CUSTOMER_SINCE] > '2011-12-10 00:00:00'
    AND ([A_T_MILESTONES].[STATUS_ID] = 1
            OR [A_T_MILESTONES].[STATUS_ID] = 2
            OR [A_T_MILESTONES].[STATUS_ID] = 3
            OR [A_T_MILESTONES].[STATUS_ID] = 5)
    AND ([A_T_PROJECTS].[STATUS] = 'Assigned'
            OR [A_T_PROJECTS].[STATUS] = 'Delayed - per Customer'
            OR [A_T_PROJECTS].[STATUS] = 'Delayed - per Internal'
            OR [A_T_PROJECTS].[STATUS] = 'Delayed - per TP/VAN'
            OR [A_T_PROJECTS].[STATUS] = 'In Progress'
            OR [A_T_PROJECTS].[STATUS] = 'Production'
            OR [A_T_PROJECTS].[STATUS] = 'On-Hold')
    AND [A_T_CUSTOMERS].[TRANSLATOR_ID] != 4
    AND [A_T_CUSTOMERS].[IN_PRODUCTION] = 'F'
    AND ([A_T_PROJECTS].[JOB_TYPE_ID] IS NOT NULL
            OR [A_T_PROJECTS].[JOB_TYPE_ID] != 8
            OR [A_T_PROJECTS].[JOB_TYPE_ID] != 11)
4

1 回答 1

2

您应该考虑使用JOINfor 每个表而不是使用 WHERE 子句来链接它们。如果两个表都需要包含相同的“Id”,则使用TableA INNER JOIN TableB ON TableA.Id=TableB.Id. 如果您想要所有 TableA 记录,即使 TableB 中缺少一些记录,请使用TableA LEFT JOIN TableB ON TableA.Id=TableB.Id. TableB 中没有匹配项的任何字段都将显示为NULL

您可以嵌套所有JOIN子句。我不确定netsuite是如何处理事情的,但您可能需要()绕过 join 子句。

FROM (TableA AS A INNER JOIN TableB AS B ON A.id=B.id)
                  LEFT JOIN TableC AS C ON A.id=C.id

祝你好运。

于 2012-07-17T16:24:55.463 回答