groupCol
同じだが複数の非 0を持つすべてのレコードを見つけようとしていますinfoCol
。これが私がやろうとしていることの完全な例です。
CREATE TABLE #t
(
groupCol varchar(14) NOT NULL,
infoCol int NOT NULL,
indexCol int NOT NULL IDENTITY (1, 1)
)
GO
insert into #t (groupCol, infoCol)
values ('NoRows',1), ('NoRows',1), ('NoRows',1), --Not in output due to having only 1 disticnt infoCol
('TwoResults',1), ('TwoResults',1), ('TwoResults',2), --In the output with "'TwoResults', 2"
('ThreeResults',1),('ThreeResults',2),('ThreeResults',3), --In the output with "'ThreeResults', 3"
('ExcludedZero',1),('ExcludedZero',1),('ExcludedZero',0) --Not in the output due to 0 being excluded for finding distinct infoCol's
('TwoAndZero',1), ('TwoAndZero',2), ('TwoAndZero',0) --In the output but returns 2 not 3.
select * from #t
select groupCol, count(groupCol) as distinctInfoCol
from #t
where infoCol <> 0
group by groupCol, infoCol
having count(groupCol) > 1
drop table #t
しかし、私のクエリの結果は
groupCol 個別のInfoCol -------------- --------------- 除外ゼロ 2 NoRows 3 2 つの結果 2
私の出力が
groupCol 個別のInfoCol -------------- --------------- 2 つの結果 2 3 つの結果 3 ツーアンドゼロ 2
何が間違っているのですか?必要な結果を得るためにこれを修正するにはどうすればよいですか?