Hi All, Lets say I have two sets of table which is tb1 and tb2. How can write the SQL query to get entire rows from @tb1 if I comparing the keys(CHDRNO and RSKNO) which the @tb2 and the count is more than 1 matching keys. The keys is CHDRNO and RSKNO. Some of the key have duplicate data.
Please refer the expected output below from @tb1 by applying above condition. I get two rows of (CHDRNO=Z0012725 and RSKNO=1) from @tb1 because this combination field contain more than 1 rows in @tb1 when compared to @tb2. Any combination key count have mismatch with @tb2 (the combination field count in @tb1 more than in @tb2) , the entire record should be query out from @tb1.
DDL and sample data population:
-- DDL and sample data population, start
DECLARE @tb1 TABLE (CHDRNO varchar(12), RSKNO int, ENDNLNE varchar(60),DATIME datetime2(7) )
INSERT INTO @tb1 (CHDRNO, RSKNO, ENDNLNE,DATIME) VALUES
('Z0012725','1','JUST','2020-08-03 14:45:02.7549260'),
('Z0012725','1','ABC','2020-08-04 14:45:02.7549260'),
('Z0012725','2','DEF','2020-08-03 14:45:02.7549260'),
('Z0012725','2','RUN','2020-08-03 14:45:02.7549260'),
('Z0012725','3','AND','2020-08-03 14:45:02.7549260')
-- DDL and sample data population, end
-- DDL and sample data population, start
DECLARE @tb2 TABLE (CHDRNO varchar(12), RSKNO int, ENDNLNE varchar(60),DATIME datetime2(7) )
INSERT INTO @tb2 (CHDRNO, RSKNO, ENDNLNE,DATIME) VALUES
('Z0012725','1','JUST','2020-08-03 14:45:02.7549260'),
('Z0012725','2','DEF','2020-08-03 14:45:02.7549260'),
('Z0012725','2','RUN','2020-08-03 14:45:02.7549260'),
('Z0012725','3','AND','2020-08-03 14:45:02.7549260')
-- DDL and sample data population, end
Expected Output: