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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:27:27+00:00 2026-05-17T17:27:27+00:00

Hey all, I’ve been struggling with a select statement for a bit now and

  • 0

Hey all,
I’ve been struggling with a select statement for a bit now and I was hoping to get some help. I currently have a list IPs in a temporary table and I want to calculate how many times this IP acts as a server and how many times it acts as a client. No matter how I manipluate this select statement I always get the same error telling me that ServerCount is an invalid column, no matter what I replace the nulls with. Here is the select statement:

select IPS, sum (ClientCount) as ClientCount, sum(ServerCount) as ServerCount
from (
       select IP as IPS, Count(*) as ClientCount, null
       from table1 join temp_table 
       on table1.client_ip = temp_table.IP
       group by IP
       union all
       select null,IP as IPS, Count(*) as ServerCount
       from table1 join temp_table 
       on table.server_ip = temp_table.IP
       group by IP
       )t
group by IPS, ClientCount, ServerCount

the first half and the second half work independently without the union. Any ideas as to what is causing this error? Also if I use Nulls I get a second error too. Here is the complete error with nulls in place:

Msg 8155, Level 16, State 2, Line 1
No column was specified for column 3 of ‘t’.
Msg 207, Level 16, State 1, Line 13
Invalid column name ‘ServerCount’.
Msg 207, Level 16, State 1, Line 1
Invalid column name ‘ServerCount’.

Thanks.

  • 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-17T17:27:28+00:00Added an answer on May 17, 2026 at 5:27 pm

    When doing a UNION ALL, your columns should be in the same order. The query will ordinarily take the names of the first SELECT statement to be the names of the column.

    Also, in the outer query, I’m not sure why you are grouping by the aggregate columns; I don’t think that will work. I think what you want is probably better served by something like:

    SELECT tt.IP,
           (SELECT COUNT(*) FROM table1 t1 WHERE tt.IP = t1.client_ip) AS ClientCount,
           (SELECT COUNT(*) FROM table1 t2 WHERE tt.IP = t2.server_ip) AS ServerCount
    FROM   temp_table tt
    ORDER BY tt.IP
    

    Much simpler to look at, at least.

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

Sidebar

Related Questions

Hey all, I have something of an interesting requirement for my project. I need
Hey all - I have an app where I'm authenticating the user. They pass
Hey all. I have a server written in java using the ServerSocket and Socket
Hey all, I'm currently trying to write a compile-time string encryption (using the words
hey all.i'm newbie at this problem.i have this data in table result: item range_code
Hey all! Ive got timezone troubles. I have a time stamp of 2010-07-26 23:35:03
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey all. We're sending quite a few emails (around 23k) using IIS6 SMTP service
Hey all, I'm pulling my hair out on this one. I've checked all my

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.