英文:
SQL Subquery issue - Invalid object name
问题
我已准备好以下的“子查询”代码。错误消息显示:“无效的对象名称'group1-2'”。请问如何修复此问题。
select
CASE
WHEN reference_s = 'R1' THEN
(select sum(DATEDIFF(second, datetime2_s, datetime6_s)) from group1_2)
WHEN reference_s = 'R2' THEN
(select sum(DATEDIFF(second, datetime3_s, datetime6_s)) from group1_2)
END
/ (CAST(count(*) AS float)*60)
from
(
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group1
where type_s=2 and size_s=44
UNION
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group2
where type_s=1
) group1_2
where datetime6_s IS NOT null
and datetime6_s >= '2023-03-01 00:00'
and datetime6_s <= '2023-03-30 23:59'
请注意,我稍微更正了其中一个条件,将 cntr_size_s
更正为 size_s
,因为在原始查询中未定义 cntr_size_s
。
英文:
I have prepared the below "subquery" code. Error message: "Invalid object name 'group1-2'" is shown. May I know how to fix the issue.
select
CASE
WHEN reference_s = 'R1' THEN
(select sum(DATEDIFF(second, datetime2_s,datetime6_s) ) from group1_2)
WHEN reference_s = 'R2' THEN
(select sum(DATEDIFF(second, datetime3_s,datetime6_s) ) from group1_2)
END
/ (CAST(count(*) AS float)*60)
from
(
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group1
where type_s=2 and cntr_size_s=44
UNION
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group2
where type_s=1
) group1_2
where datetime6_s IS NOT null
and datetime6_s >= '2023-03-01 00:00'
and datetime6_s <= '2023-03-30 23:59'
答案1
得分: 0
在你的FROM
子句中,你有一个子查询,其结果被称为group1_2
。在你的SELECT
子句中,你有子查询从中选择。这在SQL中是不允许的,因为在SELECT
子句中,你查看表中的单个行(或其聚合行)。SELECT
子句不知道整个表。如果你想定义这样一个临时视图,以便在查询中多次访问它,你必须将其设置为CTE(又称WITH
子句)。
但是你的查询中还有更多问题:
- 你有
from (...) group1 where ... cntr_size_s = 44
,但在group1
中你没有选择任何cntr_size_s
。 - 你有
from (...) group2 where ... cntr_size_s = 44
,但在group2
中你也没有选择任何cntr_size_s
。 - 你有
select case when reference_s = 'R1' ...end ... / cast(count(*) ...)
。所以你访问了group1_2
中的一列和一个聚合结果。但是没有GROUP BY reference_s
,它会为你提供每个reference_s
的聚合结果。那么你如何访问这里的数据,既可以聚合为单行(COUNT(*)
),又可以不聚合(reference_s
)?这是不可能的。GROUP BY reference_s
在语法上可以解决这个问题,但我不知道这是否是你正在寻找的解决方案。
英文:
In your FROM
clause you have a subquery the result of which you call group1_2
. In your SELECT
clause you have subqueries selecting from this. This is not allowed in SQL, because in the SELECT
clause you look at a single row from the table (or an aggregated row of it). The SELECT
clause does not know the whole table. If you want to define such an ad-hoc view that you can access multiple times in your query, you must make this a CTE (aka WITH
clause).
But there are more flaws in your query:
- You have
from (...) group1 where ... cntr_size_s = 44
, but you don't select anycntr_size_s
ingroup1
. - You have
from (...) group2 where ... cntr_size_s = 44
, but you don't select anycntr_size_s
ingroup2
. - You have
select case when reference_s = 'R1' ...end ... / cast(count(*) ...)
. So you access a column fromgroup1_2
and an aggregation result. But there is noGROUP BY reference_s
, which would give you an aggregation result perreference_s
. So how could the data you are accessing here be aggregated to a single row (COUNT(*)
) and not aggregated (reference_s
) at the same time? This is not possible.GROUP BY reference_s
could solve this syntactically, but I don't know whether this is the solution you are looking for.
答案2
得分: 0
从派生表中使用的子查询不允许在SELECT部分中。
除此之外,您的查询中还存在一些逻辑问题。
如果您希望您的查询能够正常运行,可以尝试以下内容:
select
CASE
WHEN reference_s = 'R1' THEN
sum(DATEDIFF(second, datetime2_s, datetime6_s))
WHEN reference_s = 'R2' THEN
sum(DATEDIFF(second, datetime3_s, datetime6_s))
END
/ (CAST(count(*) AS float)*60)
from
(
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group1
where type_s = 2 and size_s = 44
UNION
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group2
where type_s = 1
) group1_2
where datetime6_s IS NOT null
and datetime6_s >= '2023-03-01 00:00'
and datetime6_s <= '2023-03-30 23:59'
GROUP BY id, reference_s
英文:
Subquery which you used from the derived table is not allowed in SELECT part.
Apart from that few more logical issues are there in your query
If you want your query to run without any issue then try below
select
CASE
WHEN reference_s = 'R1' THEN
sum(DATEDIFF(second, datetime2_s,datetime6_s))
WHEN reference_s = 'R2' THEN
sum(DATEDIFF(second, datetime3_s,datetime6_s) )
END
/ (CAST(count(*) AS float)*60)
from
(
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group1
where type_s=2 and cntr_size_s=44
UNION
select * from
(
select
id,
min(reference) as reference_s, min(datetime2) as datetime2_s,
min(datetime3) as datetime3_s, max(datetime6) as datetime6_s,
count(*) as type_s, sum(size) as size_s
from dbo.testing
group by id
) group2
where type_s=1
) group1_2
where datetime6_s IS NOT null
and datetime6_s >= '2023-03-01 00:00'
and datetime6_s <= '2023-03-30 23:59'
GROUP BY id,reference_s
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论