需要有关困难 SQL 查询的帮助
我有一个查询,当前收到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在此处使用的子查询中:
AND term_start_date > ...
它必须返回单个值。我认为最好的解决方案是使用
MAX()
:使用
MAX()
意味着term_start_date
必须大于全部 在子查询中找到term_start_date
)。如果您需要它大于any,请改用
MIN()
。已编辑我发誓您已经编辑了原始查询!无论如何,尝试在外部选择上使用聚合,如下所示:
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()
:Using
MAX()
meansterm_start_date
must be greater than allterm_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:
这很难阅读 - 您需要为所有表引用添加别名。如果您不关心子查询返回哪一行,则可以绕过该错误 - 只需返回 MIN(terms_id) 或 MAX(terms_id) 或其他仅返回 1 行的内容。但是您可能需要更深入地考虑您的数据 - 子查询返回哪个术语真的很重要吗?
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?
您是否期望只有一条记录与子查询条件匹配?
您应该将 TOP 1 添加到子查询中:
Are you expecting only one record to match the subquery conditions?
Than you should add a TOP 1 to your subquery: