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 8145339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:38:04+00:00 2026-06-06T13:38:04+00:00

How do I rewrite this T-SQL code to produce the same results SELECT ACC.Title,

  • 0

How do I rewrite this T-SQL code to produce the same results

   SELECT   ACC.Title,
            ACC.AdvertiserHierarchyId,
            1 AS Counter
    FROM    admanAdvertiserHierarchy_tbl ACC
    JOIN    dbo.admanAdvertiserObjectType_tbl AOT ON AOT.AdvertiserObjectTypeId = ACC.AdvertiserObjectTypeId
              WHERE  (EXISTS
                         (SELECT 1
                          FROM    dbo.admanAdvertiserHierarchy_tbl CAMP
                          JOIN    dbo.admanAdvertiserAdGroup_tbl AG ON CAMP.AdvertiserHierarchyId = AG.AdvertiserHierarchyId
                          JOIN    dbo.admanAdvertiserCreative_tbl AC ON AC.AdvertiserAdGroupId = AG.AdvertiserAdGroupId
                          AND     CAMP.ParentAdvertiserHierarchyId = ACC.AdvertiserHierarchyId
                          WHERE   CAMP.ERROR = 0
                                  AND AC.Dirty & 7 > 0
                                  AND AC.ERROR = 0
                                  AND AG.ERROR = 0 ))

its preventing the optimizer from using indexes efficiently .
trying to achieve the following results

    Title                               AdvertiserHierarchyId       Counter
    trcom65@travelrepublic.co.uk        15908                       1
    paul570@travelrepublic.co.uk        37887                       1
    es88@travelrepublic.co.uk           37383                       1
    it004@travelrepublic.co.uk          27006                       1
    011                                 10526                       1
    013                                 10528                       1
    033                                 12013                       1
    062                                 17380                       1
    076                                 20505                       1

this is a count of the dirty tinyint column

    Dirty   total
    0       36340607
    1       117569
    2       873553
    3       59

that links to a static reason table

    DirtyReasonId   Title
    0               Nothing
    1               Overnight Engine
    2               End To End
    3               Overnight And End To End
    4               Pause Resume
    5               Overnight Engine and Paused
    6               Overnight Engine E2E and Paused
    7               All Three
  • 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-06-06T13:38:06+00:00Added an answer on June 6, 2026 at 1:38 pm

    If you are asking specifically about the use of the BITWISE AND operator, I believe you are correct, and it’s unlikely that SQL Server sees that as sargable, at least, not with an index with Dirty as a leading column.

    You are showing only the lowest two bits in use (maximum value of Dirty is 3), yet you are testing the lowest three bits.

    So, AC.Dirty > 0 would return an equivalent result, given that 3 is largest value of Dirty. But there is a possibility that other (higher-order) bits are set, for example Dirty could be set to 8. So, if the intent is to check ONLY the lowest three bits, then we need to ensure that we test only the three lowest-order bits. This expression would do that, and one of the predicates is sargable:

    ( AC.Dirty > 0 AND AC.Dirty % 8 > 0 )
    

    This basically tests first whether ANY bits in AC.Dirty are set, and then checks if any of the last three bits are set. (We’re using the MODULO division operator to return the remainder of AC.Dirty divided by 8, which will of course return an integer value between 0 and 7. If we get a zero, then we know that none of the lower three bits are set, else we know at least one of the bits is set.

    Just to be clear: the predicate on AC.Dirty > 0 is redundant. It’s included here in case you are wanting to make sure that database can at least consider using an existing index with Dirty as a leading column.


    I will mention that another option to consider would be adding a persisted COMPUTED COLUMN on the expression, and create an index on it. But that seems a bit overkill for what you need here.


    If you are asking specifically about getting an index used on table admanAdvertiserCreative_tbl (AC), then likely your best candidate would be covering index on (AdvertiserAdGroupId, Error, Dirty).


    The SQL rewrite below should return equivalent results, perhaps with better performance (depending on your data distribution, indexes, et al.)

    Basically, replace the EXISTS (correlated subquery) with a JOIN to a subquery. The subquery returns distinct values of CAMP.ParentAdvertiserHierarchyId, which is the column you referenced to correlate the subquery.

    This may or may not make use of any indexes, depending on what indexes are available. (It’s likely have clustered unique indexes on the primary keys, and have non-clustered indexes on the foreign keys, which should help join performance.)

    Untested:

    SELECT  ACC.Title,
            ACC.AdvertiserHierarchyId,
            1 AS Counter
    FROM    admanAdvertiserHierarchy_tbl ACC
    JOIN    dbo.admanAdvertiserObjectType_tbl AOT 
            ON AOT.AdvertiserObjectTypeId = ACC.AdvertiserObjectTypeId
    JOIN   (SELECT    CAMP.ParentAdvertiserHierarchyId
              FROM    dbo.admanAdvertiserHierarchy_tbl CAMP
              JOIN    dbo.admanAdvertiserAdGroup_tbl AG 
                ON    CAMP.AdvertiserHierarchyId = AG.AdvertiserHierarchyId
              JOIN    dbo.admanAdvertiserCreative_tbl AC 
                ON    AC.AdvertiserAdGroupId = AG.AdvertiserAdGroupId
             WHERE    CAMP.ERROR = 0
               AND    ( AC.Dirty > 0 AND AC.Dirty % 8 > 0 )
               AND    AC.ERROR = 0
               AND    AG.ERROR = 0 )
             GROUP BY CAMP.ParentAdvertiserHierarchyId
           ) c
      ON c.ParentAdvertiserHierarchyId = ACC.AdvertiserHierarchyId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this sql code in Drupal: $query = db_query('SELECT wpv.nid, n.title, AVG(vote)
i want to know how i can rewrite this SQL into a single select
How would I rewrite this query to be performant by executing the SQL function
I thought I would rewrite this question (same iteration). The original was how to
How can I rewrite this code to check for all characters including the swedish
How can I rewrite this code to coffeescript: $('.delete_action').click(function(event) { $.get('/delete_action?name=' + event.target.name +
I've this piece of code, it retrieves a node from my node type 'Student'.
How can I rewrite this SQL query to avoid division by zero errors in
I'm about to have to rewrite some rather old code using SQL Server's BULK
Edit How can I rewrite this PHP code for POST[ 'phrase' ] == session[

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.