如何筛选MQ SQL查询结果

时间:2017-12-19 20:05:25

标签: sql-server

此查询:

select (sum(pages_kb))/1024 as 'sizein_mb',type as 'clerktype'
from sys.dm_os_memory_clerks
group by type order by (sum(pages_kb)*128)/1024 desc

返回:

  sizein_mb   clerktype  
1 10588       MEMORYCLERK_SQLBUFFERPOOL
2 6093        MEMORYCLERK_SQLQERESERVATIONS
3 458         MEMORYCLERK_SOSNODE
4 107         CACHESTORE_SQLCP

我只需要" 10588"

我尝试在最后使用select-where:

select (sizein_mb) where clerktype = 'MEMORYCLERK_SQLBUFFERPOOL'

但这只会返回一个无效的列名错误:

Msg 207, Level 16, State 1, Line 4
Invalid column name 'clerktype'.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'sizein_mb'.

4 个答案:

答案 0 :(得分:0)

Clerktype是您指定给“type”的别名,因此您需要将Type替换为clerktype。 sizein_mb ......

select (sum(pages_kb))/1024
from sys.dm_os_memory_clerks
where type = 'MEMORYCLERK_SQLBUFFERPOOL'

答案 1 :(得分:0)

您必须从原始查询中创建子查询,并在其外部选择。像这样:

SELECT sizein_mb
FROM
(
    SELECT (SUM(pages_kb))/1024 AS 'sizein_mb',type AS 'clerktype'
    FROM sys.dm_os_memory_clerks
    GROUP BY type
)
WHERE clerktype = 'MEMORYCLERK_SQLBUFFERPOOL'

答案 2 :(得分:0)

别名不能在where子句中使用。

解决方案是替换where clerktype = 'MEMORYCLERK_SQLBUFFERPOOL'having type = 'MEMORYCLERK_SQLBUFFERPOOL'

答案 3 :(得分:0)

添加HAVING应过滤所需的汇总。

SELECT (SUM(pages_kb))/1024 AS 'sizein_mb', type AS 'clerktype'
FROM sys.dm_os_memory_clerks
GROUP BY type 
HAVING (SUM(pages_kb))/1024 = 10588
ORDER BY sizein_mb DESC