我有以下的一个表数据:
partno         jgno                 materspec
8704235C5       01            1/5L                          
8704235C5       02            7628Y                         
8704235C5       03            2116Y                         
8704235C5       04            6/7L                          
8704235C5       05            7628Y                         
8704235C5      06            3313Y                         
8704235C5       07            1     
我希望得到 jgno='01'  对应的materspec 数据1/5L  和 jgno='07'  对应的materspec 数据1,
也就是 jbno最小和最大的materspec值。希望最后得到的结构如下:
pdctno       minmaterspec     maxmaterspec
8704235C5    1/5L             1 谢谢!

解决方案 »

  1.   

    select pdctno,min(materspec) minmaterspec,max(materspec) maxmaterspec from 表 group by pdctno
      

  2.   

    --for example:
    表中测试数据
    SQL> select * from test_tab;PARTNO                     JGNO MATERSPEC
    -------------------- ---------- ----------
    8704235C5                     1 1/5L
    8704235C5                     2 7628Y
    8704235C5                     3 2116Y
    8704235C5                     4 6/7L
    8704235C5                     5 7628Y
    8704235C5                     6 3313Y
    8704235C5                     7 1
    8704235C8                     1 1/5L
    8704235C8                     2 7628Y
    8704235C8                     3 2116Y
    8704235C8                     4 6/7L
    8704235C8                     5 7628Y
    8704235C8                     6 3313Y
    8704235C8                     7 1--查询语句
    select partno,
           min(decode(rn,1,materspec)) minmaterspec,
           max(decode(rn,0,materspec)) minmaterspec
    from ( 
           select mod(rownum,2) rn,a.* 
           from   test_tab a ,
                 (
                  select partno,
                         min(jgno) min_jgno ,
                         max(jgno) max_jgno 
                  from   test_tab 
                  group by partno
                 ) b
           where  a.partno = b.partno 
           and   ( a.jgno=b.min_jgno or a.jgno=b.max_jgno)
           order by a.partno,a.jgno
          )
    group by partno
    ---
    SQL> select partno,
      2         min(decode(rn,1,materspec)) minmaterspec,
      3         max(decode(rn,0,materspec)) minmaterspec
      4  from (
      5         select mod(rownum,2) rn,a.*
      6         from   test_tab a ,
      7               (
      8                select partno,
      9                       min(jgno) min_jgno ,
     10                       max(jgno) max_jgno
     11                from   test_tab
     12                group by partno
     13               ) b
     14         where  a.partno = b.partno
     15         and   ( a.jgno=b.min_jgno or a.jgno=b.max_jgno)
     16         order by a.partno,a.jgno
     17        )
     18  group by partno
     19  /PARTNO               MINMATERSPEC MINMATERSPEC
    -------------------- ------------ ------------
    8704235C5            1/5L         1
    8704235C8            1/5L         1
      

  3.   

    VFP是什么意思?
    SQL> select * from test_tab where MATERSPEC like '%/%L'
      2  /PARTNO                     JGNO MATERSPEC
    -------------------- ---------- ----------
    8704235C5                     1 1/5L
    8704235C5                     4 6/7L
    8704235C8                     1 1/5L
    8704235C8                     4 6/7L
      

  4.   

    select pdctno,min(materspec) minmaterspec,max(materspec) maxmaterspec from 表 group by pdctno
    这样不可以吗?
    包子兄搞得这么复杂,为什么?