当前位置: 首页 > 面试题库 >

带有UNION ALL的CASE的SQL ORDER BY

夹谷辰沛
2023-03-14
问题内容

运行PostgreSQL(7.4和8.x),我以为这是可行的,但是现在我遇到了错误。

我可以单独运行查询,它可以正常工作,但是如果我使用UNION或UNION ALL,则会引发错误。

出现此错误:(警告:pg_query():查询失败:错误:列“ Field1”不存在…按情况订购“ Field1” W …)

SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName" 
WHERE condition
AND other_condition
UNION ALL
SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName" 
WHERE yet_another_condition
AND yet_another_other_condition
ORDER BY CASE "Field1"
    WHEN 'A' THEN 1
    WHEN 'B' THEN 2
    WHEN 'C' THEN 3
    ELSE 4
END

这有效:

SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName" 
WHERE yet_another_condition
AND yet_another_other_condition
ORDER BY CASE "Field1"
    WHEN 'A' THEN 1
    WHEN 'B' THEN 2
    WHEN 'C' THEN 3
    ELSE 4
END

这也有效:

SELECT "Field1" AS field_1, "Field2" AS field_2,
"Field3" AS field_3, "Field4" AS field_4
FROM "TableName" 
WHERE condition
AND other_condition
ORDER BY CASE "Field1"
    WHEN 'A' THEN 1
    WHEN 'B' THEN 2
    WHEN 'C' THEN 3
    ELSE 4
END

如果我不使用ORDER BY,而只使用UNION或UNION ALL,那么它也可以正常工作。

有任何想法吗?


问题答案:

将所有内容放入另一个SELECT中:

SELECT * FROM (
  SELECT "Field1" AS field_1, "Field2" AS field_2,
  "Field3" AS field_3, "Field4" AS field_4
  FROM "TableName" 
  WHERE condition
  AND other_condition
  UNION ALL
  SELECT "Field1" AS field_1, "Field2" AS field_2,
  "Field3" AS field_3, "Field4" AS field_4
  FROM "TableName" 
  WHERE yet_another_condition
  AND yet_another_other_condition
) As A
ORDER BY CASE field_1
    WHEN 'A' THEN 1
    WHEN 'B' THEN 2
    WHEN 'C' THEN 3
    ELSE 4
END

或者,最好在ORDER BY中使用别名,因为它是在UNION的末尾传递的:

  SELECT "Field1" AS field_1, "Field2" AS field_2,
  "Field3" AS field_3, "Field4" AS field_4
  FROM "TableName" 
  WHERE condition
  AND other_condition
  UNION ALL
  SELECT "Field1" AS field_1, "Field2" AS field_2,
  "Field3" AS field_3, "Field4" AS field_4
  FROM "TableName" 
  WHERE yet_another_condition
  AND yet_another_other_condition
  ORDER BY CASE field_1
    WHEN 'A' THEN 1
    WHEN 'B' THEN 2
    WHEN 'C' THEN 3
    ELSE 4
  END


 类似资料:
  • 在短 我想在thymeleaf中有一个switch语句,逻辑一旦写入多个case语句。 详细地 我想在thymeleaf中实现这一点 我当前的胸腺叶代码失败并出现运行时错误 但是上面的代码错误地失败了 注意:我知道出现上述错误消息的原因。我所需要的只是知道一种方法来实现单输出的多情况切换

  • 我想将FieldMmap类的集合保存为json字符串- ... etc-完整代码:https://github.com/alexeyOnGitHub/scala-typesafe/blob/master/src/main/scala/com/example/model/Field.scala Circe代码: 错误:(14,65)找不到io类型的延迟隐式值。circe。通用的解码。DerivedD

  • QueryDsl 3.3.4 Hibernate 3.6.10-Final I有两个实体: org.hibernate.hql.ast.tree.SelectClause.InitializeExplicitSelectClause(SelectClause.java:156)

  • 可以将case statement作为外部case statement的语句序列的一部分。 即使内部和外部的case constants包含共同的值,也不会产生冲突。 语法 (Syntax) 嵌套case语句的语法如下 - case (ch1) of 'A': begin writeln('This A is part of outer case' ); cas

  • 现在我的问题是,如何在自定义的而不是自定义的中重写方法?我没有在这里公布我的代码,因为它与链接的代码本质上是相同的,只是我需要为子创建一个自定义的来代替,这样它就可以按照“pptang”的答案所述进行正确的度量。 否则,有没有比在第二个RecyclerView中使用1个RecyclerView更好的方法?只能有1个RecyclerView使用上述列表和每个中唯一项的网格填充活动/片段吗?