现在表t有两列:a,b 数据为:
a  b
1  1
1  1
1  1
1  1          4行
1  2
1  2          2行
1  3          1行
2  1
2  1
2  1
2  1
2  2
2  2
2  3  我想以a列分组 当b列值为1是的总行数 - 当b行不等于1时的总行数(4-2-1),最后结果想得到是:
1     (4-2-1)
2     (4-2-1) 这两行结果 SQl怎么写呀???

解决方案 »

  1.   

    select a,wm_concat(xh||'-') from 
    (select a,count(1)over(PARTITION BY a,b)xh  from table) GROUP BY  a
    或者这样
    select a,wm_concat(xh||'-')over(PARTITION BY a) from 
    (select a,count(1)over(PARTITION BY a,b)xh  from table)
    我没有测试过,你试验一下,看看好用不!
      

  2.   

    select a, REPLACE(wmsys.WM_CONCAT(rn), ',', '-') 
      from ((select a, b, max(rn) rn
               from (select a,
                            b,
                            row_number() over(PARTITION by a, b order by to_char(a) || to_char(b)) rn
                       from test t)
              GROUP by a, b))
     group by a
      

  3.   

    现在表t有两列:a,b 数据为: 
    a  b 
    1  1 
    1  1 
    1  1 
    1  1          4行 
    1  2 
    1  2          2行 
    1  3          1行 
    2  1 
    2  1 
    2  1 
    2  1 
    2  2 
    2  2 
    2  3  我想以a列分组 当b列值为1是的总行数 - 当b行不等于1时的总行数(4-2-1),最后结果想得到是: 
    1    (4-2-1)=1    这里是数字,不是拼串。
    2    (4-2-1)=1 这两行结果 SQl怎么写呀??? 
      

  4.   

    4-2-1=2*4-(4+2+1)2*max(4,2,1)-sum(4,2,1)
      

  5.   

    具体SQL自己拼把,算法基本是这样的
      

  6.   

    SQL> select * from t;
     
             A          B
    ---------- ----------
             1          1
             1          1
             1          1
             1          1
             1          2
             1          2
             1          3
             2          1
             2          1
             2          1
             2          1
             2          2
             2          2
             2          3
     
    14 rows selected
     
    SQL> 
    SQL> select a,
      2         sum(case
      3               when rn = 1 then
      4                cu
      5               else
      6                -cu
      7             end) su
      8    from (select a,
      9                 b,
     10                 cu,
     11                 row_number() over(partition by a order by cu desc) rn
     12            from (select a, b, count(1) cu from t group by a, b order by a))
     13   group by a;
     
             A         SU
    ---------- ----------
             1          1
             2          1
      

  7.   


    select a,max(cu)*2-sum(cu) from 
    (select a, b, count(1) cu from t group by a, b order by a
    ) group by b这样能满足要求,因为b=1时,cu是最大的(这个条件是一定要有) 刚好满足!
    如果没有这个条件,就是楼上的SQL总之:谢谢!
      

  8.   

    linzhangs 分到你那没了, 下次多给你。
      

  9.   

    select priceId_Id,sum(
    case when dealtypeId_Id=1 then cu else -cu end
    ) from(select priceId_Id, dealtypeId_Id, count(1) cu from tb_decticketflow group by priceId_Id, dealtypeId_Id order by priceId_Id)group by priceId_Id