聚合函数检查'如果包含'

时间:2011-03-25 17:10:47

标签: sql sql-server-2005 tsql

我有数据:

Code   Page     Task
    2   1   Activity
    2   2   Activity
    2   3   Activity Symbols
    2   4   Activity Symbols
    2   5   Activity Symbols
    2   12  Activity
    2   12  Activity Symbols
    2   999 Consider
    2   999 Constituents
    2   999 Material
    2   999 Material Hazards
    316 999 Constituents
    356 999 Constituents
    398 999 Constituents
    604 70  Activity Symbols
    604 999 Constituents

我希望按Code进行分组,并且每次检查该组中的任何记录是否包含Page的'999',如果是,则返回'True'。是否有一个聚合函数可以做到这一点?或者也许有更好的方法来构建这样的查询?

1 个答案:

答案 0 :(得分:19)

select code,
max(case when page = '999' then 1 else 0 end) as has999
from table
group by code