clear markdown compare help best fiddles feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3601588 fiddles created (48003 in the last week).

Declare @Main Table (sn varchar(100), amt float, sd date) Declare @Sub Table (sn varchar(100), samt float, ssd date) Insert Into @Main (sn, amt, sd) Values ('G', 23.00, '20170601'), ('G', 21.00, '20170602'), ('G', 33.00, '20170608') Insert Into @Sub (sn, samt, ssd) VALUES ('G', 11.00, '20170502'), ('G', 12.00, '20170601'), ('G', -1.00, '20170601') ;WITH g1 AS ( SELECT sn, SUM(samt) AS samt FROM @Sub WHERE ssd >= '20170601' AND ssd <= '20170605' GROUP BY sn ) , g2 AS ( SELECT sn, SUM(amt) as amt FROM @Main WHERE sd >= '20170601' AND sd <= '20170605' GROUP BY sn ) SELECT g1.sn AS SalesPerson, g1.samt AS SalesAmt, (g1.samt - g2.amt) AS DiffAmp FROM g1 JOIN g2 ON g1.sn = g2.sn;
SalesPerson SalesAmt DiffAmp
G 11 -33
 hidden batch(es)