简单的sql问题
Create Table #t
(ID Varchar(10),
Value Int)
--插入數據
Insert #t Select 'C1 ',0
Union All Select 'C2 ',1
Union All Select 'C3 ',20
Union All Select 'C1 ',10
Union All Select 'C2 ',11
Union All Select 'C3 ',12
Union All Select 'C3 ',12
GO
select (select count(*) from #t where id=a.id and value <a.value) as ccc,max(id),max(value) from #t a group by ccc
只是举了这么个例子,我想问为什么group by ccc 会出现ccc为无效呢?
不能用这样的方法吗?那我该如何解决呢,谢谢
[解决办法]
group by后面不能使用列别名,只能使用列表达式.
[解决办法]
Create Table #t
(ID Varchar(10),
Value Int)
--插入數據
Insert #t Select 'C1 ',0
Union All Select 'C2 ',1
Union All Select 'C3 ',20
Union All Select 'C1 ',10
Union All Select 'C2 ',11
Union All Select 'C3 ',12
Union All Select 'C3 ',12
GO
select ccc,max(id),max(value)
from (
select (select count(*) from #t where id=a.id and value <a.value) as ccc,* from #t a
) as t
group by ccc