目前我正在做一个项目,我需要从一个表中提取最新的数据用于报告目的。下面是示例表结构:-
我使用下面的SQL查询并能够提取数据。
CREATE TABLE COURSE ("STUDENT_ID" int, "COURSE_ID" int, "COURSE_NAME" varchar2(31), "COURSE_START_DATE" timestamp) ; INSERT ALL INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100001, -100, 'C Programming Language', '04-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100001, -200, 'Java Programming Language', '11-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100001, -300, 'C# Programming Language', '07-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100001, 100, 'Data Structure and algorithms', '05-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100001, 200, 'Computer Graphics', '13-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100001, 300, 'Networking', '02-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100002, -300, 'C# Programming Language', '12-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100002, -400, 'Python Programming Language', '07-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100002, -500, 'JavaScript Programming Language', '08-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100002, 100, 'Data Structure and algorithms', '17-Jan-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100002, 300, 'Computer Graphics', '26-Jan-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100002, 400, 'DataBase Management', '10-Jan-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100003, -500, 'JavaScript Programming Language', '07-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100003, -600, 'SQL', '13-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100003, -200, 'Java Programming Language', '17-Jan-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100003, 300, 'Networking', '04-Feb-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100003, 400, 'DataBase Management', '05-Jan-2019 12:00:00 AM') INTO COURSE ("STUDENT_ID", "COURSE_ID", "COURSE_NAME", "COURSE_START_DATE") VALUES (100003, 600, 'Cryptography', '18-Jan-2019 12:00:00 AM') SELECT * FROM dual ; SELECT STUDENT_ID ,COURSE_ID ,COURSE_NAME ,COURSE_START_DATE FROM ( SELECT ROW_NUMBER() OVER(PARTITION BY STUDENT_ID ORDER BY COURSE_START_DATE DESC) AS ROW_NUM ,STUDENT_ID ,COURSE_ID ,COURSE_NAME ,COURSE_START_DATE FROM COURSE WHERE COURSE_ID 0) TEMP1 WHERE TEMP1.ROW_NUM = 1;
但问题是真正的表非常大。大约有85k行,这个查询需要一些时间。还有其他更好的方法吗。我正在使用Oracle 11g R2。请建议
这是SQLfiddle链接http://sqlfiddle.com/#!4/b3fe1/8
您可以使用查询并将符号(课程id)
添加到分区:
查询:
SELECT STUDENT_ID
, COURSE_ID
, COURSE_NAME
, COURSE_START_DATE
FROM (
SELECT ROW_NUMBER() OVER (
PARTITION BY STUDENT_ID, SIGN( COURSE_ID )
ORDER BY COURSE_START_DATE DESC
) AS ROW_NUM
, STUDENT_ID
, COURSE_ID
, COURSE_NAME
, COURSE_START_DATE
FROM COURSE
WHERE COURSE_ID != 0
)
WHERE ROW_NUM = 1;
输出:
STUDENT_ID | COURSE_ID | COURSE_NAME | COURSE_START_DATE ---------: | --------: | :------------------------ | :--------------------------- 100001 | -200 | Java Programming Language | 11-FEB-19 12.00.00.000000 AM 100001 | 200 | Computer Graphics | 13-FEB-19 12.00.00.000000 AM 100002 | -300 | C# Programming Language | 12-FEB-19 12.00.00.000000 AM 100002 | 300 | Computer Graphics | 26-JAN-19 12.00.00.000000 AM 100003 | -600 | SQL | 13-FEB-19 12.00.00.000000 AM 100003 | 300 | Networking | 04-FEB-19 12.00.00.000000 AM
db
将CTE与row_number结合
with pro as
(
select t1.*, row_number() over(partition by student_id order by course_start_date desc) rn
from course
where course_id > 0 -- programming
)
, nonpro as
(
select t1.*, row_number() over(partition by student_id order by course_start_date desc) rn
from course
where course_id < 0 -- non-programming
)
select *
from pro
where rn = 1
union
select *
from nonpro
where rn = 1
您可以在下面尝试-您需要添加按学生ID划分的分区,当课程ID为
SELECT STUDENT_ID,COURSE_ID,COURSE_NAME,COURSE_START_DATE
FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY STUDENT_ID,case when course_id<0 then 1 else 2 end order by COURSE_START_DATE DESC) AS ROW_NUM
, STUDENT_ID,COURSE_ID,COURSE_NAME,COURSE_START_DATE FROM COURSE
)TEMP1 WHERE TEMP1.ROW_NUM = 1;
如何使用Java从MongoCollection检索最近添加的文档?大多数现有参考文献描述了如何在v2中实现这一点。在v3.3中如何做到这一点? 我想这与find(Bson filter)方法有关。如何指定Bson过滤器,如何将可查找表转换为文档?
我只是Kafka的新手,我有个问题: 我在Kafka中有一个主题“A”,我启动Spring boot应用程序并使用MessageChannel向主题“A”发送一些消息,然后我停止应用程序。 当我再次启动应用程序时,是否可以获取我发送到主题“A”的最新消息(并非所有消息)?我搜索了所有的解决方案,但它们对我帮助不大,如果我只发送新消息,它总是会立即收到消息。如果你有可运行的代码,请分享,我非常感谢:
我们目前使用Nexus OSS 3.0.0-03,我需要通过控制台检索最新的快照(或快照名称)。 在其他文章中提到的Rest-API(.../service/local/artifact/maven/...)在Nexus OSS3.x中不再可用(我没有找到一个404) null
我有一个渲染器,它可以更改JTable单元格的值。 在表的数据模型中,第1行第2列中的数据在JTable GUI中是“-” ,第1行第2列中的数据是“error” 这段代码打印数据模型中的值:“-”。 有什么简单的方法来检索值“error”吗?
{“type”:“record”、“name”:“twitter_schema”、“namespace”:“com.miguno.avro”、“fields”:[{“name”:“username”、“type”:“string”、“doc”:“Twitter.com上的用户帐户名称”}、{“name”:“tweet”、“type”:“string”、“doc”:“用户的Twitter消息内容”}
我试图创建一个简单的程序,从用户的名字,手机号码和电子邮件地址,然后把数据在Firebase实时数据库。 有3个输入框和一个按钮,按一下就可以完成上面的操作。代码如下: 我这样设置了消防基地: