有4个表 :
test1                        test2                             test3
 id  serviceid mocmd         pid   serviceid  itemid           serviceid  itemid fsStr
 25    1        'A'           25      1          1                1       1      'A'
 26    1        'B'           26      1          1                1       1      'B'      
 27    1        'C'           27      1          1                1       1      'C'
 28    1        'B'           28      1          1                1       1      ''
 29    1        ''            29      1          1                1       2      'a'
 30    1        ''            30      1          1                1       2      'b' 
 31    1        'a'           31      1          2                1       2      ''
 32    1        'b'           32      1          2
 33    1        ''            33      1          2 test4
  itemid  serviceid
   1         1
   2         1其中3个表之间的关系是 :
  test1.id = test2.pid 
  test1.serviceid = test2.serviceid = test3.serviceid
  test2.itemid = test3.itemid
  test3.itemid = test4.itemid
  现在要统计在表test1 中 这些fsStr出现的百分率 ,按itemid  serviceid分组的
  希望得到的结果是 :
    serviceid  itemid  fsStr  num  percent
       1         1       'A'   1     1/(1+2+1+2)
       1         1       'B'   2     2/6
       1         1       'C'   1     1/6
       1         1       ''    2     2/6
       1         2       'a'   1     1/(1+1+1)
       1         2       'b'   1     1/3
       1         2       ''    1     1/3不知道能否实现这样要求,希望高手指点!!!

解决方案 »

  1.   

    select x1.serviceid,x1.itemid,x1.mocmd,x1.num,x1.num/x2.all_num from 
      (select a.serviceid,b.itemid,a.mocmd,count(*) num from test1 a,test2 b
        where a.id=b.pid and a.serviceid=b.serviceid
        group by a.serviceid,b.itemid,a.mocmd) x1,
      (select a.serviceid,b.itemid,count(*) all_num from test1 a,test2 b
        where a.id=b.pid and a.serviceid=b.serviceid
        group by a.serviceid,b.itemid) x2
    where x1.serviceid=x2.serviceid and x1.itemid=x2.itemid
      

  2.   

    谢谢  hfyang9095(嘿嘿哈) ( ) 的回复,的确可以实现我写的,但是实际上是没有实现全的,
    如果在 test3 ,test4表中的数据是这样的话 :test3                                        test4
     1       1      'A'                           1      1
     1       1      'B'                           2      1
     1       1      'C'                           3      1 
     1       1      ''
     1       2      'a'
     1       2      'b' 
     1       2      ''
     1       3      'A'
     1       3      'B'
     1       3      ''上面的话,那么需要得到的结果是 :  serviceid  itemid  fsStr  num  percent
           1         1       'A'   1     1/(1+2+1+2)
           1         1       'B'   2     2/6
           1         1       'C'   1     1/6
           1         1       ''    2     2/6
           1         2       'a'   1     1/(1+1+1)
           1         2       'b'   1     1/3
           1         2       ''    1     1/3
           1         3       'A'   0     0
           1         3       'B'   0     0
           1         3       ''    0     0由于你写的语句没有联合test3个test4,所以如果在表test1和test2中没有出现的话,就不能正确显示了,不知道能否继续改写一下!谢谢!