SQL子查询问题 – 无效的对象名称

huangapple go评论124阅读模式
英文:

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 = &#39;R1&#39; THEN
      (select sum(DATEDIFF(second, datetime2_s,datetime6_s) ) from group1_2)
    WHEN reference_s = &#39;R2&#39; 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 &gt;= &#39;2023-03-01 00:00&#39;
and datetime6_s &lt;= &#39;2023-03-30 23:59&#39;

答案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 any cntr_size_s in group1.
  • You have from (...) group2 where ... cntr_size_s = 44, but you don't select any cntr_size_s in group2.
  • You have select case when reference_s = &#39;R1&#39; ...end ... / cast(count(*) ...). So you access a column from group1_2 and an aggregation result. But there is no GROUP BY reference_s, which would give you an aggregation result per reference_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 = &#39;R1&#39; THEN
      sum(DATEDIFF(second, datetime2_s,datetime6_s))
    WHEN reference_s = &#39;R2&#39; 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 &gt;= &#39;2023-03-01 00:00&#39;
and datetime6_s &lt;= &#39;2023-03-30 23:59&#39;
GROUP BY id,reference_s

huangapple
  • 本文由 发表于 2023年6月8日 12:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定