I have the following tables:CREATE TABLE #member(member_id int) INSERT INTO #member(member_id)SELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3 UNION ALLSELECT 4 UNION ALLSELECT 5 CREATE TABLE #Allergies(member_id int) INSERT INTO #Allergies (member_id)SELECT 1 UNION ALLSELECT 2 CREATE TABLE #Diagnosis(member_id int) INSERT INTO #Diagnosis (member_id)SELECT 2 UNION ALLSELECT 3 CREATE TABLE #rx (member_id int)INSERT INTO #rx (member_id)SELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 5
I want this query:select member_id, (select count(*) from #Allergies a where m.member_id = a.member_id) as Allergies, (select count(*) from #Diagnosis d where m.member_id = d.member_id) as Diagnosis, (select count(*) from #rx r where m.member_id = r.member_id) as RX from #member m
To return only those rows where at least one of the counts is greater than 0Adding this where clause:select member_id, (select count(*) from #Allergies a where m.member_id = a.member_id) as Allergies, (select count(*) from #Diagnosis d where m.member_id = d.member_id) as Diagnosis, (select count(*) from #rx r where m.member_id = r.member_id) as RX from #member mwhere (allergies > 0) or (diagnosis > 0) or (rx > 0)
Gives me these errors:Server: Msg 207, Level 16, State 3, Line 1Invalid column name 'allergies'.Server: Msg 207, Level 16, State 1, Line 1Invalid column name 'diagnosis'.Server: Msg 207, Level 16, State 1, Line 1Invalid column name 'rx'.
Expected results:member_id Allergies Diagnosis RX1 1 0 12 1 1 13 0 1 05 0 0 1
Thanks,Laurie