MS SQLServer 7.0<BR><BR>TABLE1:<BR><BR>document_id, confidential_cd<BR>1, "show"<BR>2, "hide"<BR>7, "show"<BR>9, "show"<BR>10, "hide"<BR>12, "show"<BR>18, "show"<BR>20, "show"<BR><BR>***************************<BR><BR>T ABLE2:<BR><BR>document_id, category_id<BR>1, A<BR>2, A<BR>7, A<BR>9, B<BR>10, B<BR>12, C<BR>18, D<BR>20, E<BR><BR>*********************<BR><BR>TABLE3:<BR>< BR>category_id, Department<BR>A, 1<BR>B, 1<BR>C, 1<BR>D, 1<BR>E, 7<BR>F, 7<BR><BR>***************************<BR><BR>TABLE4 :<BR><BR>document_id<BR>2<BR><BR><BR>I need to write a query to get number of documents in each Department<BR><BR>Department, document_count<BR>1, 6<BR>7 1<BR><BR>The only catch in the logic is we should not consider documents with confidential_cd of "hide" unless it appears in TABLE4.<BR>I tried with using temp tables. But it is very slow as we have thousands of records.<BR>I really appreciate any good logic.<BR><BR>Thanks