如何解决 "incorrect syntax near ','"?

How do I resolve an "incorrect syntax near ','"?

我正在为广告系列编写查询,每当我尝试 运行 时,我都会收到一条错误消息,指出语法不正确。

select
    opp.*
from
(
    select 
        opp.*,
        row_number() over (partition by opp.contact_email_address order by opp.status_date desc) as row_number
    from
        opportunity_data opp
    where
        opp.email_bounced = 'false'
        and opp.email_unsubscribe = 'false'
        and opp.first_mkt_medium not in ('partner', 'inbound_outbound')
        and opp.latest_mkt_medium not in ('partner', 'inbound_outbound')
        and datediff (day, cast(latest_rfq_submitted_date as date), cast(getdate() as date)) > 30
        and opp.on_cover = 'no'
        and opp.primary_group in ('Market_trader', 'Food_stand', 'Mobile_food_van', 'Caterer')
        and opp.site = 'simplybusiness'
        and opp.opportunity_status = ('quote_recieved', 'rfq_submitted', 'policy_expired_not_renewed')
) opp
where row_number = 1

错误出现是因为您的最后 where 行。你不能这样使用它。我认为您想使用 in 而不是 =:

AND opp.opportunity_status IN ('quote_recieved', 'rfq_submitted', 'policy_expired_not_renewed')

用这个检查:

select
    opp.*
from
(
    select 
        opp.*,
        row_number() over (partition by opp.contact_email_address order by opp.status_date desc) as row_number
    from
        opportunity_data opp
    where
        opp.email_bounced = 'false'
        and opp.email_unsubscribe = 'false'
        and opp.first_mkt_medium not in ('partner', 'inbound_outbound')
        and opp.latest_mkt_medium not in ('partner', 'inbound_outbound')
        and datediff (day, cast(latest_rfq_submitted_date as date), cast(getdate() as date)) > 30
        and opp.on_cover = 'no'
        and opp.primary_group in ('Market_trader', 'Food_stand', 'Mobile_food_van', 'Caterer')
        and opp.site = 'simplybusiness'
        and opp.opportunity_status in ('quote_recieved', 'rfq_submitted', 'policy_expired_not_renewed')
) opp
where row_number = 1