假设我有一张桌子TableA
:
parent_id int
this_id int
filter int
this_date date
以及一些示例数据:
parent_id = 1, this_id = 1, filter = 1, this_date = ...
parent_id = 1, this_id = 2, filter = 0, this_date = ...
parent_id = 1, this_id = 3, filter = 1, this_date = ...
parent_id = 4, this_id = 4, filter = 0, this_date = ...
parent_id = 4, this_id = 5, filter = 0, this_date = ...
parent_id = 4, this_id = 6, filter = 1, this_date = ...
parent_id = null, this_id = 7, filter = 0, this_date = ...
parent_id = null, this_id = 8, filter = 1, this_date = ...
parent_id
总是和其中一个孩子的一样this_id
。如果没有孩子,parent_id
则为空
桌子不能改变,这就是我们所拥有的。
我创建了一个视图来给我总结:
SELECT ISNULL(parent_id,this_id) id, COUNT(*) numparts, MAX(this_date)...
FROM TableA
GROUP BY ISNULL(parent_id,this_id)
我想在我的摘要视图中添加具有filter=1
.
所以我对这个例子的结果(如果我做 a SELECT * FROM theview
)将是:
id numparts dt numOfFilter1
1 3 ... 2
4 3 ... 1
7 1 ... 0
8 1 ... 1
实际上,我的过滤器列将是一个子查询,但我想一旦我对这个位进行了排序,我就可以弄清楚这一点。