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

DECLARE @A table (A integer NULL, B integer NULL); DECLARE @B table (A integer NULL, B integer NULL); INSERT @A (A, B) VALUES (1, 1); INSERT @B (A, B) VALUES (2, 2); -- Original SELECT * FROM @A AS A CROSS APPLY (SELECT c = COUNT_BIG(*) FROM @B AS B WHERE B.A = A.A GROUP BY B.A) AS CA; -- Rewrite SELECT A.*, J1.c FROM @A AS A JOIN ( SELECT B.A, c = COUNT_BIG(*) FROM @B AS B GROUP BY B.A ) AS J1 ON J1.A = A.A;
A B c
A B c
 hidden batch(es)