【发布时间】:2021-03-17 06:49:20
【问题描述】:
我正在尝试加入两个表,但对这两个表都应用了过滤器。例如。过滤左表,然后与右表的过滤版本连接,保留左表。
我的查询如下:
SELECT REP_Exposure_secured.DeliveryRegion, REP_Exposure_secured.DeliveryCountry,
REP_Exposure_secured.EngagementCompanyCode, REP_Exposure_secured.EngagementId,
REP_Exposure_secured.Engagement, REP_Exposure_secured.EngagementType,
REP_Exposure_secured.EngagementStatus, REP_Exposure_secured.EngagementPartner,
REP_Exposure_secured.EngagementPartnerID, REP_Exposure_secured.EngagementManager,
REP_Exposure_secured.ServiceLine, REP_Exposure_secured.SubServiceLine,
REP_Exposure_secured.Competency, REP_Exposure_secured.ServiceOffering,
REP_Exposure_secured.EngagementServiceCode, REP_Exposure_secured.EngagementService,
REP_Exposure_secured.EngagementLastTimeChargedDate, REP_Exposure_secured.EngagementCreationDate,
REP_Exposure_secured.Account, REP_Exposure_secured.UltimateDunsNumber,
REP_Exposure_secured.Client, REP_Exposure_secured.ClientID,
REP_Exposure_secured.AR_0_30, REP_Exposure_secured.AR_31_60,
REP_Exposure_secured.AR_61_90, REP_Exposure_secured.AR_91_120,
REP_Exposure_secured.AR_121_150, REP_Exposure_secured.AR_151_179,
REP_Exposure_secured.AR_180_365, REP_Exposure_secured.AR_Above_365,
REP_Exposure_secured.MTD_TER, REP_Exposure_secured.MTD_BilledAmt,
REP_Exposure_secured.MTD_CollectedAmt, REP_Exposure_secured.UnbilledInvAging30Amt,
REP_Exposure_secured.UnbilledInvAging60Amt, REP_Exposure_secured.UnbilledInvAging90Amt,
REP_Exposure_secured.UnbilledInvAging150Amt, REP_Exposure_secured.UnbilledInvAging180Amt,
REP_Exposure_secured.UnbilledInvAging365Amt, REP_Exposure_secured.UnbilledInvAging365PlusAmt,
REP_FTEReport_secured.GPN, REP_FTEReport_secured.Country AS FTE_Country,
REP_FTEReport_secured.Country AS FTE_Country.EmployeeName AS FTE_Name
FROM Reporting.REP_Exposure_secured
LEFT JOIN REP_FTEReport_secured
ON REP_Exposure_secured.EngagementPartnerID = REP_FTEReport_secured.GUI
WHERE (REP_Exposure_secured.DeliveryCountry IN ('Cambodia', 'Singapore', 'Malaysia', 'Indonesia',
'Guam', 'Laos', 'Maldives', 'Myanmar', 'Philippines', 'Sri Lanka', 'Thailand', 'Vietnam')
OR REP_Exposure_secured.DeliveryCountry LIKE 'Brunei Daruss%')
AND REP_Exposure_secured.SubServiceLine='Forensics'
AND REP_Exposure_secured.EngagementType LIKE 'External%'
AND REP_Exposure_secured.Currency='USD'
AND REP_FTEReport_secured.AccountingCycleDate IN
(
SELECT
DISTINCT
MAX(AccountingCycleDate)
FROM
REP_FTEReport_secured
)
AND REP_FTEReport_secured.Country IN ('Brunei', 'Cambodia', 'Guam', 'Indonesia', 'Lao', 'Malaysia', 'Maldives',
'Myanmar', 'Philippines', 'Singapore', 'Sri Lanka', 'Thailand', 'Vietnam')
AND REP_FTEReport_secured.SubServiceLine='Forensics'
AND REP_FTEReport_secured.RankName IN ('Partner', 'Director', 'Executive Director');
这会返回以下错误:
: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]'.' 附近的语法不正确。(102) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server驱动程序][SQL Server]关键字'AND'附近的语法不正确。(156)")
对我做错的任何帮助将不胜感激!
【问题讨论】:
-
与您的问题无关,但请在您的查询中开始使用别名,它可以使它们更好地阅读
-
还有一些缩进也会提高可读性
-
非常感谢@GuidoG - 我会接受这个。甚至不知道别名。
标签: sql sql-server tsql left-join where-clause