0

以下查询给出错误:

SELECT DISTINCT dealer_state
FROM A
WHERE country='SS' 
UNION
SELECT DISTINCT dealer_state
FROM B
WHERE country='SS'
ORDER BY upper(dealer_state)

如何使用upper(column)按子句排序?

4

2 回答 2

2

假设您的查询给出的“错误”是它没有像您预期的那样对记录中的记录进行排序BA您需要使UNION查询成为执行排序的子查询:

SELECT dealer_state FROM (
    SELECT dealer_state FROM A WHERE country='SS' 
  UNION
    SELECT dealer_state FROM B WHERE country='SS'
) t
ORDER BY upper(dealer_state)
于 2012-04-25T10:36:07.123 回答
1

DISTINCT和中最左边的表达式ORDER BY必须匹配。

最简单的方法是执行以下操作:

SELECT dds FROM (
    SELECT DISTINCT dealer_state AS dds FROM A
     WHERE country='SS'
    UNION
    SELECT DISTINCT dealer_state AS dds FROM B
     WHERE country='SS') AS d
ORDER BY upper(dds);
于 2012-04-25T10:38:16.623 回答