小编Set*_*iei的帖子

由于列别名导致oracle中的标识符无效错误

我有一个查询如下

select t.col1,
 t.col2,
 (select count(col1)
  from tab 
  where col1 = t.col1
        and col2 = t.col2
   ) as col3 
from tab t
where col3 > 1
Run Code Online (Sandbox Code Playgroud)

该查询给出了"col3无效标识符"错误.

我尝试了不同的变体来定义我在下面给出的别名以及我在使用它时得到的错误

  1. select t.col1,
           t.col2,
           (select count(col1)
            from tab 
            where col1 = t.col1
              and col2 = t.col2
           ) as "col3" 
    from tab t
    where col3 > 1
    
    Run Code Online (Sandbox Code Playgroud)

错误:col3无效标识符

  1. select t.col1,
           t.col2,
           (select count(col1)
            from tab 
            where col1 = t.col1
              and col2 = t.col2
            ) as 'col3' 
    from tab t
    where [col3] > 1
    
    Run Code Online (Sandbox Code Playgroud)

错误:在哪里丢失表达式 …

sql oracle

6
推荐指数
1
解决办法
1万
查看次数

使用 Hibernate Criteria API 返回每组的第一行

我是 Hibernate 新手,我正在尝试编写一个条件查询来返回给定日期员工的最新状态

id  |   Status  | status_date
1   |   Active  |   1/10/2017
2   |   Active  |   1/10/2017
...
1   |   Inactive|   5/10/2017
...
1   |   Active  |   9/10/2017
Run Code Online (Sandbox Code Playgroud)

因此,我将向查询传递一个日期,并希望找到该日期每个员工的最新状态预期结果将是这样的

示例:对于日期 6/1/2017,这将是返回的数据

id  |   Status  |   Date
1   |   Inactive|   5/10/2017
2   |   Active  |   1/10/2017
Run Code Online (Sandbox Code Playgroud)

我能够使用 id 添加分组依据,并按状态日期按降序排列行。有没有办法可以只选择每组的顶行?我尝试在 status_date 上使用 max 函数,但这不起作用。

CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> cq = builder.createQuery(Employee);
Root<Employee> root = cq.from(Employee.class);
cq.select(root);
cq.groupBy(root.get("id"));
cq.orderBy(builder.desc(root.get("status_date")));
cq.having(builder.max(root.get("status_date")));
Run Code Online (Sandbox Code Playgroud)

java hibernate oracle11g hibernate-criteria

5
推荐指数
1
解决办法
4252
查看次数

其中count函数在where子句中的SQL查询

我有桌子跟踪部门和预算中心之间的关联.该协会是多对多的.现在我想只显示那些与其关联的预算中心超过10个的部门.所以我的查询应该是这样的

select dept,
       count(budget_centers) as bcCount
from myTable
where bcCount > 10
group by dept
Run Code Online (Sandbox Code Playgroud)

现在oracle会给出一个错误,说"bcCount"是一个无效的标识符.有没有办法重新定义oracle中可接受的查询

sql oracle

0
推荐指数
1
解决办法
126
查看次数

标签 统计

oracle ×2

sql ×2

hibernate ×1

hibernate-criteria ×1

java ×1

oracle11g ×1