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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:18:21+00:00 2026-05-20T05:18:21+00:00

When I run the following query against a MSSQL 2000 SELECT DISTINCT(Email), (SELECT TOP

  • 0

When I run the following query against a MSSQL 2000

SELECT 
    DISTINCT(Email),
    (SELECT TOP 1 ActivityID
        FROM Activity aa, ActivityType tt 
        WHERE aa.ActivityTypeId = tt.ActivityTypeId 
            AND aa.ConsumerID = c.ConsumerID
            AND tt.ActivityType = 'Something_OptIn') optin,
    (SELECT TOP 1 ActivityID
        FROM Activity aa, ActivityType tt 
        WHERE aa.ActivityTypeId = tt.ActivityTypeId 
            AND aa.ConsumerID = c.ConsumerID
            AND tt.ActivityType = 'Something_OptOut') optout
FROM
    Activity a,
    Consumer c,
    ActivityType t
WHERE
    c.CountryID = '23'
    AND t.ActivityType = 'Something_Create'
    AND a.ActivityTypeId = t.ActivityTypeId
    AND c.ConsumerID = a.ConsumerID
    AND optin > 1

I get the following error

Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'optin'.

Why does this happen? I can’t see why it would be invalid.

  • 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-20T05:18:22+00:00Added an answer on May 20, 2026 at 5:18 am

    SQL Server does not allow you to refer to aliases by name at the same level. To fix this, repeat the column definition:

    WHERE
        c.CountryID = '23'
        AND t.ActivityType = 'Something_Create'
        AND a.ActivityTypeId = t.ActivityTypeId
        AND c.ConsumerID = a.ConsumerID
        AND (SELECT TOP 1 ActivityID
            FROM Activity aa, ActivityType tt 
            WHERE aa.ActivityTypeId = tt.ActivityTypeId 
                AND aa.ConsumerID = c.ConsumerID
                AND tt.ActivityType = 'Something_OptIn'
            ) > 1
    

    Or use a subquery:

    SELECT  *
    FROM    (
            SELECT 
                DISTINCT(Email),
                (...) optin,
                (...) optout
            FROM
                Activity a,
                Consumer c,
                ActivityType t
            ) as SubqueryAlias
    WHERE
        c.CountryID = '23'
        AND t.ActivityType = 'Something_Create'
        AND a.ActivityTypeId = t.ActivityTypeId
        AND c.ConsumerID = a.ConsumerID
        AND optin > 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I run the following query in SQL Server 2000 Query Analyzer: BULK INSERT
How do I run the following WMI query, both programmatically to and as a
When I try to run the following code (from the REPL) in Clojure: (dotimes
I want to run an update query against a production database and as good
I am doing the following: $type = 'attachment'; $images = $wpdb->get_results($wpdb->prepare(' SELECT p.* FROM
When I run the following, PowerShell hangs waiting for the dialog to close, even
When I run the following code to test copying a directory, I get a
When I run the following test in Gallio's Icarus it passes, but when I
I want to run the following code: ajaxUpdate(10); With a delay of 1 second
I'm trying to run the following commands: replace -x must A2input.txt replace -x a

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.