oracle表
id timestamp status
1 2019-10-20 12:34:56.000 approved
1 2019-10-22 12:34:56.000 approved
2 2019-10-20 17:34:56.000 approved
2 2019-10-21 12:34:56.000 approved
3 2019-10-23 18:10:10.000 mod_in_ip
3 2019-10-24 11:10:10.000 approved
3 2019-10-24 12:10:10.000 approved
4 2019-10-25 12:10:10.000 approved
4 2019-10-25 18:10:10.000 approved
我想将id标记为新或已编辑。问题是从下午5点到下午5点的记录被视为工作时间即
从'2019-10-25 17:00:00'到'2019-10-26 17:00:00'将被视为工作时间
或
昨天的下午5点到今天的下午5点是工作时间。
例如:一个ID记录,该记录具有昨天的6:00 PM记录和今天的11AM记录,应标记为new 如果您看一下表格和预期结果,您就会知道
预期结果/结果应该是
1 edited
2 new
3 new
4 edited
最初,我尝试过此操作,但无法解决上述问题
select id,
case
when count(id)<=1 then 'New'
else 'Edited' End AS prefix
from(select id,status ,trunc(timestamp) from table
where
status='approved' and id in (1,2,3,4)
group by id,status,trunc(timestamp))
group by id
结果是
1 Edited
2 Edited
3 new
4 new
但预期结果是
1 edited
2 new
3 new
4 edited
我正在寻找诸如从下午5点到下午5点的内部查询组记录之类的解决方案,以便外部查询可以正常工作 或者完全不同的解决方案也是可行的
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=abd90416004000043c85316423d64b17
答案 0 :(得分:1)
如果要将5pm到5pm视为“同一天”,则很容易将Oracle日期向前或向后移动一天的时间(例如,可以将5pm向前移动7个小时以成为开始日期)。 “第二天”
public static void main(String[] args) {
int i, j, s;
System.out.println("Perfect numbers 1 to 1000: ");
for(i=1;i<=1000;i++){
s=0;
for(j=1;j<i;j++){
if(i%j==0){
s=s+j;
}
}
if(i==s){ //if i == s is perfect
System.out.println(i);
}
}
}
}
答案 1 :(得分:0)
这应该给您您想要的东西:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /usr/share/maven
Java version: 1.8.0_162, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF8
OS name: "linux", version: "4.15.0-1010-aws", arch: "amd64", family: "unix"
您可能需要检查结束条件-即如何在下午5点正好处理WITH cte(id, ts, NewCutoff, Row_Num) AS (
select
id,
ts,
MIN(
trunc(ts) +
CASE
WHEN EXTRACT(HOUR FROM ts) < 17 THEN INTERVAL '00 17' DAY TO HOUR
ELSE INTERVAL '01 17' DAY TO HOUR -- If time is after 5pm, add an extra day
END
) OVER(PARTITION BY id) AS NewCutoff, -- Determine cut-off TS for "new" entries
ROW_NUMBER() OVER(
PARTITION BY id ORDER BY ts DESC
) AS Row_Num -- Order entries by date within "id" group
FROM mytable
)
SELECT id, CASE WHEN ts < NewCutoff THEN 'new' ELSE 'edited' END AS prefix
FROM cte
WHERE Row_Num = 1 -- Only look at latest entry per id
;
值。