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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:16:49+00:00 2026-06-09T20:16:49+00:00

How can I implement something like the following: declare @myInt int set @myInt=(select count(*)

  • 0

How can I implement something like the following:

declare @myInt int
set @myInt=(select count(*) from x)
;with x as

(
select row_number() over(partition by c.patientid order by c.admissiondate) as rn
    ,c.patientid,c.admissiondate
    ,max(c.claimsfromdate)  as maxHemiDate
    ,min(c.claimsfromdate) minHemiDate  
    ,(
        select max(c2.claimsfromdate)
            from claims as c2
            where c2.patientid=c.patientid
            group by c2.patientid
     ) as maxClaimsDate

         ,p.drgCode
         ,datediff(dd,min(c.claimsfromdate),max(c.claimsfromdate)) /7 as weeksWithHemi
         from claims as c inner join icdclaims as ci on ci.id=c.id
         inner join tblicd as t on t.icd_id=ci.icd_id
         inner join patient as p on p.patientid=c.patientid
         and p.admissiondate = c.admissiondate
         and p.dischargedate = c.dischargedate
         where t.icdText like '%X%'  and p.statecode='21'
         group by c.patientid, c.admissiondate, p.drgCode
)

select p.patientid, count(*)
    from patient as p
    left join x on x.patientid=p.patientid
    where x.patientid is null
    group by p.patientid

The error thrown when this is executed is

invalid object name x

I kinda figured that this would happen since the variable declaration is outside of the CTE. If I move the declaration inside of the parentheses of WITH I get another error.

How can I assign a variable like this inside a CTE? Or can you not use a variable that draws data from the CTE at all?

  • 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-09T20:16:50+00:00Added an answer on June 9, 2026 at 8:16 pm

    If you are using SQL Server 2005 or later, you can put the total on each row:

    select t.*, cnt, count(*) over () as NumRows
    from (select p.patientid, count(*) as cnt
          from patient as p left join
               x
               on x.patientid=p.patientid
          where x.patientid is null
          group by p.patientid
        ) t
    

    In case you are using the total to calculate percentages, or something like that, it might be convenient to have the value on each row.

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

Sidebar

Related Questions

I am wondering how I can implement something like the following: test(){ cat>file<<'EOF' abc
What Swing class can I use to implement something like this? Add To List
In Java I can do something like the following: TreeMap<Double, String> myMap = new
I have a program doing something like the following: class SomeObject{} { void function(int
I'd like to know if anyone knows how I can implement something like the
I have a class Something that implements ISomething . How can I convert/cast from
Can someone please tell me how I can implement the following line of pseudo-code.
I have a lot of code like the following, where I explicitly implement some
I want to write something like the following: internal class InternalData { } public
I have a class that is something like the following: public class Foo {

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.