sql查询帮助where子句

t40tm48m  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(416)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

11个月前关门了。
改进这个问题
我有一个sql查询,在添加 where 条款。如果没有where子句,它可以正常工作。

SELECT t.*, n.Caption, n.Description
FROM (
    SELECT NodeID
        , COUNT(DISTINCT CPUIndex) AS "Number of CPU"
        , COUNT(DISTINCT CPUIndex)/8 AS "Number of Cores"
    FROM CPUMultiLoad_Detail AS cpu WITH (NOLOCK)
    --WHERE N.Description LIKE '%T7%'
    GROUP BY NodeID
) AS T
INNER JOIN NodesData AS N ON T.NodeID = n.NodeID

但是有了 where 第一条错误如下:
无法绑定多部分标识符“n.description”。
我该怎么修?

3lxsmp7m

3lxsmp7m1#

要筛选的列属于表 NodesData ,在子查询中不可用。您需要将筛选移到外部查询:

select t.*, n.caption, n.description
from (
    select 
        nodeid, 
        count(distinct cpuindex) as number_of_cpu, 
        count(distinct cpuindex)/8 as number_of_cores
    from CPUMultiLoad_Detail
    group by nodeid
) as t
inner join NodesData as n on t.nodeid = n.nodeid
where n.description like '%t7%'

旁注: with(nolock) 是一般不需要的,还是应该一味的用-我把它去掉了
一般来说,最好使用不需要引用的标识符

相关问题