Re: Table compare

From: Dan Guzman (danguzman_at_nospam-earthlink.net)
Date: 04/21/04


Date: Tue, 20 Apr 2004 22:24:44 -0500


> If I want to limit the choices to a smaller set, say a date range for
> instance, I must put the same range in both the main and
subquery....right?

If you want to filter on outer (Table1) criteria, you can do something like
the example below:

SELECT *
FROM Table1 AS t1
WHERE
    t1.SomeDate BETWEEN '20040401' AND '20040420' AND
    NOT EXISTS
    (
    SELECT *
    FROM Table2 AS t2
    WHERE t2.ID = t1.ID
    )

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"Steve C" <stevec@nospam.com> wrote in message
news:%23hwE31uJEHA.3412@TK2MSFTNGP09.phx.gbl...
> If I want to limit the choices to a smaller set, say a date range for
> instance, I must put the same range in both the main and
subquery....right?
>
> "Dan Guzman" <danguzman@nospam-earthlink.net> wrote in message
> news:uLaIrYRHEHA.700@TK2MSFTNGP09.phx.gbl...
> > You can use NOT EXISTS, NOT IN or LEFT JOIN.  Personally, I prefer NOT
> > EXISTS:
> >
> > SELECT *
> > From Table1 AS t1
> > WHERE NOT EXISTS
> >     (
> >     SELECT *
> >     FROM Table2 AS t2
> >     WHERE t2.ID = t1.ID
> >     )
> >
> > -- 
> > Hope this helps.
> >
> > Dan Guzman
> > SQL Server MVP
> >
> > "Dave" <sysadmin@enaples.com> wrote in message
> > news:uW83XPRHEHA.3952@TK2MSFTNGP10.phx.gbl...
> > > Looking to find all records in one table that are not in another
table.
> > What
> > > is the most efficient way?
> > >
> > >
> >
> >
>
>


Relevant Pages

  • Re: Status Column in sysobjects table...SQL2000
    ... Please post a sample proc that demonstrates the ... >> Dan Guzman ... >> SQL Server MVP ... even though there are no compilation errors. ...
    (microsoft.public.sqlserver.programming)
  • Re: Status Column in sysobjects table...SQL2000
    ... The QUOTED_IDENTIFER setting may or may not prevent the proc from being ... > Quoted_Identifiers and/or ANSI_NULLS it would not even compile. ... >> Dan Guzman ... >> SQL Server MVP ...
    (microsoft.public.sqlserver.programming)
  • Re: Database recovery with data file only
    ... Basically using that command breaks your business logic as there's no ... "Dan Guzman" wrote in message ... >> database Backup as well as ... >>> SQL Server MVP ...
    (microsoft.public.sqlserver.server)
  • Re: how to scramble data in a table
    ... >> accomplish this with set-based processing using CASE expressions. ... >> Dan Guzman ... >> SQL Server MVP ... >>> Does anyone have a neat way to scramble data in one table? ...
    (microsoft.public.sqlserver.programming)
  • Re: Read log files
    ... for ad-hoc analysis. ... >> Dan Guzman ... >> SQL Server MVP ... can't we read the log file without this. ...
    (microsoft.public.sqlserver.server)