需要有关困难 SQL 查询的帮助

发布于 2024-12-01 23:29:09 字数 1776 浏览 1 评论 0原文

我有一个查询,当前收到 ORA-01427: 单行子查询返回多行错误。我了解该错误以及导致该错误的原因,但无法找到解决方法。我也想在一个查询中完成此操作。这是我目前拥有的:

SELECT   v1.internal_code,
       (SELECT   terms_id
          FROM   terms
         WHERE   term_start_date =
                    (SELECT   MIN (term_start_date)
                       FROM   terms
                      WHERE   terms_id LIKE '%SU'
                              AND term_start_date >
                                    (SELECT   term_start_date
                                       FROM      terms
                                              JOIN
                                                 vals
                                              ON terms_id =
                                                    internal_code
                                      WHERE   internal_code =
                                                 v1.internal_code
                                              AND valcode_id =
                                                    'TERMS')))
          AS mmTerm
FROM      terms
       JOIN
          vals v1
       ON terms_id = internal_code
  WHERE   internal_code LIKE '%SP' AND valcode_id = 'WEB.SEARCH.TERMS'
 ORDER BY   mmTerm ASC

vals 表的相关部分是这样的:

internal_code      valcode_id
-------------      ----------
   2003SP            TERMS
   2004SP            TERMS
   2005SP            TERMS

好的,所以大子查询正在尝试获取以“SU”结尾的任何术语,其中 term_start_date 大于中术语的 term_start_date瓦尔斯表。所以期望的结果是:

v1.internal_code      mmTerm
----------------      ------
    2003SP            2003SU
    2004SP            2004SU
    2005SP            2005SU

我知道这很难理解,所以如果有问题请提问。此外,任何建议都会被欣然接受。谢谢!

编辑:我想通了。只是需要做一些修改,谢谢您的建议。如果有人有兴趣查看最终查询,我会发布它

I have a query that I am currently getting the ORA-01427: single-row subquery returns more than one row error on. I understand the error, and what is causing it, but cant figure out a way to fix it. I would also like to do this in just one query. Here is what I currently have:

SELECT   v1.internal_code,
       (SELECT   terms_id
          FROM   terms
         WHERE   term_start_date =
                    (SELECT   MIN (term_start_date)
                       FROM   terms
                      WHERE   terms_id LIKE '%SU'
                              AND term_start_date >
                                    (SELECT   term_start_date
                                       FROM      terms
                                              JOIN
                                                 vals
                                              ON terms_id =
                                                    internal_code
                                      WHERE   internal_code =
                                                 v1.internal_code
                                              AND valcode_id =
                                                    'TERMS')))
          AS mmTerm
FROM      terms
       JOIN
          vals v1
       ON terms_id = internal_code
  WHERE   internal_code LIKE '%SP' AND valcode_id = 'WEB.SEARCH.TERMS'
 ORDER BY   mmTerm ASC

And the relevant part of the vals table would be this:

internal_code      valcode_id
-------------      ----------
   2003SP            TERMS
   2004SP            TERMS
   2005SP            TERMS

Okay, so the big subquery is attempting to get any terms that end with 'SU' where the term_start_date is greater than the term_start_date of the terms in the vals table. So the desired result is:

v1.internal_code      mmTerm
----------------      ------
    2003SP            2003SU
    2004SP            2004SU
    2005SP            2005SU

I know this is tough to understand, so please ask questions if there are any. Also, any suggestions are gladly accepted. Thanks!

EDIT: I figured it out. Just had to do some reworking, thanks for the suggestions. I will post it if anyone is interested in seeing the final query

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

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

发布评论

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

评论(3

丿*梦醉红颜 2024-12-08 23:29:09

问题出在此处使用的子查询中: AND term_start_date > ...

它必须返回单个值。我认为最好的解决方案是使用 MAX()

AND term_start_date >
    (SELECT MAX(term_start_date) -- USE MAX HERE
    FROM terms
    JOIN vals
    ON terms_id = internal_code
    WHERE internal_code = v1.internal_code
    AND valcode_id = 'TERMS')

使用 MAX() 意味着 term_start_date 必须大于全部 在子查询中找到 term_start_date)。

如果您需要它大于any,请改用MIN()

已编辑我发誓您已经编辑了原始查询!无论如何,尝试在外部选择上使用聚合,如下所示:

SELECT   v1.internal_code,
   (SELECT   MAX(terms_id)
      FROM   terms
      ...

The problem is in subquery that is used here: AND term_start_date > ...

It must return a single value. The best solution I think is to use MAX():

AND term_start_date >
    (SELECT MAX(term_start_date) -- USE MAX HERE
    FROM terms
    JOIN vals
    ON terms_id = internal_code
    WHERE internal_code = v1.internal_code
    AND valcode_id = 'TERMS')

Using MAX() means term_start_date must be greater than all term_start_date found in the subquery).

If you need it to be greater than any, use MIN() instead.

EDITED I swear you have edited your original query! Anyway, try using an aggregate on the outer select, like this:

SELECT   v1.internal_code,
   (SELECT   MAX(terms_id)
      FROM   terms
      ...
咋地 2024-12-08 23:29:09

这很难阅读 - 您需要为所有表引用添加别名。如果您不关心子查询返回哪一行,则可以绕过该错误 - 只需返回 MIN(terms_id) 或 MAX(terms_id) 或其他仅返回 1 行的内容。但是您可能需要更深入地考虑您的数据 - 子查询返回哪个术语真的很重要吗?

SELECT   v1.internal_code,
       (SELECT   MIN(terms_id)
          FROM   terms
         WHERE   term_start_date =
                    (SELECT   MIN (term_start_date)
                       FROM   terms
                      WHERE   terms_id LIKE '%SU'
                              AND term_start_date >
                                    (SELECT   term_start_date
                                       FROM      terms
                                              JOIN
                                                 vals
                                              ON terms_id =
                                                    internal_code
                                      WHERE   internal_code =
                                                 v1.internal_code
                                              AND valcode_id =
                                                    'TERMS')))
          AS mmTerm
FROM      terms
       JOIN
          vals v1
       ON terms_id = internal_code
  WHERE   internal_code LIKE '%SP' AND valcode_id = 'WEB.SEARCH.TERMS'
 ORDER BY   mmTerm ASC

That's quite hard to read - you need to add aliases to all the table references. The error can be gotten around if you don't care about which row the subquery returns - simply return MIN(terms_id) or MAX(terms_id) or whatever to only bring back 1 row. But you may need to think about your data more deeply - does it really matter which term the subquery returns?

SELECT   v1.internal_code,
       (SELECT   MIN(terms_id)
          FROM   terms
         WHERE   term_start_date =
                    (SELECT   MIN (term_start_date)
                       FROM   terms
                      WHERE   terms_id LIKE '%SU'
                              AND term_start_date >
                                    (SELECT   term_start_date
                                       FROM      terms
                                              JOIN
                                                 vals
                                              ON terms_id =
                                                    internal_code
                                      WHERE   internal_code =
                                                 v1.internal_code
                                              AND valcode_id =
                                                    'TERMS')))
          AS mmTerm
FROM      terms
       JOIN
          vals v1
       ON terms_id = internal_code
  WHERE   internal_code LIKE '%SP' AND valcode_id = 'WEB.SEARCH.TERMS'
 ORDER BY   mmTerm ASC
薄情伤 2024-12-08 23:29:09

您是否期望只有一条记录与子查询条件匹配?
您应该将 TOP 1 添加到子查询中:

...
(SELECT TOP 1 terms_id ...

Are you expecting only one record to match the subquery conditions?
Than you should add a TOP 1 to your subquery:

...
(SELECT TOP 1 terms_id ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文