以下查询给出错误:
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)按子句排序?
假设您的查询给出的“错误”是它没有像您预期的那样对记录中的记录进行排序B
,A
您需要使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)
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);