一张表:mytable其他字段不管,假设已知的四个: user_id flag month amount (用户ID 有效性 月份 金额)
这四个字段对应的内容大致形式是:200001 0/1 200501 50 (1表示有效)表里基本是每个user_id都会有几条记录,一条记录就是一个月的(month字段),表记录列举如下:
200001 1 200501 60
200001 1 200502 45
200001 0 200503 72
200002 1 200412 61
.................现在我想查出表里,满足05年1月和2月两个月的金额都大于50元(即,同时满足)的有效用户的总记录数
select flag,count(*) from mytable............后面该怎么写?请各位指教
这四个字段对应的内容大致形式是:200001 0/1 200501 50 (1表示有效)表里基本是每个user_id都会有几条记录,一条记录就是一个月的(month字段),表记录列举如下:
200001 1 200501 60
200001 1 200502 45
200001 0 200503 72
200002 1 200412 61
.................现在我想查出表里,满足05年1月和2月两个月的金额都大于50元(即,同时满足)的有效用户的总记录数
select flag,count(*) from mytable............后面该怎么写?请各位指教
解决方案 »
- Oracle中一次update多条记录?
- 请问如何遍历rowtype或游标中的各个列?
- in, or, temporary table?
- 急救:各位高手帮忙看看这个语句怎么写?
- 为何无法将类型为types的数据倒入到新的数据库中呢?
- oracle10g 使用序列插入数据时,提示此处不允许序列?
- ★★★超难度问题:如何在水晶报表里调用oracle的存储过程, 希望有高手赐教
- Oracle安装问题!哪位哥哥知道?告诉MM一下好吗?高分相送的哦!
- PL/SQL DEVELOPER中调试过程,为什么表里的数据不能更新?
- [求助]DBMS_OUTPUT的错误?(在线等待)
- oracle客户端(如:sql/plus)如何经过一台服务器连接到另一台oracle服务器上?
- 如何用rman恢复表空间?
select user_id,count(*) from mytable where user_id = 1 and amount > 50 and (month = '200501' or month = '200502') group by flag;
select user_id,count(*) from mytable where user_id = 1 and amount > 50 and (month = '200501' or month = '200502') group by user_id;
或
select flag,count(*) from mytable where user_id = 1 and amount > 50 and (month = '200501' or month = '200502') group by flag;
user_id = 1 怎么回事啊?
嗯,按照我上面题目的要求,查询出来的应该只有两条记录,一条flag为0;一条flag为1。
我要的就是看看有效用户的记录数,所以我根据这种结果就看到了。不过没大看懂您的意思,能否分析分析?
select flag,count(distinct user_id) from mytable where month in ('200501','200502') and amount > 50
group by flag
(select user_id,flag,
count(*) over (partition by user_id,flag) user_num
from test1
where amount>50 and month in ('200501','200502'))
where user_num=2
group by flag;
from (select user_id,row_number() over (partition by user_id order by user_id) num
from mytable
where month in ('200501','200502')
and amount>50
and flag=1) a
where a.num=1
前提:一个用户在一个月内只有一条记录方可使用,如果出现2条则下面方法不适合。select count(*)
from (select user_id,row_number() over (partition by user_id order by user_id) num
from mytable
where month in ('200501','200502')
and amount>50
and flag=1) a
where a.num=2
其实不是这个意思!
会出问题!!我已经试过了!
select user_id,user_cnt from
(select tbname.*,count(*) over(partition by user_id) user_cnt from tbname)
where amount>50 and month in ('200501','200502') and flag='1'
group by user_id,user_cnt having count(*)=2
要的是记录数啊,你没看到我问的是
select flag,count(*) from mytable............后面该怎么写?前面这个部分已经定下来是我要的东西了,所以我关键要的是from mytable之后的内容怎么写?重复一遍哦,前面这段定S的了,大家不要改罗
select flag,count(*) from mytable .................
不过一样的啊,我确实要的就是 有多少个用户满足:他1月和2月的两个月金额都大于50,而1月是一条记录,2月是另一条记录啊! 所以其实我希望找出来的数据是有多少个用户,通过select记录数来找。
user_id flag month amount
200001 1 200501 60
200001 1 200502 45
200001 0 200503 72
200002 1 200412 61
200002 1 200501 60
200002 0 200502 77 ----无效?
200002 0 200503 44
200003 1 200501 50 ----??
200003 1 200502 11 ----??
200004 1 200501 66
200004 0 200503 77
...
前几天没空,今天才来,不好意思。我举一个例子吧。==========user_id flag month amount (用户ID 有效性 月份 金额)
200001 1 200501 60
200001 1 200502 45
200001 0 200503 72
200002 1 200412 61
200002 1 200501 49
200002 1 200502 52
200002 1 200503 55
200002 0 200504 60
200002 1 200505 50
200003 0 200411 30
200003 1 200501 55
200003 1 200502 51
200004 0 200501 60
200004 1 200502 69
200005 1 200501 136
200005 1 200502 90
200006 1 200501 56
200006 0 200502 65
200007 0 200501 66
200007 0 200502 80==========
要求:满足05年1月和2月两个月的金额都大于50元(即,同时满足)的有效用户的总记录数分析:
用户 200001 1月虽然大于50元但是2月没过,不算;
用户 200002 2月虽然大于50元但是1月没过,不算;
用户 200003 1月和2月都过了50元了,算;
用户 200004 1月和2月都过了50元了,但是1月不是有效的,不算;
用户 200005 1月和2月都过了50元了,算;
用户 200006 1月和2月都过了50元了,但是2月不是有效的,不算;
用户 200007 1月和2月都过了50元了,但是1月和2月都不是有效的,不算;结论:
这样的有效用户有2个,他们分别是200003和200005用户。
select user_id from table_name where
flag = 1 and month = '200501' and amount >50
intersect
select user_id from table_name where
flag = 1 and month = '200502' and amount >50
(select user_id from table where flag='1' and amount>50 and (month='200501' or month='200502') group by user_id having count(*) >1);
select user_id ,count(user_id) from
(
select user_id from table
where month='200501' and amount>50 and flag='1'
intersect
select user_id from table
where month='200502' and amount>50 and flag='1'
)
group by user_id
2 /USER_ID FLAG MONTH AMOUNT
---------- ---------- ---------- ----------
200001 1 200501 60
200001 1 200502 45
200001 0 200503 72
200002 1 200412 61
200002 1 200501 49
200002 1 200412 61
200002 1 200501 49
200002 1 200502 52
200002 1 200503 55
200002 0 200504 60
200002 1 200505 50USER_ID FLAG MONTH AMOUNT
---------- ---------- ---------- ----------
200003 0 200411 30
200003 1 200501 55
200003 1 200502 51
200004 0 200501 60
200004 1 200502 69
200005 1 200501 136
200005 1 200502 90
200006 1 200501 56
200006 0 200502 65
200007 0 200501 66
200007 0 200502 80已选择22行。SQL> select user_id from tbname
2 where amount>50 and month in ('200501','200502') and flag='1'
3 group by user_id having count(*) = 2
4 /USER_ID
----------
200003
200005SQL> select count(*) from
2 (select user_id from tbname
3 where amount>50 and month in ('200501','200502') and flag='1'
4 group by user_id having count(*) = 2)
5 / COUNT(*)
----------
2