从turnus等于1的查询返回所有行

时间:2016-12-09 09:07:26

标签: sql oracle having

我有一个Oracle Apex函数,它在日期范围内给出一系列行。序列中选取的项目从名为TURNUS的标识符中显示。例如:

日期范围:01-NOV-2016至30-NOV-2016
TURNUS是:7
回报是:
01-NOV-2016
07-NOV-2016
14-NOV-2016
21-NOV-2016
29-NOV-2016

如果我将TURNUS改为1,我会得到一个回复​​:
01-NOV-2016

我想看到的是:
01-NOV-2016
02-NOV-2016
...
30-NOV-2016

要收到结果,我在Oracle APEX App的前端使用了一个查询,该查询从我在数据库中创建的视图中选择数据。

让我们从内容表开始:

数据库表是:

CREATE TABLE  "GROUPS" 
(   "PK_ID" NUMBER, 
"NAME_OF_GROUP" VARCHAR2(120), 
"SEATS" NUMBER, 
"DATE_FROM" DATE, 
"TURNUS" VARCHAR2(120), 
"CREATE_DATE" DATE, 
"CREATED_BY" VARCHAR2(120), 
"FK_PUBLIC_FUNDING_BODY" NUMBER, 
"VALID_TILL" DATE, 
"DELETE_FLAG" NUMBER, 
"DESCRIPTION" VARCHAR2(300), 
"DATETIME_FROM" TIMESTAMP (6), 
"DATETIME_TILL" TIMESTAMP (6), 
"TIMEFROM" TIMESTAMP (6), 
"TIMETILL" TIMESTAMP (6), 
"DURATION" NUMBER, 
"FK_ROOM_ID" NUMBER, 
"DELETE_DATE" DATE, 
"GROUP_DELETE_REASON" VARCHAR2(255), 
 CONSTRAINT "GROUPS_PK" PRIMARY KEY ("PK_ID")
 USING INDEX  ENABLE
 )

在此之后我创建了一个视图:

CREATE OR REPLACE FORCE VIEW "VIEW_GROUP_DATERANGE" (
"DAY", "PK_ID", "TURNUS", "NAME_OF_GROUP",
"FK_PUBLIC_FUNDING_BODY", "TIME_FROM", "TIME_TILL", 
"DATETIME_FROM", "DATETIME_TILL", "D_FROM", "D_TO") AS 
with 
T_from_to as (
select
trunc(DATETIME_FROM) as d_from, 
trunc(DATETIME_TILL) as d_to,
DATETIME_FROM as DATETIME_FROM,
DATETIME_TILL as DATETIME_TILL,
PK_ID,
TURNUS,
NAME_OF_GROUP,
FK_PUBLIC_FUNDING_BODY
FROM GROUPS
),
T_seq as (
select level-1 as delta
from dual
connect by level-1 <= (select max(d_to-d_from) from T_from_to)
)
select d_from + delta as DAY, PK_ID, TURNUS, NAME_OF_GROUP,
FK_PUBLIC_FUNDING_BODY, TO_CHAR(DATETIME_FROM,'HH.MI') as TIME_FROM,
TO_CHAR(DATETIME_TILL,'HH.MI') as TIME_TILL, DATETIME_FROM, DATETIME_TILL,
d_from, d_to 
from T_from_to, T_seq
where d_from + delta <= d_to
order by day
/

前端查询:

SELECT rownum, ID, DAY, d_from, d_to, TURNUS,
NAME_OF_GROUP
FROM VIEW_GROUP_DATERANGE
WHERE ID= 1 (Variable ID of the Group)
GROUP BY rownum, ID, DAY, d_from, d_to, TURNUS, NAME_OF_GROUP
HAVING MOD(rownum,TURNUS) = 0 OR rownum = 2-1
ORDER BY rownum

除TURNUS ='1'外,结果显示正确。它只返回一行。我目前没有想法得到一个序列,当TURNUS为'1'时,该序列返回日期范围内的任何一行。

我想并尝试了一些CASE陈述,但没有成功。

非常感谢你。

2 个答案:

答案 0 :(得分:0)

从我身边试了一下。这就是我得到的,我也无法在这里获得1月1日。检查下面这可能会给你一些想法。我在考虑你只需要约会。

IoEnumerateDeviceObjectList

答案 1 :(得分:0)

我通过在我的视图中添加另一列来解决它,称为&#34; MOD_TURNUS&#34;这是一个案例陈述

T_from_to as (
select
 trunc(DATETIME_FROM) as d_from, 
  trunc(DATETIME_TILL) as d_to,
  DATETIME_FROM as DATETIME_FROM,
  DATETIME_TILL as DATETIME_TILL,
  PK_ID,
  TURNUS,
  CASE WHEN TURNUS > 1 THEN 1 ELSE 0 END as MOD_TURNUS,
  NAME_OF_GROUP,
  FK_PUBLIC_FUNDING_BODY
FROM GROUPS
),
T_seq as (
select level-1 as delta
from dual
connect by level-1 <= (select max(d_to-d_from) from T_from_to)
)
select d_from + delta as DAY, PK_ID, TURNUS, 
CASE WHEN TURNUS > 1 THEN 1 ELSE 0 END as MOD_TURNUS, 
NAME_OF_GROUP,  FK_PUBLIC_FUNDING_BODY, TO_CHAR(DATETIME_FROM,'HH.MI') as TIME_FROM, 
TO_CHAR(DATETIME_TILL,'HH.MI') as TIME_TILL, DATETIME_FROM, DATETIME_TILL, d_from, d_to
from T_from_to, T_seq
where d_from + delta <= d_to
order by day

在我的前端查询中,我将HAVING语句更改为并将MOD_TURNUS添加到组功能。

HAVING MOD(rownum,TURNUS) = MOD_TURNUS

......最后这么简单