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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:13:56+00:00 2026-06-13T01:13:56+00:00

I have a CTE which pares down a set of people based on their

  • 0

I have a CTE which pares down a set of people based on their ID numbers. I don’t have a good way to test this at the moment. I haven’t used a CTE in conjunction with a delete statement before, I think this is right but I’d like to proceed after I’m sure.

I did test this and I got the following error:

(0 row(s) affected)
Msg 208, Level 16, State 1, Line 35
Invalid object name ‘x’.

What am I doing wrong here?

--this CTE pares down the number of people that I need for my final result set
;with x as (select distinct patid from
(
select distinct patid
    from clm_extract
    where (diag1 like '952%' or diag1 like '806%') and drg =444

union
select distinct patid
    from clm_extract
    where (diag2  like '952%' or diag2 like '806%') and drg =444

union
select distinct patid
    from clm_extract
    where (diag3  like '952%' or diag3  like '806%') and drg =444
union
select distinct patid
    from clm_extract
    where (diag4  like '952%' or diag4  like '806%') and drg =444
union
select distinct patid
    from clm_extract
    where (diag5  like '952%' or diag5  like '806%') and drg =444
) x

)
--this is a query to show me the list of people that I need to delete from this table because they do not match the criteria
select distinct x.patid
    from x
    inner join clm_extract as c on c.patid = x.patid
    where x.patid !=1755614657 (1 person did match)

--this is my attempt at using a CTE with said query to remove the IDs that I don't need from my table

delete from clm_extract
where patid in(select distinct x.patid
    from x
    inner join clm_extract as c on c.patid = x.patid
    where x.patid !=1755614657)
  • 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-13T01:13:57+00:00Added an answer on June 13, 2026 at 1:13 am

    I think your CTE is wrong – you have a CTE called x and inside that CTE you have a subselect also aliassed to be x – this is causing confusion…

    Why not just have:

    ;with x as 
    (
        select distinct patid
        from clm_extract
        where (diag1 like '952%' or diag1 like '806%') and drg =444
    
        union
    
        select distinct patid
        from clm_extract
        where (diag2  like '952%' or diag2 like '806%') and drg =444
    
        ......     
    )
    select 
        distinct x.patid
    from x
    inner join clm_extract as c on c.patid = x.patid
    where x.patid !=1755614657 (1 person did match)
    

    I don’t see any need nor any benefit in having that extra subquery inside your CTE, really ….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CTE based query into which I pass about 2600 4-tuple latitude/longitude
I have a CTE based query, to which I pass about 2600 4-tuple latitude/longitude
I have a CTE-based query in which I retrieve hourly intervals between two given
I have a CTE-based query in which I retrieve hourly intervals between two given
have written this little class, which generates a UUID every time an object of
I have a table which maintain version of record with their parent id Eg
So I have now a grouping of values by their currencies and values, which
I have a SQL Server 2008 CTE which is responsible for returning the Top
I have a CTE Recursive query which returns the output as expected, however It
I have a Standard table, which sotres parent, child category relationship...like this. id, parent,

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.