select id as n from tb where n<>1 这种是错误的,
下面的这个为什么有可以呢?
select
a.continent,
sum(PO)
from
(SELECT
name,
PO,
CASE name
WHEN '中国' THEN '亚洲'
WHEN '印度' THEN '亚洲'
WHEN '日本' THEN '亚洲'
WHEN '美国' THEN '北美洲'
WHEN '加拿大' THEN '北美洲'
WHEN '墨西哥' THEN '北美洲'
ELSE '其他' END as continent
FROM @table) a
group by a.continent
下面的这个为什么有可以呢?
select
a.continent,
sum(PO)
from
(SELECT
name,
PO,
CASE name
WHEN '中国' THEN '亚洲'
WHEN '印度' THEN '亚洲'
WHEN '日本' THEN '亚洲'
WHEN '美国' THEN '北美洲'
WHEN '加拿大' THEN '北美洲'
WHEN '墨西哥' THEN '北美洲'
ELSE '其他' END as continent
FROM @table) a
group by a.continent
建议看看逻辑执行顺序吧
因为N还没有生成
(1) FROM <left_table>
(3) <join_type> JOIN <right_table>
(2) ON <join_condition>
(4) WHERE <where_condition>
(5) GROUP BY <group_by_list>
(6) WITH {CUBE | ROLLUP}
(7) HAVING <having_condition>
(10) ORDER BY <order_by_list>
select id as n from tb where n<>1 n 还没有生成
如果是这样就行了select n
from (select id as n from tb) a
where n<>1
as (select id as n from tb)select * from TT where n<>1