问一sql语句

来源:百度知道 编辑:UC知道 时间:2024/06/25 23:13:30
表名为test 包括属性aid,num,ts,year
现在想获取表中aid为123,year为2009的所有行其中还要另加一个属性mnum来记录num大于100的个数
例如:123,98,abc,2009
123,103,dn,2009
123,109,af,2009
执行后返回结果应为
aid,num,ts,year,mnum
123,103,dn,2009,2
123,109,af,2009,2

select *
from test t,
(select count(*)
from test t,
where t.aid = '123'
and t.year = '2009'
and num > '100') cc
where t.aid = '123'
and t.year = '2009'
and num > '100'

大概就是这么个意思!试一下行不

刚写错了。。。 oracle11G支持刚刚的sql语句

select aid,num,ts,year,count(num) mnum from test where aid= 123 and year = 2009 and num > 100
如果是字符串请加上引号 那个count(num) mnum 中间要不要加as 我忘了 你试试看吧。

--重新写下。
select t1.aid,t1.num,t1.ts,t1.[year],t2.mnum from test t1 inner join (select aid,sum(case when num>100 then 1 else 0 end) mnum from test where aid='123' and [year]='2009' group by aid) t2 on (t1.aid=t2.aid and t1.[year]=t2.[year]);

----
纠正下楼上
select *,(select count(*) mnum from test where aid= 123 and [year] = 2009 and num > 100) mnum from test where aid= 123 and [year] = 2009;

<