Sql 根据同一查询中返回的数据限制查询结果?

Sql 根据同一查询中返回的数据限制查询结果?,sql,oracle,Sql,Oracle,如果这是一个简单的解决方案,我会提前道歉,但因为我不知道该怎么称呼它,所以我没有找到它 给出以下示例数据: ID, QUANTITY, Date 1,2,01-APR-16 1,1,02-APR-16 1,0,03-APR-16 1,1,04-APR-16 1,0,05-APR-16 1,1,06-APR-16 1,0,07-APR-16 我想要一个查询,当数量等于零时返回项目的ID和相应的日期,但仅当数量大于零时才返回。因此,在上面的示例中,select将返回 1,03-APR-16 1,0

如果这是一个简单的解决方案,我会提前道歉,但因为我不知道该怎么称呼它,所以我没有找到它

给出以下示例数据:

ID, QUANTITY, Date
1,2,01-APR-16
1,1,02-APR-16
1,0,03-APR-16
1,1,04-APR-16
1,0,05-APR-16
1,1,06-APR-16
1,0,07-APR-16
我想要一个查询,当数量等于零时返回项目的ID和相应的日期,但仅当数量大于零时才返回。因此,在上面的示例中,select将返回

1,03-APR-16
1,05-APR-16

我从这个网站上学到了很多东西,但我不知道如何完成这个任务。我知道如何进行基本选择以及如何使用子查询,但在这种情况下,我似乎需要将一行的结果传递给另一行的子查询?谢谢你的指导,再次为你是一个新手感到抱歉。另外,一个关于如何以表格格式显示示例表的快速链接也会很有帮助,高级帮助没有显示我可能在错误的位置查找的部分。谢谢。

假设您的桌子叫t。我将列名date更改为dt-不要使用Oracle保留字作为列名

with a as (select id, max(dt) max_dt from t where quantity > 0 group by id)
select id, dt from t join a using (id) where quantity = 0 and t.dt < a.max_dt
补充:OP在下面的评论中要求附加条件。这将回答这个额外的请求

哦,是的,Mobilemike,就是你想法是一样的。通过一些练习,你将能够自己做这件事。注意:仅当最旧记录的值为0时,我才删除数量为0的记录。如果数量为0的最旧记录不是该id的绝对最旧记录,我不会删除该记录

祝你好运

with a as (select id, max(dt) max_dt from t where quantity > 0 group by id),
     b as (select id, min(dt) min_dt group by id)
select id, dt from t join a using (id) join b using (id)
where quantity = 0 and t.dt < a.max_dt and t.dt != b.min_dt

另一种使用子查询而不是临时表的解决方案可能如下所示

select t1.id, t1.dt from t t1, (select id, max(dt) max_dt from t where quantity>0 group by id)  t2
where  t1.id=t2.id and t1.dt < t2.max_dt and t1.quantity =0
我也更改了列名以匹配mathguy给出的列名


更新:我已将查询更改为帐户ID。

您可以使用分析来解决此问题,只需在表上读取一次:

with t as
(
  select 1 id, 2 QUANTITY, date'2016-04-01' dt from dual union all
  select 1 , 1 , date'2016-04-02' from dual union all
  select 1 , 0 , date'2016-04-03' from dual union all
  select 1 , 1 , date'2016-04-04' from dual union all
  select 1 , 0 , date'2016-04-05' from dual union all
  select 1 , 1 , date'2016-04-06' from dual union all
  select 1 , 0 , date'2016-04-07' from dual
)

select id, quantity, dt from (
  select id, quantity, t.dt, max( quantity ) over ( partition by id order by dt rows between current row and unbounded following ) nxt_qt
  from t
)
where quantity = 0 and nxt_qt > 0
order by 1, 3

您好,显然您不熟悉子查询分解。我的解决方案中有一个子查询,它只是写在with子句中;Oracle将其处理为子查询是在查询中出现的位置写入的。此外,在您的解决方案中,您可以在整个表上选择maxdt,只有当所有id都相同时才可以;在我的解决方案中,我考虑了表中不同id的可能性。这就是为什么“我的子查询”和“我的联接”都包含id的原因。此外,如果您的解决方案在所有行上都使用maxdt,而不仅仅是那些数量>0的行,那么如果按日期最后两行的数量都为0,则会给出不正确的结果。您的查询不会选择最后一个,但会选择倒数第二个,因为它的日期