如果只有一个以上相同的记录值,则只选择一个记录

时间:2015-11-17 09:12:42

标签: sql sql-server

enter image description here

在sql中,我想要所有记录,但是RM列中的值MCCU出现两次。但是我处于无法区分它的情况,因为misc的值两个值都不一样..

如果MCCU有多个RM,然后只选择列POSImisc列中位置较高的那个,我该如何制作,添加把他们两个价值放在一起。希望能解决它。非常感谢你!

这是我的sql语句

select * from Oclaimc Where cono='NP' and CLNO='7150000032'

2 个答案:

答案 0 :(得分:1)

图片中没有列名misc。我假设你需要总结gamntMiscgttlMisc

试试这个。在需要时添加其他列。

select max(CONO) as CONO,max(CLNO) as CLNO,max(posi) as posi,MCCU,
sum(gamntMisc) as totalgamntMisc,sum(gttlMisc) as totalgttlMisc from Oclaimc
where cono='NP' and CLNO='7150000032'
group by mccu

注意:如果删除where子句,查询将失败。如果您对conoclno的每个组合都需要此结果,请将group by子句更改为 group by cono,clno,mccu

答案 1 :(得分:0)

这可能有所帮助:

 select *
  from oclaimc
 where cono = 'NP'
   and clno = '7150000032'
   and mccu <> 'RM'

 union

 select *
  from (select *
          from oclaimc
         where cono = 'NP'
           and clno = '7150000032'
           and mccu = 'RM'
         order by posi)
 where rownum = 1
相关问题