I was told it's not efficient to use the natdocfile twice in the two queries I've joined. How can I set up the queries to just use one natdocfile? I want to right join on the natdocfile to get all records in there to appear even if they are 0 for the value.Here's my two queriesSELECT a.doc, pendinfo1, pendinfo2 FROM (select n.doc,Isnull(COUNT(t.cossn), 0)AS PendinFO1 FROM t16pendall t right join natdocfile n on n.doc=t.fo and (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y'))and t.cossn not in(select cossn from t16pendmvt)GROUP BY n.doc)ajoin(SELECT n.doc, Isnull(COUNT(m.cossn), 0) AS pendinfo2 FROM t16pendmvt m JOIN t16pendall t ON m.cossn = t.cossn right join natdocfile n on n.doc=t.fo and ( mvt_typ = 'R' ) AND not (mvt_loc LIKE 'R%' or mvt_loc LIKE 'S%' OR mvt_loc LIKE 'V%' ) GROUP BY n.doc)bon a.doc = b.docHere's the table info:CREATE TABLE [dbo].[T16pendall]( [COSSN] [char](6) NOT NULL, [FLG_CDT] [datetime] NULL, [AGED_ALIEN_RSW] [char](1) NOT NULL, [MFT_POSN1_CD] [char](1) NOT NULL, [FO] [varchar](4) NOT NULL) ON [PRIMARY]insert into T16pendallselect '268763', '10/6/2010', ' ', 'D', '389' union allselect '337629', '10/21/2010', ' ', 'D', 'A00' union allselect '240312', '10/28/2010', ' ', 'D', '330' union allselect '406339', '10/4/2010', ' ', 'D', '442' union allselect '279700', '11/1/2010', ' ', 'D', '387' union allselect '680031', '10/29/2010', ' ', 'D', 'A00' union allselect '298582', '10/26/2010', ' ', 'D', '387' union allselect '074506', '9/23/2010', ' ', 'D', '329' union allselect '610128', '10/6/2010', ' ', 'A', '893' union allselect '427337', '6/30/2010', ' ', 'D', 'B14' union allselect '553084', '10/15/2010', ' ', 'D', '913' union allselect '587962', '8/30/2010', ' ', 'D', '100' union allselect '746855', '9/10/2010', ' ', 'D', 'A38' union allselect '429194', '9/13/2010', ' ', 'D', 'A00' union allselect '424431', '9/14/2010', ' ', 'D', 'A00' Second Table and Insert statementCREATE TABLE [dbo].[T16pendmvt]( [COSSN] [char](6) NOT NULL, [MVT_TYP] [char](1) NOT NULL, [MVT_LOC] [char](3) NOT NULL, [MVT_DEST] [varchar](3) NOT NULL, [MVT_CDT] [datetime] NULL) ON [PRIMARY]insert into T16pendmvtselect '074506', 'T', '329', 'S36', '9/23/2009' union allselect '610128', 'R', 'S15', ' ', '10/6/2010' union allselect '427337', 'R', 'S27', ' ', '7/1/2010' union allselect '553084', 'R', 'V64', ' ', '10/26/2010' union allselect '999962', 'T', 'SS2', 'R42', '2/3/2003' union allselect '452262', 'R', 'VS2', ' ', '1/22/2004' union allselect '458962', 'R', 'V36', ' ', '5/5/2004' union allselect '458962', 'R', 'V25', '858', '6/8/2004' union allselect '458962', 'T', 'S72', 'S24', '7/19/2004' union allselect '458962', 'R', '004', ' ', '4/8/2010'Last table and insert statementCREATE TABLE [dbo].[natdocfile]([doc] [varchar](3) NOT NULL) ON [PRIMARY]insert into natdocfileselect '001' union allselect 'A00' union allselect '389' union allselect 'A38' union allselect 'B14' union allselect '913' union allselect '009' union allselect '00K' union allselect '00N' union allselect '00U'
Here are the results:Doc pendinfo1 pendinfo2 001 0 0009 0 000K 0 000N 0 000U 0 0389 1 0913 0 0A00 4 0A38 1 0B14 0 0
As you can see all of the doc fields are listed from the Natdocfile which is what I want.Is there another way to just list the one natdocfile at the end of the query or the top of it, I can't figure it out. Thanks!