一张表: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............后面该怎么写?请各位指教

解决方案 »

  1.   

    这样应该可以满足你的求
    select user_id,count(*) from mytable where user_id = 1 and amount > 50 and (month = '200501' or month = '200502') group by flag;
      

  2.   

    呵呵不好意思,分组错了
    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;
      

  3.   

    楼上的,不好意思没看懂。
    user_id = 1 怎么回事啊?
    嗯,按照我上面题目的要求,查询出来的应该只有两条记录,一条flag为0;一条flag为1。
    我要的就是看看有效用户的记录数,所以我根据这种结果就看到了。不过没大看懂您的意思,能否分析分析?
      

  4.   

    有效用户的总记录数,是用户数?
    select flag,count(distinct user_id) from mytable where month in ('200501','200502') and amount > 50
    group by flag
      

  5.   

    select flag,count(distinct user_id) from 
    (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;
      

  6.   

    select count(*) from (select user_id, count(*) ct from mytable where amount>50 and month in ('200501','200502') and flag='1' group by user_id having count(*)=2);
      

  7.   

    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=1
      

  8.   

    SORRY,忘记是1月2月都有的记录更正如下
    前提:一个用户在一个月内只有一条记录方可使用,如果出现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
      

  9.   

    to  duanzilin(寻) 你那样分类会出问题的,distinct user_id 会导致只搜到一个记录就停止了!
    其实不是这个意思!
      

  10.   

    to all:   不能用distinct!
        会出问题!!我已经试过了!
      

  11.   

    满足05年1月和2月两个月的金额都大于50元(即,同时满足)的有效用户,然后求这些用户在原始表的总记录数?
    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
      

  12.   

    to 各位:
    要的是记录数啊,你没看到我问的是
    select flag,count(*) from mytable............后面该怎么写?前面这个部分已经定下来是我要的东西了,所以我关键要的是from mytable之后的内容怎么写?重复一遍哦,前面这段定S的了,大家不要改罗
    select flag,count(*) from mytable .................
      

  13.   

    to all:
       不过一样的啊,我确实要的就是 有多少个用户满足:他1月和2月的两个月金额都大于50,而1月是一条记录,2月是另一条记录啊!  所以其实我希望找出来的数据是有多少个用户,通过select记录数来找。
      

  14.   

    还是没说明白这样吧,我给1组数据,你把结果贴出来吧
    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 
    ...
      

  15.   

    to all:
       前几天没空,今天才来,不好意思。我举一个例子吧。==========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用户。
      

  16.   

    --flag为1的符合条件的用户
    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 
      

  17.   

    select '1',count(*) from 
    (select user_id from table where flag='1' and amount>50 and (month='200501' or month='200502') group by user_id having count(*) >1);
      

  18.   

    用交集运算.
    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
      

  19.   

    SQL> select * from tbname
      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