获取整个组没有值的行
Get rows where the entire group does NOT have a value
给定 table 和分组数据(如下),什么是仅选择整个组没有指定值的值的查询?
这是我的数据:
id firstid lastid descrip
1 1 2 Y
2 2 3 Y
3 2 4 N
4 2 5 Y
5 3 6 Y
5 3 7 Y
5 4 8 N
5 4 9 N
6 4 10 Y
7 5 11 N
8 5 12 N
9 6 13 Y
10 6 14 Y
11 7 15 Y
12 7 16 N
在这种情况下,我想获取数据的 FirstId
,其中具有特定 FirstId
的行的 none 具有Descrip
个 Y
。例如,FirstId
2 中有一个“Y”,FirstId
7 中也有一个“Y”。所以这个例子的结果应该是“5”。
我试过了,
select FirstId, descrip from sampletable
where descrip <> 'Y'
group by FirstId, descrip
可能有点复杂,但我会尝试这样的事情:
select
a.firstid
from
sampletable a
left outer join (
select
id
from
sampletable
where
descrip = 'Y'
group by 1
) b ON a.id = b.id
where
b.id is null
group by 1
set nocount on
declare @sampletable table
(
id int
,firstid int
,lastid int
,descrip nvarchar(1)
)
insert into @sampletable values(1 ,1 ,2 ,'Y')
insert into @sampletable values(2 ,2 ,3 ,'Y')
insert into @sampletable values(3 ,2 ,4 ,'N')
insert into @sampletable values(4 ,2 ,5 ,'Y')
insert into @sampletable values(5 ,3 ,6 ,'Y')
insert into @sampletable values(5 ,3 ,7 ,'Y')
insert into @sampletable values(5 ,4 ,8 ,'N')
insert into @sampletable values(5 ,4 ,9 ,'N')
insert into @sampletable values(6 ,4 ,10,'Y')
insert into @sampletable values(7 ,5 ,11,'N')
insert into @sampletable values(8 ,5 ,12,'N')
insert into @sampletable values(9 ,6 ,13,'Y')
insert into @sampletable values(10,6 ,14,'Y')
insert into @sampletable values(11,7 ,15,'Y')
insert into @sampletable values(12,7 ,16,'N')
--Show FirstIDs where the # of "N" entries is the same as the total # of entries
SELECT ST.FIRSTID
FROM @SAMPLETABLE ST
WHERE ST.DESCRIP = 'N'
GROUP BY ST.FIRSTID, ST.DESCRIP
HAVING COUNT(*) = (
SELECT COUNT(*) FROM @SAMPLETABLE ST2 WHERE ST2.FIRSTID = ST.FIRSTID
)
给定 table 和分组数据(如下),什么是仅选择整个组没有指定值的值的查询?
这是我的数据:
id firstid lastid descrip
1 1 2 Y
2 2 3 Y
3 2 4 N
4 2 5 Y
5 3 6 Y
5 3 7 Y
5 4 8 N
5 4 9 N
6 4 10 Y
7 5 11 N
8 5 12 N
9 6 13 Y
10 6 14 Y
11 7 15 Y
12 7 16 N
在这种情况下,我想获取数据的 FirstId
,其中具有特定 FirstId
的行的 none 具有Descrip
个 Y
。例如,FirstId
2 中有一个“Y”,FirstId
7 中也有一个“Y”。所以这个例子的结果应该是“5”。
我试过了,
select FirstId, descrip from sampletable
where descrip <> 'Y'
group by FirstId, descrip
可能有点复杂,但我会尝试这样的事情:
select
a.firstid
from
sampletable a
left outer join (
select
id
from
sampletable
where
descrip = 'Y'
group by 1
) b ON a.id = b.id
where
b.id is null
group by 1
set nocount on
declare @sampletable table
(
id int
,firstid int
,lastid int
,descrip nvarchar(1)
)
insert into @sampletable values(1 ,1 ,2 ,'Y')
insert into @sampletable values(2 ,2 ,3 ,'Y')
insert into @sampletable values(3 ,2 ,4 ,'N')
insert into @sampletable values(4 ,2 ,5 ,'Y')
insert into @sampletable values(5 ,3 ,6 ,'Y')
insert into @sampletable values(5 ,3 ,7 ,'Y')
insert into @sampletable values(5 ,4 ,8 ,'N')
insert into @sampletable values(5 ,4 ,9 ,'N')
insert into @sampletable values(6 ,4 ,10,'Y')
insert into @sampletable values(7 ,5 ,11,'N')
insert into @sampletable values(8 ,5 ,12,'N')
insert into @sampletable values(9 ,6 ,13,'Y')
insert into @sampletable values(10,6 ,14,'Y')
insert into @sampletable values(11,7 ,15,'Y')
insert into @sampletable values(12,7 ,16,'N')
--Show FirstIDs where the # of "N" entries is the same as the total # of entries
SELECT ST.FIRSTID
FROM @SAMPLETABLE ST
WHERE ST.DESCRIP = 'N'
GROUP BY ST.FIRSTID, ST.DESCRIP
HAVING COUNT(*) = (
SELECT COUNT(*) FROM @SAMPLETABLE ST2 WHERE ST2.FIRSTID = ST.FIRSTID
)