How can I prevent a record from appearing based on two columns

  • Sorry for the vague Subject line but, I couldn' t come up with a good summary. In the table the PrescriptionID and DuplicatePrescriptionID repeat themselves. Once as PrescriptionID and DuplicatePrescriptionID then, as DuplicatePrescriptionID and PrescriptionID. It will be apparent when you look at the table. How can I get a "unique" list of records that counts the "duplicate pair" only once?

    create table #T

    (

    VisitID varchar(30),

    PrescriptionID varchar(30),

    DuplicateSeqID int,

    DuplicateName varchar(30),

    DuplicatePrescriptionID varchar(30),

    DuplicateType char(1)

    )

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',1,'IBUPROFEN','594060','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594060','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',3,'IBUPROFEN','594060','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',1,'IBUPROFEN','594059','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594059','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',3,'IBUPROFEN','594059','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',1,'QUEtiapine Fumarate','594065','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',2,'ANTIPSYCHOTIC AGENTS','594065','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',3,'QUETIAPINE','594065','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',1,'QUEtiapine Fumarate','594064','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',2,'ANTIPSYCHOTIC AGENTS','594064','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',3,'QUETIAPINE','594064','I')

  • NineIron - Monday, January 30, 2017 10:21 AM

    Sorry for the vague Subject line but, I couldn' t come up with a good summary. In the table the PrescriptionID and DuplicatePrescriptionID repeat themselves. Once as PrescriptionID and DuplicatePrescriptionID then, as DuplicatePrescriptionID and PrescriptionID. It will be apparent when you look at the table. How can I get a "unique" list of records that counts the "duplicate pair" only once?

    create table #T

    (

    VisitID varchar(30),

    PrescriptionID varchar(30),

    DuplicateSeqID int,

    DuplicateName varchar(30),

    DuplicatePrescriptionID varchar(30),

    DuplicateType char(1)

    )

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',1,'IBUPROFEN','594060','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594060','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',3,'IBUPROFEN','594060','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',1,'IBUPROFEN','594059','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594059','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',3,'IBUPROFEN','594059','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',1,'QUEtiapine Fumarate','594065','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',2,'ANTIPSYCHOTIC AGENTS','594065','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',3,'QUETIAPINE','594065','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',1,'QUEtiapine Fumarate','594064','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',2,'ANTIPSYCHOTIC AGENTS','594064','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',3,'QUETIAPINE','594064','I')

    Could you tell us the "answer" (the correct set) and then maybe we can figure it out?

  • pietlinden - Monday, January 30, 2017 10:31 AM

    NineIron - Monday, January 30, 2017 10:21 AM

    Sorry for the vague Subject line but, I couldn' t come up with a good summary. In the table the PrescriptionID and DuplicatePrescriptionID repeat themselves. Once as PrescriptionID and DuplicatePrescriptionID then, as DuplicatePrescriptionID and PrescriptionID. It will be apparent when you look at the table. How can I get a "unique" list of records that counts the "duplicate pair" only once?

    create table #T

    (

    VisitID varchar(30),

    PrescriptionID varchar(30),

    DuplicateSeqID int,

    DuplicateName varchar(30),

    DuplicatePrescriptionID varchar(30),

    DuplicateType char(1)

    )

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',1,'IBUPROFEN','594060','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594060','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594059',3,'IBUPROFEN','594060','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',1,'IBUPROFEN','594059','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594059','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594060',3,'IBUPROFEN','594059','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',1,'QUEtiapine Fumarate','594065','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',2,'ANTIPSYCHOTIC AGENTS','594065','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594064',3,'QUETIAPINE','594065','I')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',1,'QUEtiapine Fumarate','594064','G')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',2,'ANTIPSYCHOTIC AGENTS','594064','C')

    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType) values('F1-B20170128151307684','594065',3,'QUETIAPINE','594064','I')

    Could you tell us the "answer" (the correct set) and then maybe we can figure it out?

    Not entirely sure if I'm supposed to UNION the two columns together and then do a GROUP BY or just a plain GROUP
    The first option would be like this:
    SELECT VisitID, PrescriptionID, COUNT(*) As dupecount
    FROM (
    SELECT VisitID, PrescriptionID
    FROM #T
    UNION ALL SELECT VisitID, DuplicatePrescriptionID
    FROM #T) x
    GROUP BY x.visitID, PrescriptionID;

  • Can you elaborate a little bit further on the problem and the expected output, not entirely clear what you are after?
    😎

    Quick suggestions, not certain though
    create table #T
    (
      VisitID varchar(30),
      PrescriptionID varchar(30),
      DuplicateSeqID int,
      DuplicateName varchar(30),
      DuplicatePrescriptionID varchar(30),
      DuplicateType char(1)
    )
    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType)
    values('F1-B20170128151307684','594059',1,'IBUPROFEN','594060','G')
      ,('F1-B20170128151307684','594059',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594060','C')
      ,('F1-B20170128151307684','594059',3,'IBUPROFEN','594060','I')
      ,('F1-B20170128151307684','594060',1,'IBUPROFEN','594059','G')
      ,('F1-B20170128151307684','594060',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594059','C')
      ,('F1-B20170128151307684','594060',3,'IBUPROFEN','594059','I')
      ,('F1-B20170128151307684','594064',1,'QUEtiapine Fumarate','594065','G')
      ,('F1-B20170128151307684','594064',2,'ANTIPSYCHOTIC AGENTS','594065','C')
      ,('F1-B20170128151307684','594064',3,'QUETIAPINE','594065','I')
      ,('F1-B20170128151307684','594065',1,'QUEtiapine Fumarate','594064','G')
      ,('F1-B20170128151307684','594065',2,'ANTIPSYCHOTIC AGENTS','594064','C')
      ,('F1-B20170128151307684','594065',3,'QUETIAPINE','594064','I')
    ;
    SELECT
      T.VisitID    
     ,T.PrescriptionID    
     ,T.DuplicateSeqID    
     ,T.DuplicateName    
     ,T.DuplicatePrescriptionID    
     ,T.DuplicateType
    FROM  #T T
    WHERE T.DuplicateSeqID = 1

    ;WITH BASE_DATA AS
    (
      SELECT
       T.VisitID    
       ,ROW_NUMBER() OVER
        (
          PARTITION BY T.PrescriptionID
              ,T.DuplicatePrescriptionID
          ORDER BY  T.DuplicateSeqID ASC
        ) AS RID
       ,T.PrescriptionID    
       ,T.DuplicateSeqID    
       ,T.DuplicateName    
       ,T.DuplicatePrescriptionID    
       ,T.DuplicateType
      FROM  #T T
    )
    SELECT
      BD.VisitID    
     ,BD.PrescriptionID    
     ,BD.DuplicateSeqID    
     ,BD.DuplicateName    
     ,BD.DuplicatePrescriptionID    
     ,BD.DuplicateType
    FROM  BASE_DATA BD
    WHERE BD.RID = 1

    DROP TABLE #T;

    Output (same for both)

    VisitID                        PrescriptionID                 DuplicateSeqID DuplicateName                  DuplicatePrescriptionID        DuplicateType
    ------------------------------ ------------------------------ -------------- ------------------------------ ------------------------------ -------------
    F1-B20170128151307684          594059                         1              IBUPROFEN                      594060                         G
    F1-B20170128151307684          594060                         1              IBUPROFEN                      594059                         G
    F1-B20170128151307684          594064                         1              QUEtiapine Fumarate            594065                         G
    F1-B20170128151307684          594065                         1              QUEtiapine Fumarate            594064                         G

  • Eirikur Eiriksson - Monday, January 30, 2017 10:39 AM

    Can you elaborate a little bit further on the problem and the expected output, not entirely clear what you are after?
    😎

    Quick suggestions, not certain though
    create table #T
    (
      VisitID varchar(30),
      PrescriptionID varchar(30),
      DuplicateSeqID int,
      DuplicateName varchar(30),
      DuplicatePrescriptionID varchar(30),
      DuplicateType char(1)
    )
    insert into #T(VisitID, PrescriptionID, DuplicateSeqID, DuplicateName, DuplicatePrescriptionID, DuplicateType)
    values('F1-B20170128151307684','594059',1,'IBUPROFEN','594060','G')
      ,('F1-B20170128151307684','594059',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594060','C')
      ,('F1-B20170128151307684','594059',3,'IBUPROFEN','594060','I')
      ,('F1-B20170128151307684','594060',1,'IBUPROFEN','594059','G')
      ,('F1-B20170128151307684','594060',2,'NONSTEROIDAL ANTI-INFLAMMATORY','594059','C')
      ,('F1-B20170128151307684','594060',3,'IBUPROFEN','594059','I')
      ,('F1-B20170128151307684','594064',1,'QUEtiapine Fumarate','594065','G')
      ,('F1-B20170128151307684','594064',2,'ANTIPSYCHOTIC AGENTS','594065','C')
      ,('F1-B20170128151307684','594064',3,'QUETIAPINE','594065','I')
      ,('F1-B20170128151307684','594065',1,'QUEtiapine Fumarate','594064','G')
      ,('F1-B20170128151307684','594065',2,'ANTIPSYCHOTIC AGENTS','594064','C')
      ,('F1-B20170128151307684','594065',3,'QUETIAPINE','594064','I')
    ;
    SELECT
      T.VisitID    
     ,T.PrescriptionID    
     ,T.DuplicateSeqID    
     ,T.DuplicateName    
     ,T.DuplicatePrescriptionID    
     ,T.DuplicateType
    FROM  #T T
    WHERE T.DuplicateSeqID = 1

    ;WITH BASE_DATA AS
    (
      SELECT
       T.VisitID    
       ,ROW_NUMBER() OVER
        (
          PARTITION BY T.PrescriptionID
              ,T.DuplicatePrescriptionID
          ORDER BY  T.DuplicateSeqID ASC
        ) AS RID
       ,T.PrescriptionID    
       ,T.DuplicateSeqID    
       ,T.DuplicateName    
       ,T.DuplicatePrescriptionID    
       ,T.DuplicateType
      FROM  #T T
    )
    SELECT
      BD.VisitID    
     ,BD.PrescriptionID    
     ,BD.DuplicateSeqID    
     ,BD.DuplicateName    
     ,BD.DuplicatePrescriptionID    
     ,BD.DuplicateType
    FROM  BASE_DATA BD
    WHERE BD.RID = 1

    DROP TABLE #T;

    Output (same for both)

    VisitID                        PrescriptionID                 DuplicateSeqID DuplicateName                  DuplicatePrescriptionID        DuplicateType
    ------------------------------ ------------------------------ -------------- ------------------------------ ------------------------------ -------------
    F1-B20170128151307684          594059                         1              IBUPROFEN                      594060                         G
    F1-B20170128151307684          594060                         1              IBUPROFEN                      594059                         G
    F1-B20170128151307684          594064                         1              QUEtiapine Fumarate            594065                         G
    F1-B20170128151307684          594065                         1              QUEtiapine Fumarate            594064                         G

    ='font-size:9.0pt;mso-bidi-font-size:11.0pt;line-height:107%;font-family:"courier>

    Here is what I need. 
    Looking at the first record, PrescriptionID=594059. The patient is given ibuprofen then, the MD orders ibuprofen again, DuplicatePrescriptionID=594060. If you look at the way the data is stored in the table there is a combination of PrescriptionID=594060 and the DuplicatePrescriptionID=594059. It's true that they are both duplicates of each other but, because 594059 is a lower number than 594060, 594060 should be the duplicate.
    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAq0AAABjCAYAAACxITlGAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABkOSURBVHhe7dxdstO6EgVguMNgBLwx/1nAE0wApnFuNbCKtdfu1p+VxE7WV6WK3Wq1LckJqVM5++PX7z//+2BmZmZmdmK/v7R++fzp76nZH99+/Prg56LP63Sc19Cs5vfHc/P+jou1+t/fYzMzMzOz0/KXVrMT+vjx498jW+U1NKv5/fHcnnV/33xp1UnivDX5KifO0fQcMbuGR+1d71rav3pvmBfG8zliI1bHtazUmRnD97tyraDj+RyxUUfGVlbqjI7J7nPXfZspPG/Zc7dqpc7oGL7Xleus6l1L+1fvDfPCeD5HbMbR8ZnV+xjB94p2a0P/pfW//+r/V6vqQ5z74xjtHpOzfR6xd3GtGbP5gHE8Po7RZua7Oq4SdW4l7o/vd/VaGMfjuebsOhwZm4k6Zs/iau+P3fc7YnZOq2uAcTw+jtFW5nt0vIo6t4L71ONbGvrSygsXx2g4B473xORGc+2cWs+A7m0W11jrlRvjGPchXsU43rP6rPI4Hc9xbopjWV4WC6N5lSq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zqn4WtzXDPw/I8Eb0xEaN5N3L2e7nlnSfnm3eOr+gMe6bsauOnR/+MYlXPgZ9FszsOWTvbY1xnz3OoS+to3Z+QdxRa9f98D9oI2bzw861u7KRtfNandPMcx/7p/n33Ne4DrcjYvzM3M3sdkbei7f4rPFnwF7DX1qx8L0NiH403nyORwv4UEfTfH5tWanD+eibrbMrv1Uns6vOlbTmNjPfqk6rvh0zu7atfO3Tfu1rQX8vr2W2RuRxM7P7aH2uZLFKVWc2nol+frX33nxpbS1m9KFVOIfzNF71sVY808rXOMdafeyR8UwrP4tDFb+Cam5ZjFX5o3Go4tbXW1vVyuVanKNx7gvZOedpf2j1BYyv8rRPm5ndT/Xe670fq/yjcTWa98ru8vMAM7MV8eGN/0JhZmav7ePX7z/9ld7MzMzMTuzDh/8DkVOHQfOyVwYAAAAASUVORK5CYII=
    ">

  • Unless I'm missing something, it's very simple.

    SELECT *
    FROM #T
    WHERE PrescriptionID < DuplicatePrescriptionID

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen - Monday, January 30, 2017 11:11 AM

    Unless I'm missing something, it's very simple.

    SELECT *
    FROM #T
    WHERE PrescriptionID < DuplicatePrescriptionID

    Drew

    I'm such an idiot................
    Thanx.

  • drew.allen - Monday, January 30, 2017 11:11 AM

    Unless I'm missing something, it's very simple.

    SELECT *
    FROM #T
    WHERE PrescriptionID < DuplicatePrescriptionID

    Drew

    Image result for homer simpson doh
    😎

  • Eirikur Eiriksson - Monday, January 30, 2017 11:26 AM

    drew.allen - Monday, January 30, 2017 11:11 AM

    Unless I'm missing something, it's very simple.

    SELECT *
    FROM #T
    WHERE PrescriptionID < DuplicatePrescriptionID

    Drew

    Image result for homer simpson doh
    😎

    Exactly.

  • NineIron - Monday, January 30, 2017 11:29 AM

    Eirikur Eiriksson - Monday, January 30, 2017 11:26 AM

    drew.allen - Monday, January 30, 2017 11:11 AM

    Unless I'm missing something, it's very simple.

    SELECT *
    FROM #T
    WHERE PrescriptionID < DuplicatePrescriptionID

    Drew

    Image result for homer simpson doh
    😎

    Exactly.

    can you really use < on varchar data and have it act reliably? Or do you have to convert it into a number?

  • bgalway - Tuesday, January 31, 2017 7:13 AM

    NineIron - Monday, January 30, 2017 11:29 AM

    Eirikur Eiriksson - Monday, January 30, 2017 11:26 AM

    drew.allen - Monday, January 30, 2017 11:11 AM

    Unless I'm missing something, it's very simple.

    SELECT *
    FROM #T
    WHERE PrescriptionID < DuplicatePrescriptionID

    Drew

    Image result for homer simpson doh
    😎

    Exactly.

    can you really use < on varchar data and have it act reliably? Or do you have to convert it into a number?

    Seems to work OK.

  • bgalway - Tuesday, January 31, 2017 7:13 AM

    can you really use < on varchar data and have it act reliably? Or do you have to convert it into a number?

    Yes, you can use it reliably.  The point was to be able to pick one of the two records.  It doesn't matter which of the two you pick, as long as you only pick one of them.  It doesn't matter that a different method (converting to numeric) might sometimes pick the other of the two records as long as either method will only pick one of them.  It doesn't matter that changing the collation may change the results, it still only picks one of them.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • bgalway - Tuesday, January 31, 2017 7:13 AM

    can you really use < on varchar data and have it act reliably? Or do you have to convert it into a number?

    This is perfectly reliable provided you are comparing like with like- comparison operators operate on the character codes which means you need to be aware of the collations used as these will affect the sort order.

    If the two operands have collation A you will always get the same result.
    If they both have collation B then they will still produce a consistent result but it may not be the same as that of the first query.
    If both your operands have different collations all bets are off.

  • However ....
    When comparing varchars
    '100' < '99'

  • DesNorton - Wednesday, February 1, 2017 2:45 AM

    However ....
    When comparing varchars
    '100' < '99'

    That's because '1' < '9' in all collations I am aware of - you will also find that '099' < '100'

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply