我通过ODBC连接到NetSuite数据库,并为数据库创建了一个相当大的查询来运行报告。我把代码放在这篇文章的末尾。我的问题是,查询排除了“A_P_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)
答案 0 :(得分:2)
您应该考虑对每个表使用JOIN
,而不是使用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 如何处理事情,但您可能需要将()
放在连接子句中。
FROM (TableA AS A INNER JOIN TableB AS B ON A.id=B.id)
LEFT JOIN TableC AS C ON A.id=C.id
祝你好运。