请问这个sql怎么写?
A表如下,有4个字段
ID   VALUE1   VALUE2   VALUE3
1    4        5        4
1    1        2        5
2    12       45       4
3    47       4        5
3    7        6        9
3    3        5        6我希望select出来后的结果可以多一个字段COUNT,表示对相同ID的数量的统计,比如第一行2表示ID=2的有2条记录:
ID   VALUE1   VALUE2   VALUE3   COUNT
1    4        5        4        2
1    1        2        5        2
2    12       45       4        1
3    47       4        5        3
3    7        6        9        3
3    3        5        6        3

解决方案 »

  1.   

    select ID,   VALUE1,   VALUE2 ,  VALUE3,   COUNT(*) over (partition by id)  COUNT
    from A
      

  2.   

    select tb.*,count(*) over(PARTITION BY id ORDER BY id) as count from tb
      

  3.   

    select a.*,count(id) over(partition by id) 
    from 
    a
      

  4.   

    有个效率差的方法
    select tab1.*, tab2.cnt from (select * from a) tab1, (select id, count(*) cnt from a) tab2
    where tab1.id = tab2.id
      

  5.   

    谢谢各位,再深入一点,不好意思,分不够我再加:
    就是现在有另外一张表b
    ID   VALUE4
    1    4
    2    5
    3    8
    现在如何根据ID关联,把b表的value同时取出来,即结果如下:
    ID   VALUE1   VALUE2   VALUE3   COUNT   VALUE4
    1    4        5        4        2       4
    1    1        2        5        2       4
    2    12       45       4        1       5
    3    47       4        5        3       8
    3    7        6        9        3       8
    3    3        5        6        3       8初学者,多谢各位了。
      

  6.   

    select a.ID,   a.VALUE1,   a.VALUE2 ,  a.VALUE3,   COUNT(*) over (partition by a.id)  COUNT ,b.VALUE4
    from A ,b 
    where a.ID = b.id(+)