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

  • SEARCH
  • Home
  • 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 4240592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:11:08+00:00 2026-05-21T03:11:08+00:00

I have a query, or batch of queries, that I just can’t get to

  • 0

I have a query, or batch of queries, that I just can’t get to play nice 🙂

SELECT 
      C.ID AS cust_id,
      C.State AS cust_state,
      C.PrevState AS cust_previous_state,
      C.ProjectID AS cust_imported_on_project_id,
      C.CampaignID AS cust_imported_on_campaign_id,
      C.Priority AS cust_priority,
      C.Name AS cust_firstname,
      C.Name2 AS cust_lastname,
      C.AllocatedUser AS cust_allocated_user,
      C.ED1 AS cust_social_security_number,
      C.ED2 AS cust_customer_number,
      C.ED3 AS cust_type,
      C.ED4 AS cust_initial_fact_1,
      C.ED5 AS cust_initial_fact_2,
      C.ED6 AS cust_initial_fact_3,
      C.ED7 AS cust_initial_fact_4,
      C.ED8 AS cust_initial_fact_5,
      C.ED9 AS cust_extra_1,
      C.ED10 AS cust_extra_2,
      CED2.ED11 AS cust_extra_3,
      CED2.ED12 AS cust_extra_4,
      CED2.ED13 AS cust_extra_5,
      A.Serial AS address_serial,
      A.PostAddress AS address_postal_address,
      A.PostCode AS address_postal_code,
      A.PostOffice AS address_city,
      A.PhoneNr AS address_phonenumber,
      A.FaxNr AS address_faxnumber,
      A.EMail AS address_email,
      A.Notes AS address_notes,
      A.ED1 AS address_secondary_phonenumber,
      A.ED2 AS address_origin_file,
      A.State AS address_state
FROM TCustomers C WITH (NOLOCK)
    LEFT JOIN TCustED2 CED2 WITH (NOLOCK) ON C.ID = CED2.CustomerID 
    LEFT JOIN TAddresses A WITH (NOLOCK) ON C.AddressNr = A.Serial AND C.ID = A.CustomerID
WHERE C.CampaignID IN(SELECT items FROM dbo.Split('196,195,210,206,205,207,204,200,209,213,197,198,214', ',')) AND C.InsDate > '2011-04-03 00:00:00'

This query in itself is very snappy and executes on a million+ rows in a few seconds.
The problem is when I want to use information from the order table to filter my result.

I have tried to join on the Orders table

SELECT 
      C.ID AS cust_id,
      C.State AS cust_state,
      C.PrevState AS cust_previous_state,
      C.ProjectID AS cust_imported_on_project_id,
      C.CampaignID AS cust_imported_on_campaign_id,
      C.Priority AS cust_priority,
      C.Name AS cust_firstname,
      C.Name2 AS cust_lastname,
      C.AllocatedUser AS cust_allocated_user,
      C.ED1 AS cust_social_security_number,
      C.ED2 AS cust_customer_number,
      C.ED3 AS cust_type,
      C.ED4 AS cust_initial_fact_1,
      C.ED5 AS cust_initial_fact_2,
      C.ED6 AS cust_initial_fact_3,
      C.ED7 AS cust_initial_fact_4,
      C.ED8 AS cust_initial_fact_5,
      C.ED9 AS cust_extra_1,
      C.ED10 AS cust_extra_2,
      CED2.ED11 AS cust_extra_3,
      CED2.ED12 AS cust_extra_4,
      CED2.ED13 AS cust_extra_5,
      A.Serial AS address_serial,
      A.PostAddress AS address_postal_address,
      A.PostCode AS address_postal_code,
      A.PostOffice AS address_city,
      A.PhoneNr AS address_phonenumber,
      A.FaxNr AS address_faxnumber,
      A.EMail AS address_email,
      A.Notes AS address_notes,
      A.ED1 AS address_secondary_phonenumber,
      A.ED2 AS address_origin_file,
      A.State AS address_state
FROM TCustomers C WITH (NOLOCK)
    LEFT JOIN TCustED2 CED2 WITH (NOLOCK) ON C.ID = CED2.CustomerID 
    LEFT JOIN TAddresses A WITH (NOLOCK) ON C.AddressNr = A.Serial AND C.ID = A.CustomerID
    LEFT JOIN TOrders O WITH (NOLOCK) ON C.ID = O.CustomerID
WHERE C.CampaignID IN(SELECT items FROM dbo.Split('196,195,210,206,205,207,204,200,209,213,197,198,214', ',')) AND 
(C.InsDate > '2011-04-03 00:00:00' OR O.CustomerID NOT NULL OR O.[Date] > '2011-04-03 00:00:00' OR O.Exported IS NULL);

This results in an execution time of several minutes.
If I just run the interesting part from TOrders it just takes a few seconds to execute.

SELECT CustomerID 
FROM TOrders 
WHERE O.CustomerID NOT NULL 
   OR O.[Date] > '2011-04-03 00:00:00' 
   OR O.Exported IS NULL;

So the problem is when combining the two. I tried running the TOrders query and pasting the resulting CustomerID’s into the main query directly and that was snappy and took around 5-10 seconds. I tried prefetching the interesting data from TOrders and putting it into a temporary table but that didn’t make it any faster.

CREATE TABLE #ORDERCUSTOMERS (
  CustomerID int
);
CREATE UNIQUE CLUSTERED INDEX IX_1 on #ORDERCUSTOMERS (CustomerID);

INSERT #ORDERCUSTOMERS SELECT DISTINCT O.CustomerID FROM LPD1_8.dbo.TOrders O WHERE O.CampaignID IN(SELECT items FROM dbo.Split('196,195,210,206,205,207,204,200,209,213,197,198,214', ',')) AND (O.[Date] > '2011-04-03 00:00:00' OR O.Exported IS NULL);

SELECT 
          C.ID AS cust_id,
          C.State AS cust_state,
          C.PrevState AS cust_previous_state,
          C.ProjectID AS cust_imported_on_project_id,
          C.CampaignID AS cust_imported_on_campaign_id,
          C.Priority AS cust_priority,
          C.Name AS cust_firstname,
          C.Name2 AS cust_lastname,
          C.AllocatedUser AS cust_allocated_user,
          C.ED1 AS cust_social_security_number,
          C.ED2 AS cust_customer_number,
          C.ED3 AS cust_type,
          C.ED4 AS cust_initial_fact_1,
          C.ED5 AS cust_initial_fact_2,
          C.ED6 AS cust_initial_fact_3,
          C.ED7 AS cust_initial_fact_4,
          C.ED8 AS cust_initial_fact_5,
          C.ED9 AS cust_extra_1,
          C.ED10 AS cust_extra_2,
          CED2.ED11 AS cust_extra_3,
          CED2.ED12 AS cust_extra_4,
          CED2.ED13 AS cust_extra_5,
          A.Serial AS address_serial,
          A.PostAddress AS address_postal_address,
          A.PostCode AS address_postal_code,
          A.PostOffice AS address_city,
          A.PhoneNr AS address_phonenumber,
          A.FaxNr AS address_faxnumber,
          A.EMail AS address_email,
          A.Notes AS address_notes,
          A.ED1 AS address_secondary_phonenumber,
          A.ED2 AS address_origin_file,
          A.State AS address_state
      FROM LPD1_8.dbo.TCustomers C WITH (NOLOCK)
    LEFT JOIN LPD1_8.dbo.TCustED2 CED2 WITH (NOLOCK) ON C.ID = CED2.CustomerID 
    LEFT JOIN LPD1_8.dbo.TAddresses A WITH (NOLOCK) ON C.AddressNr = A.Serial AND C.ID = A.CustomerID
    LEFT JOIN #ORDERCUSTOMERS O ON C.ID = O.CustomerID
      WHERE C.CampaignID IN(SELECT items FROM dbo.Split('196,195,210,206,205,207,204,200,209,213,197,198,214', ',')) AND 
    (C.InsDate > '2011-04-03 00:00:00' OR O.CustomerID NOT NULL);

DROP TABLE #ORDERCUSTOMERS;

So do you guys have any idea? This will reside in a stored procedure so I want to avoid dynamic SQL if possible. Otherwise that would probably be one way, to fetch the data from TOrders and inject the returned CustomerID’s into the IN clause of the main query.
But prehaps there is a similar way to “inject” an array of id’s into an IN clause or something?

  • 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-21T03:11:09+00:00Added an answer on May 21, 2026 at 3:11 am

    you are using the order table in your where clause. this means that the query planner can’t start removing lines from the customer table early enough. have you tried joining in the order table earlier?

    SELECT 
          C.ID AS cust_id,
          C.State AS cust_state,
          C.PrevState AS cust_previous_state,
          C.ProjectID AS cust_imported_on_project_id,
          C.CampaignID AS cust_imported_on_campaign_id,
          C.Priority AS cust_priority,
          C.Name AS cust_firstname,
          C.Name2 AS cust_lastname,
          C.AllocatedUser AS cust_allocated_user,
          C.ED1 AS cust_social_security_number,
          C.ED2 AS cust_customer_number,
          C.ED3 AS cust_type,
          C.ED4 AS cust_initial_fact_1,
          C.ED5 AS cust_initial_fact_2,
          C.ED6 AS cust_initial_fact_3,
          C.ED7 AS cust_initial_fact_4,
          C.ED8 AS cust_initial_fact_5,
          C.ED9 AS cust_extra_1,
          C.ED10 AS cust_extra_2,
          CED2.ED11 AS cust_extra_3,
          CED2.ED12 AS cust_extra_4,
          CED2.ED13 AS cust_extra_5,
          A.Serial AS address_serial,
          A.PostAddress AS address_postal_address,
          A.PostCode AS address_postal_code,
          A.PostOffice AS address_city,
          A.PhoneNr AS address_phonenumber,
          A.FaxNr AS address_faxnumber,
          A.EMail AS address_email,
          A.Notes AS address_notes,
          A.ED1 AS address_secondary_phonenumber,
          A.ED2 AS address_origin_file,
          A.State AS address_state
    FROM TCustomers C WITH (NOLOCK)
        LEFT JOIN TOrders O WITH (NOLOCK) ON C.ID = O.CustomerID
        LEFT JOIN TCustED2 CED2 WITH (NOLOCK) ON C.ID = CED2.CustomerID 
        LEFT JOIN TAddresses A WITH (NOLOCK) ON C.AddressNr = A.Serial AND C.ID = A.CustomerID
    WHERE C.CampaignID IN(SELECT items FROM dbo.Split('196,195,210,206,205,207,204,200,209,213,197,198,214', ',')) AND 
    (C.InsDate > '2011-04-03 00:00:00' OR O.CustomerID NOT NULL OR O.[Date] > '2011-04-03 00:00:00' OR O.Exported IS NULL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just discovered that SQL Server CE doesn't allow you to batch queries.
I have a batch file that runs some SELECT queries using sqlcmd, puts the
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I am using in C# MYsql .I have query that works if I run
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have a query that successfully grabs the unique products from my products table
I have a query like this: select empno,name from emp where job = 'CLERK'
I would like to know how I can use the reg query in batch
Hey i can't seem to be able to solve this, i have this query
Suppose that I have users, tasks and each task belongs to a batch. Each

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.