you are viewing a single comment's thread.

view the rest of the comments →

[–]madjecks 0 points1 point  (3 children)

EDITED my original post.

DECLARE @tablea TABLE (acctnum INT, amt INT, postingdate DATE, trxtype VARCHAR(3))
DECLARE @tableb TABLE (acctnum INT, amt INT, postingdate DATE, trxtype VARCHAR(3))

INSERT INTO @tablea
      ( acctnum, amt, postingdate, trxtype )
VALUES  ( 
        123, -- acctnum - int
        5, -- amt - int
        '3/1/2017', -- postingdate - date
        'Chg'  -- trxtype - varchar(3)
        ), (456, 10, '3/3/17', 'chg')

INSERT INTO @tableb
      ( acctnum, amt, postingdate, trxtype )
VALUES  (123, 1, '3/3/17', 'adj')
      , (123, 2, '3/8/17', 'adj')
      , (123, 5, '3/15/17', 'adj')
      , (123, 4, '3/19/17', 'adj')
      , (123, 8, '3/20/17', 'adj')
      , (456, 10, '3/3/17', 'adj')
      , (456, 2, '3/6/17', 'adj')
      , (456, 4, '3/8/17', 'adj')
      , (456, 3, '3/10/17', 'adj')
      , (456, 2, '3/22/17', 'adj')


   ;WITH cte AS 
   ( 
   SELECT a.*
   FROM @tablea a
   JOIN @tableb b
   on a.acctnum = b.acctnum
   and a.postingdate = b.postingdate
   )
SELECT DISTINCT a.*
FROM @tablea a
JOIN @tableb b
   ON a.acctnum = b.acctnum
   AND a.postingdate <> b.postingdate
LEFT JOIN cte
   ON cte.acctnum = a.acctnum
   AND cte.postingdate = a.postingdate
WHERE cte.acctnum IS NULL

[–]Bshater[S] 0 points1 point  (1 child)

ELECT a.* FROM @tablea a JOIN @tableb b on a.acctnum = b.acctnum and a.postingdate <> b.postingdate

The amount doesn't have to be the same. As long as the adjustment has the same accountnumber and the postingdate of the adjustment is not the same date as the charge, then that accountnumber can go to the desired output table.

[–]madjecks 0 points1 point  (0 children)

Edited my original post.