【问题标题】:Trying to perform an SQL join between two filtered tables尝试在两个过滤表之间执行 SQL 连接
【发布时间】: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


【解决方案1】:

我认为您的问题在于这一行,即您的 select 子句中的最后一个字段。

REP_FTEReport_secured.Country AS FTE_Country.EmployeeName AS FTE_Name

它有 2 个别名和 2 个dots

混淆在最后两行

REP_FTEReport_secured.GPN, REP_FTEReport_secured.Country AS FTE_Country,
REP_FTEReport_secured.Country AS FTE_Country.EmployeeName AS FTE_Name

首先让它们更具可读性

REP_FTEReport_secured.GPN, 
REP_FTEReport_secured.Country AS FTE_Country,
REP_FTEReport_secured.Country AS FTE_Country.EmployeeName AS FTE_Name

现在您可以看到最后有问题。
你可能需要这个

REP_FTEReport_secured.GPN, 
REP_FTEReport_secured.Country AS FTE_Country,
REP_FTEReport_secured.EmployeeName AS FTE_Name

如您所见,只要使查询更具可读性,在这种情况下,每行只放置一个字段,而不是 2 个,错误就会立即可见

【讨论】:

  • 非常感谢@GuidoG - 复制和粘贴错误,看不到树木的木材。
  • @Omar 这是你应该开始使用别名的原因之一,它使代码更具可读性,这样的错误会更好地突出
猜你喜欢
  • 2016-02-27
  • 2022-11-14
  • 2012-04-25
  • 2019-12-14
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
相关资源
最近更新 更多