按数量对另一个表中使用的行分类

发布于 2025-02-11 19:17:39 字数 187 浏览 2 评论 0原文

我有2个表格和文档。 客户表的ID列与文档表中的CS_NO列相同,并且可以重复CS_NO。另外,在客户表中,我有一个名为as_ resident的列(值为y或n)。我需要查询是否具有IS_RESSIDEN VALUE N的客户有1个文档(如果CS_NO列中只写了一次客户ID) - “一个文档”,如果有2个或更多文档 - “某些文档”,则没有文件'。我不能加入

I have 2 tables- customers and documents.
Id column of customers table is same with the cs_no column in the documents table and cs_no can be repeated of course. Also in customers table, I have a column named as is_resident (values can be Y or N). I need to query if the customer with is_resident value N has 1 document(If customer id is written only once in cs_no column) - 'one document', if has 2 or more documents - 'some documents', if doesn't have 'no document'. I can't use join

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

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

发布评论

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

评论(1

就是爱搞怪 2025-02-18 19:17:39

使用相关的子问题:

SELECT id,
       (
         SELECT CASE COUNT(*)
                WHEN 0 THEN 'No Documents'
                WHEN 1 THEN 'One Document'
                ELSE        'Some documents'
                END
         FROM   document d
         WHERE  c.id = d.cs_no
       ) AS documents
FROM   customers c
WHERE  is_resident = 'N'

Use a correlated sub-query:

SELECT id,
       (
         SELECT CASE COUNT(*)
                WHEN 0 THEN 'No Documents'
                WHEN 1 THEN 'One Document'
                ELSE        'Some documents'
                END
         FROM   document d
         WHERE  c.id = d.cs_no
       ) AS documents
FROM   customers c
WHERE  is_resident = 'N'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文