我有两列,一个ID,另一个是0或1的值。我试图选择ID为0和1的所有行,例如,
{% if entity.name == 'My entity' %}
{{ form_row(form.name, { 'attr':{'disabled':'disabled'} }) }}
{% else %}
{{ form_row(form.name) }}
{% endif %}
{{ form_row(form._token) }}
结果应选择第4,5,8,9,10行
答案 0 :(得分:1)
您可以使用COUNT
的窗口版本:
SELECT RowNumber, ID, value
FROM (
SELECT RowNumber, ID, value,
COUNT(CASE WHEN value = 1 THEN 1 END) OVER (PARTITION BY ID) AS cntOnes,
COUNT(CASE WHEN value = 0 THEN 1 END) OVER (PARTITION BY ID) AS cntZeroes
FROM test
WHERE value IN (0,1) ) AS t
WHERE cntOnes >= 1 AND cntZeroes >= 1
如果两个 COUNT(DISTINCT value)
值存在于同一0, 1
切片中,则 ID
的值为2.
答案 1 :(得分:1)
DISTINCT
的窗口版本中使用 COUNT
,因此您可以使用MIN
和MAX
代替。
DECLARE @T TABLE(RN int, ID int, value int);
INSERT INTO @T (RN, ID, value) VALUES
(1, 001, 1),
(2, 001, 1),
(3, 001, 1),
(4, 002, 1),
(5, 002, 0),
(6, 003, 1),
(7, 003, 1),
(8, 004, 1),
(9, 004, 0),
(10, 004, 1);
WITH
CTE
AS
(
SELECT
RN, ID, value
,MIN(value) OVER (PARTITION BY ID) AS MinV
,MAX(value) OVER (PARTITION BY ID) AS MaxV
FROM @T AS T
)
SELECT RN, ID, value
FROM CTE
WHERE MinV <> MaxV
;
<强>结果强>
+----+----+-------+
| RN | ID | value |
+----+----+-------+
| 4 | 2 | 1 |
| 5 | 2 | 0 |
| 8 | 4 | 1 |
| 9 | 4 | 0 |
| 10 | 4 | 1 |
+----+----+-------+
答案 2 :(得分:1)
create table #shadowTemp (
RowNumber int not null,
Id char(3) not null,
value bit not null
)
insert into #shadowTemp values ( 1,'001', 0 )
insert into #shadowTemp values ( 2,'001', 1 )
insert into #shadowTemp values ( 3,'001', 1 )
insert into #shadowTemp values ( 4,'002', 0 )
insert into #shadowTemp values ( 5,'003', 0 )
insert into #shadowTemp values ( 6,'003', 1 )
select * from #shadowTemp;
;with cte ( Id ) As (
select Id
from #shadowTemp
group by Id
having sum( value + 1 ) >= 3
)
select a.*
from
#shadowTemp a
inner join cte b on ( a.Id = b.Id )
drop table #shadowTemp