0

I'm trying to retrieve only unique records from a table, but I guess something is wrong with my query.

select distinct RIID, duplicateInfo from duplicateRecords where RIID > 3920011

When I execute above query I get this result

RIID   |   duplicateInfo 
___________________________________
3920011    Repeated:12009:CLEAR
3920011    Repeated:12012:CLEAR
4233901    Repeated:18129:HIT
4820129    Repeated:22901:PENDING
4820129    Repeated:22983:PENDING

And I want the below result

RIID   |   duplicateInfo 
___________________________________    
3920011    Repeated:12012:CLEAR
4233901    Repeated:18129:HIT
4820129    Repeated:22983:PENDING

Please any help would be highly appreciated.

Thanks

Fazil Mir
  • 783
  • 2
  • 9
  • 23

3 Answers3

1
select distinct RRID,
    (select duplicateInfo 
    from duplicateRecords m 
    where m.RIID = duplicateRecords.RRID 
    having cast(substring(duplicateInfoNumber,10,6) as int) = min(cast(substring(duplicateInfoNumber,10,6) as int)))
from duplicateRecords
where RRID > 3920011
McNets
  • 10,352
  • 3
  • 32
  • 61
0

I don't have sybase to test this with. Here is an example from mysql to give you some pointers.

DROP TABLE IF EXISTS `duplicaterecords`;

CREATE TABLE `duplicaterecords` (
  `RRID` int(11) DEFAULT NULL,
  `duplicateInfo` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `duplicaterecords` (`RRID`, `duplicateInfo`)
VALUES
    (3920011,'Repeated:12009:CLEAR'),
    (3920011,'Repeated:12012:CLEAR'),
    (4233901,'Repeated:18129:HIT'),
    (4820129,'Repeated:22901:PENDING'),
    (4820129,'Repeated:22983:PENDING'),
    (4233901,'Duplicate:5555555:CLEAR');

    select grouped.*
, base.duplicateInfo 
from 
( 
    select grouped.RRID, max(grouped.duplicateInfoId) duplicateInfoId
    from (
        select RRID
        , cast(substring_index(substring_index(duplicateInfo,':',2 ),':',-1) as unsigned) duplicateInfoId
        , duplicateInfo 
        from duplicateRecords  
    ) grouped
    group by grouped.RRID
) grouped
inner join (
    select RRID
    , cast(substring_index(substring_index(duplicateInfo,':',2 ),':',-1) as unsigned) duplicateInfoId
    , duplicateInfo 
    from duplicateRecords  
) base
on grouped.duplicateInfoId = base.duplicateInfoId ;

-- example results

RRID    duplicateInfoId duplicateInfo
3920011 12012   Repeated:12012:CLEAR
4233901 5555555 Duplicate:5555555:CLEAR
4820129 22983   Repeated:22983:PENDING
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
0

There is a simpler and more efficient way -- but only if you're running on Sybase ASE (doesn't work for Sybase IQ or Sybase SQL Anywhere). First, this is a 'duplicate key' problem, not a 'duplicate row' problem. The trick below will remove all rows with duplicate keys. But note that it is not defined which row to choose in case of duplicate keys -- so the first one is kept, the rest is discarded. So you should apply some ordering in the SELECT query in order to implement a different selection criterium

CREATE TABLE uniquetab (RRID ..., duplicateInfo ...) go CREATE UNIQUE INDEX ix on uniquetab(RRID) WITH IGNORE_DUP_KEY go

INSERT uniquetab SELECT * FROM duplicateRecords ORDER BY go

An alternative way is to BCP-out the duplicateRecords table, and then to BCP it into the uniquetab table.

RobV
  • 2,263
  • 1
  • 11
  • 7