简体   繁体   English

sql嵌套选择无效列

[英]sql nested select invalid column

Hey all, I've been struggling with a select statement for a bit now and I was hoping to get some help. 大家好,我一直在为select语句苦苦挣扎,希望能得到一些帮助。 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. 我目前在临时表中有一个列表IP,我想计算该IP充当服务器的次数以及充当客户端的次数。 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. 无论我如何操纵此select语句,无论我用什么替换空值,我总是会收到相同的错误,告诉我ServerCount是无效的列。 Here is the select statement: 这是select语句:

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. 另外,如果我使用Nulls,也会出现第二个错误。 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'. 消息8155,级别16,状态2,第1行没有为't'的第3列指定任何列。 Msg 207, Level 16, State 1, Line 13 Invalid column name 'ServerCount'. 消息207,级别16,状态1,第13行无效的列名称'ServerCount'。 Msg 207, Level 16, State 1, Line 1 Invalid column name 'ServerCount'. 消息207,级别16,状态1,行1无效的列名称'ServerCount'。

Thanks. 谢谢。

You need to define servercount in the first query of the union. 您需要在联合的第一个查询中定义servercount。 Aslo null probably doesnt make sense....I would use zero instead. Aslo null可能没有任何意义。...我改用零。

select IPS, sum (ClientCount) as ClientCount, sum(ServerCount) as ServerCount
from (
       select IP as IPS, Count(*) as ClientCount, 0 as serverCount
       from table1 join temp_table 
       on table1.client_ip = temp_table.IP
       group by IP
       union all
       select IP as IPS,0 as ClientCount, 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 SELECT in the subquery needs to define all the column names so you need to change: 子查询中的第一个SELECT需要定义所有列名称,因此您需要更改:

select IP as IPS, Count(*) as ClientCount, null

to

select IP as IPS, Count(*) as ClientCount, null AS ServerCount

Also, I'd change the 2nd SELECT in the subquery to give the columns in the same order: 另外,我将在子查询中更改第二个SELECT,以相同的顺序给出列:

select IP as IPS, null AS ClientCount, Count(*) as ServerCount

Your query is invalid: 您的查询无效:

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

    union all

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

When you have a union, they get the column alias from the first query. 当您拥有并集时,它们将从第一个查询中获取列别名。 Your first query needs to specify the ServerCount column name 您的第一个查询需要指定ServerCount列名称

When doing a UNION ALL, your columns should be in the same order. 当执行UNION ALL时,您的列应该以相同的顺序。 The query will ordinarily take the names of the first SELECT statement to be the names of the column. 该查询通常将第一个SELECT语句的名称用作列的名称。

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. 至少看起来要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM