Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6027777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:38:32+00:00 2026-05-23T04:38:32+00:00

I have two Tables in two different databases Database1 – Table1 Database2 – Table2

  • 0

I have two Tables in two different databases

Database1 - Table1
Database2 - Table2

Table1 Columns: NimID,IDDate,Station
Table2 Columns: XilID,XilDate

Table1                                                   Table2
NimID   IDDate           Station                   XilID       XilDate      
234     2011-04-21       HYD                     234        2011-04-21
237     2011-04-21       CHN                     208        2011-04-21
208     2011-04-21       HYD                     209        2011-04-15
209     2011-04-15       DEL                      218        2011-05-28
                                                             212       2011-03-11

I want to find out how many IDs in Table1 are not in Table2 where IDDate=XilDate=’2011-04-21′ group by Table1.Station .
I made the query below

select x.Station as Station,
       count(distinct x.NimID) as Difference
from (
      select a.NimID,
             a.IDDate,
             a.Station
      from database1.dbo.table1 a
      where left(cast(a.Date as date),11)='2011-04-21'
 ) as X, (
      select b.XilID,
             b.XILDate 
      from database2.dbo.Table2 b
      where b.XilDate='2011-04-21'
 ) as Y
 where x.NimID not in (y.XilID)
 group by x.Station

But this query runs forever..
Please remember the tables are from different databases located on same server and Table1 contains 10,000,000 records and Table2 contains around 13,000,000 records
Please correct my query if wrong or suggest me the faster way

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:38:32+00:00Added an answer on May 23, 2026 at 4:38 am
    DECLARE @date datetime;
    SET @date = '20110421';
    
    SELECT
      Station,
      Diff = COUNT(*)
    FROM (
      SELECT
        a.NimID,
        a.IDDate,
        a.Station
      FROM database1.dbo.table1 a
        LEFT JOIN database2.dbo.table2 b ON a.NimID = b.XilID AND b.XilDate = @date
      WHERE b.XilID IS NULL
        AND a.IDDate >= @date
        AND a.IDDate < DATEADD(day, 1, @date)
    ) s
    GROUP BY Station
    

    UPDATE

    Actually, the above solution could be rewritten without subselects. The subselect is the result of trying some idea, which I’ve eventually discarded, but the subselect has remained for some unknown reason. Here’s an identical solution with no subselects:

    DECLARE @date datetime;
    SET @date = '20110421';
    
    SELECT
      a.Station,
      Diff = COUNT(*)
    FROM database1.dbo.table1 a
      LEFT JOIN database2.dbo.table2 b ON a.NimID = b.XilID AND b.XilDate = @date
    WHERE b.XilID IS NULL
      AND a.IDDate >= @date
      AND a.IDDate < DATEADD(day, 1, @date)
    GROUP BY a.Station
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in two different databases. In table1 (in database1) there is
I have two identical tables in two different databases with the same data but
I have a need to sync auto_increment fields between two tables in different databases
I have two tables from two different databases. For example: Table: Articles1 - Database:
I have two tables in different databases on the same database server. Both the
I have two different databases where I need to connect and update the tables
I have two different databases that contain two tables with the exact same design.
Using SQL Server 2008 (R2). I have two tables in different databases (with same
I have two different databases that share some of the same tables, and differ
I have two databases with different structure. Table 1: ch_code ch_def ch_weight Table 2:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.