select sum(decode(floor(col/1000),1,1,2,1,0)) from table1 a,table2 b where a.用户=b.用户
select a.pepole,a.m_max,b.m_min from (select pepole,max(cast(money as integer)) m_max from test where nf='2002' and cast(yf as integer) between 1 and 3 group by pepole) a, (select pepole,min(cast(money as integer)) m_min from test where nf='2002' and cast(yf as integer) between 1 and 3 group by pepole) b where a.pepole=b.pepole and a.m_max<2000 and b.m_min>1000 我在db2环境下写的,通过了测试,你改一下。 cast(money as integer)是把数字转换为integer类型,你可去掉。
select * from test -------------------------- NF YF PEPOLE MONEY ---- -- ---------- ---------- 2002 1 Wang 1500 2002 2 Wang 1600 2002 3 Wang 1700 2002 1 Li 1700 2002 2 Li 800 2002 3 Li 1200 6 条记录已选择。 -------------------------- 楼上结果: PEPOLE M_MAX M_MIN ---------- ----------- ----------- Wang 1700 1500 1 条记录已选择。
浪子兄:突然想起来了,在a和b的条件中还要加上money is not null这个条件的,不然,当某个月的money is null的时候,也可以检索出max()和min()的,所以这应该不符合楼主的要求的。现在晕晕乎乎的,不知道对不对,搂住试试吧 :)
TO jiezhi(西域浪子) :难道只能通过关联进行过滤吗?如果此表中有80万用户,这种方法会导致数据库性能急剧下降的.
1月份a用户消费 1500元
2月份a用户消费 200元
3月份a用户消费 1500 元
此种情况不应统计
只有以下情况应统计
1月份a用户消费 1500元
2月份a用户消费 1800元
3月份a用户消费 1500 元
(select pepole,max(cast(money as integer)) m_max
from test
where nf='2002' and cast(yf as integer) between 1 and 3
group by pepole) a,
(select pepole,min(cast(money as integer)) m_min
from test
where nf='2002' and cast(yf as integer) between 1 and 3
group by pepole) b
where a.pepole=b.pepole
and a.m_max<2000 and b.m_min>1000
我在db2环境下写的,通过了测试,你改一下。
cast(money as integer)是把数字转换为integer类型,你可去掉。
--------------------------
NF YF PEPOLE MONEY
---- -- ---------- ----------
2002 1 Wang 1500
2002 2 Wang 1600
2002 3 Wang 1700
2002 1 Li 1700
2002 2 Li 800
2002 3 Li 1200 6 条记录已选择。
--------------------------
楼上结果:
PEPOLE M_MAX M_MIN
---------- ----------- -----------
Wang 1700 1500 1 条记录已选择。