具有大于和小于 Sybase 数据库的计数条件
Having count condition with greater than and less than for Sybase DB
我在 sybase 数据库中使用以下查询,我需要数据来显示计数(Field1 >=2 和 Field1 <=5)
Select * FROM Table1 WHERE Field1 IN( SELECT Field1 FROM Table1 GROUP BY Field1 HAVING COUNT(Field1) >= 2 ORDER BY Field1
我怎样才能做到这一点
你基本上就在那里:
Select *
FROM Table1
WHERE Field1 IN (SELECT Field1
FROM Table1
GROUP BY Field1
HAVING COUNT(Field1) BETWEEN 2 AND 5
)
ORDER BY Field1;
不过,我建议 window 函数:
select t1.*
from (select t1.*, count(*) over (partition by field1) as cnt
from table1 t1
) t1
where t1.cnt between 2 and 5;
我在 sybase 数据库中使用以下查询,我需要数据来显示计数(Field1 >=2 和 Field1 <=5)
Select * FROM Table1 WHERE Field1 IN( SELECT Field1 FROM Table1 GROUP BY Field1 HAVING COUNT(Field1) >= 2 ORDER BY Field1
我怎样才能做到这一点
你基本上就在那里:
Select *
FROM Table1
WHERE Field1 IN (SELECT Field1
FROM Table1
GROUP BY Field1
HAVING COUNT(Field1) BETWEEN 2 AND 5
)
ORDER BY Field1;
不过,我建议 window 函数:
select t1.*
from (select t1.*, count(*) over (partition by field1) as cnt
from table1 t1
) t1
where t1.cnt between 2 and 5;