当(选择...)中的sql情况

发布于 2025-01-20 00:34:59 字数 648 浏览 0 评论 0原文

我不是SQL的专家,但我还没有发现类似的问题,也许太明显了... 因此,这是我的问题:

我有几个临时视图,例如“ Contract_ids_canceled”或“ Contract_ids_changed_date”,带有合同ID。我有一个大桌,所有这些合同ID等等。 现在,我想在新列中“标记”它们。我尝试了以下操作:

CASE WHEN contractid in (SELECT DISTINCT contractid FROM contract_ids_canceled) THEN 'contract canceled' 
     WHEN contractid in (SELECT blabla) THEN 'contract changed'
END as updates

但是我收到以下错误消息

SQL语句中的错误:AnalySisexception:IN/存在谓词子Queries只能在过滤器/加入和几个命令中使用:

如果我使用类似的东西

IN (1,2,3) 

,那么我会有些丢失,为什么为什么选择select语句案例不起作用,关于错误消息...如果有人可以在这里帮助我,那就太好了!

Databricks SQL。

I am not an expert on sql, but I have not found a similar issue, maybe it is too obvious...
So here is my question:

I have a couple of temp views like 'contract_ids_canceled' or 'contract_ids_changed_date' with contract ids. And I have a big table with all those contract ids and more.
Now I want to kind of 'tag' them in a new column. I have tried the following:

CASE WHEN contractid in (SELECT DISTINCT contractid FROM contract_ids_canceled) THEN 'contract canceled' 
     WHEN contractid in (SELECT blabla) THEN 'contract changed'
END as updates

but I get the following error message

Error in SQL statement: AnalysisException: IN/EXISTS predicate sub-queries can only be used in Filter/Join and a few commands:

If I am using something like

IN (1,2,3) 

then the IN works, so I am a bit lost why the SELECT statement in the CASE is not working and about the error message... Would be nice if someone could help me out here!

Databricks SQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2025-01-27 00:34:59

正如错误指示的那样,databricks不使用或中的中的中的中的 case> case 语句中的。作为替代方案,请考虑外部连接每个观点以主持合同表:

SELECT m.contractid,
       CASE 
          WHEN cncl.contractid IS NOT NULL 
          THEN 'contract canceled'
          WHEN dt.contractid IS NOT NULL 
          THEN 'contract date changed'
       END AS contract_status
       
FROM master_contracts m     
LEFT JOIN (
    SELECT DISTINCT contractid
    FROM contract_ids_canceled
)  cncl 
  ON m.contractid = cncl.contractid
LEFT JOIN (
    SELECT DISTINCT contractid
    FROM contract_ids_changed_date
) dt
  ON m.contractid = dt.contractid

As error indicates, Databricks does not support subqueries using IN or EXISTS in CASE statements. As an alternative, consider outer joining each view to master contract table:

SELECT m.contractid,
       CASE 
          WHEN cncl.contractid IS NOT NULL 
          THEN 'contract canceled'
          WHEN dt.contractid IS NOT NULL 
          THEN 'contract date changed'
       END AS contract_status
       
FROM master_contracts m     
LEFT JOIN (
    SELECT DISTINCT contractid
    FROM contract_ids_canceled
)  cncl 
  ON m.contractid = cncl.contractid
LEFT JOIN (
    SELECT DISTINCT contractid
    FROM contract_ids_changed_date
) dt
  ON m.contractid = dt.contractid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文