分组依据和过滤器

时间:2017-07-23 16:38:47

标签: sql oracle

我有一张桌子如下。按ID分组,如果是count(item)> 1然后,过滤等于65或66的数据。如果count(item)= 1,则不执行任何操作。我们可以使用分析函数或实现此目的的最佳方法吗?

+----+------+
| Id | Item |
+----+------+
|  1 |  65  |
+----+------+
|  1 |  66  |
+----+------+
|  1 |  01  |
+----+------+
|  2 |  93  |
+----+------+
|  3 |  11  |
+----+------+
|  3 |  12  |
+----+------+

输出:

+----+------+
| Id | Item |
+----+------+
|  1 |  65  |
+----+------+
|  1 |  66  |
+----+------+
|  2 |  93  |
+----+------+

2 个答案:

答案 0 :(得分:0)

是:

Scene scene = new Scene( personTable );
personTable.applyCss();
personTable.layout();
saveAsPng( personTable, "test2.png" );

如果select id, item from (select t.*, count(*) over (partition by id) as cnt from t ) t where cnt > 1 and item in (65, 66) or cnt = 1; 是一个字符串,并且您希望前两个字符为65或66,那么类似于:

item

答案 1 :(得分:0)

试试这个

SELECT
   r.Id,
   r.Item,
   t1.count
FROM
results r
INNER JOIN 
(
    SELECT 
      Id,
      Count(Id) as count
  FROM
    results
  GROUP BY Id
) t1 ON t1.Id = r.ID
WHERE r.Item IN (65, 66) OR t1.count = 1;