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

The Archive Base Latest Questions

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

My work is running SQL Server 2008 and I spend a lot of time

  • 0

My work is running SQL Server 2008 and I spend a lot of time querying the database for information as a side piece to my job. If I need information that isn’t at the same aggregate level as my dataset I use an embedded query in the select statement. Usually it’s 2 or 3 slightly different versions of the same number, so they both query the same tables. (See example below)

The question is what is the scoping of the aliases for the subqueries embedded in a select statement. The two options I’ve thought of are:

  1. At the Script level and must be unique to all subqueries and
    tables?
  2. At the Subquery level and can share the same aliases in
    each.

I know for tables aliased in the From statement they must be unique. I thought that the fact that the queries were executing on each row generation that it might be a different situation.

Examples (completely made up, let me know any obvious errors and I’ll correct them):

Script Level – unique aliases for all subqueries and tables:

Select
  p.purchaseid, p.purchasedate, 
  s.storename, c.customerid, 
(select count(p2.purchaseid) 
  from purchases p2 inner join 
       store s2 on p2.storeid = s2.storeid
  where s2.storeid = s.storeid
    and p2.purchasedate = p.purchasedate) as 'Store Daily Total Purchases',
(select count(p3.purchaseid) 
  from purchases p3 inner join 
       store s3 on p3.storeid = s3.storeid
  where p3.customerid = p.customerid
    and p3.purchasedate = p.purchasedate) as 'Customer Daily Total Purchases'
from 
  purchases p inner join   
  customer c on p.customerid = c.customerid
  store s on p.storeid = s.storeid

Query Level – common aliases for subqueries ok:

Select
  p.purchaseid, p.purchasedate, 
  s.storename, c.customerid, 
(select count(p2.purchaseid) 
  from purchases p2 inner join 
       store s2 on p2.storeid = s2.storeid
where s2.storeid = s.storeid
  and p2.purchasedate = p.purchasedate) as 'Store Daily Total Purchases',
(select count(p2.purchaseid) 
  from purchases p2 inner join 
       store s2 on p2.storeid = s2.storeid
where p2.customerid = p.customerid
  and p2.purchasedate = p.purchasedate) as 'Customer Daily Total Purchases'
from 
  purchases p inner join   
  customer c on p.customerid = c.customerid
  store s on p.storeid = s.storeid
  • 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:34:12+00:00Added an answer on June 6, 2026 at 1:34 pm

    The aliasing is at the query level, so your second example is fine.

    The reference to the tables in the from clause makes these correlated subqueries, in case you want to read more about that topic.

    In general, I recommend that you move the queries to the from clause and manage them as joins:

    Select p.purchaseid, p.purchasedate, s.storename, c.customerid,
           pdate.cnt as [Store Daily Total Purchases],
           pcust.cnt as [Customer Daily Total Purchases]
    from purchases p inner join   
         customer c
         on p.customerid = c.customerid join
         store s on p.storeid = s.storeid left outer join
         (select s2.storeid, p2.purchasedate, count(p2.purchaseid) 
          from purchases p2 inner join 
               store s2 on p2.storeid = s2.storeid
          group by s2.storeid, p2.purchasedate
         ) pdate
         on pdate.purchasedate = p.purchasedate and
            pdate.storeid = s.storeid left outer join
         (select s2.customerid, p2.purchasedate, count(p2.purchaseid) 
          from purchases p2 inner join 
               store s2 on p2.storeid = s2.storeid
          group by s2.storeid, p2.purchasedate
         ) pcust
         on pcust.purchasedate = p.purchasedate and
            pcust.customerid= s.customerid
    

    However, in your case, I think there is a simpler solution just using aggregation:

    Select p.purchaseid, p.purchasedate, s.storename, c.customerid,
           count(*) over (partition by s.storeid, p.purchasedate) as  as [Store Daily Total Purchases],
           count(*) over (partition by c.customerid, p.purchasedate) as [Customer Daily Total Purchases]
    from purchases p inner join   
         customer c
         on p.customerid = c.customerid join
         store s on p.storeid = s.storeid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few SSRS reports running on SQL Server 2008 Web Edition SP1
I have an application running in IIS which connects to a SQL Server 2008
Scenario: Production servers are running SQL Server 2008 in the domain myDomain.com Dev/test/stage servers
I have an instance of SQL Server 2008 and SSRS 2008 running on a
I need to set up a throw-away instance of SQL Server 2008 for students
I need to connect to a SQL server 2000 from SQL server 2008. but
I've tryed to create a connection with a Microsoft SQl Server 2008 database through
I am running SQL Server Reporting Services on SQL Server 2008 Standard and trying
I have a SQL Query running on SQL Server 2008 R2 that returns a
I'm using SQL Server 2008 and am trying to change the current database name

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.